@@ -21,7 +21,7 @@ import { extname } from '../../../util/vs/base/common/resources';
2121import { count } from '../../../util/vs/base/common/strings' ;
2222import { URI } from '../../../util/vs/base/common/uri' ;
2323import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation' ;
24- import { Position as ExtPosition , LanguageModelPromptTsxPart , LanguageModelTextPart , LanguageModelToolResult , MarkdownString , TextEdit } from '../../../vscodeTypes' ;
24+ import { Position as ExtPosition , Range as ExtRange , LanguageModelPromptTsxPart , LanguageModelTextPart , LanguageModelToolResult , MarkdownString , TextEdit } from '../../../vscodeTypes' ;
2525import { CodeBlockProcessor } from '../../codeBlocks/node/codeBlockProcessor' ;
2626import { IBuildPromptContext } from '../../prompt/common/intents' ;
2727import { renderPromptElementJSON } from '../../prompts/node/base/promptRenderer' ;
@@ -114,7 +114,15 @@ export class CreateFileTool implements ICopilotTool<ICreateFileParams> {
114114 this . sendTelemetry ( options . chatRequestId , modelId , fileExtension ) ;
115115 } else {
116116 const content = removeLeadingFilepathComment ( options . input . content , languageId , options . input . filePath ) ;
117- this . _promptContext . stream . textEdit ( uri , TextEdit . insert ( new ExtPosition ( 0 , 0 ) , content ) ) ;
117+ // When the file has been deleted from disk but VS Code still holds a stale
118+ // in-memory doc with content, use a full-document replace so the old buffer
119+ // is overwritten rather than prepended to (https://github.com/microsoft/vscode/issues/311043).
120+ if ( ! fileExists && doc && doc . getText ( ) . length > 0 ) {
121+ const lastLine = doc . lineCount - 1 ;
122+ this . _promptContext . stream . textEdit ( uri , TextEdit . replace ( new ExtRange ( 0 , 0 , lastLine , doc . lineAt ( lastLine ) . text . length ) , content ) ) ;
123+ } else {
124+ this . _promptContext . stream . textEdit ( uri , TextEdit . insert ( new ExtPosition ( 0 , 0 ) , content ) ) ;
125+ }
118126 this . _promptContext . stream . textEdit ( uri , true ) ;
119127 this . sendTelemetry ( options . chatRequestId , modelId , fileExtension ) ;
120128 return new LanguageModelToolResult ( [
0 commit comments