({
streamId,
filepath,
text,
toolCallId,
isSearchAndReplace,
}: ApplyToFilePayload)
| 26 | ) {} |
| 27 | |
| 28 | async applyToFile({ |
| 29 | streamId, |
| 30 | filepath, |
| 31 | text, |
| 32 | toolCallId, |
| 33 | isSearchAndReplace, |
| 34 | }: ApplyToFilePayload) { |
| 35 | if (filepath) { |
| 36 | await this.ensureFileOpen(filepath); |
| 37 | } |
| 38 | |
| 39 | const { activeTextEditor } = vscode.window; |
| 40 | if (!activeTextEditor) { |
| 41 | void vscode.window.showErrorMessage("No active editor to apply edits to"); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | // Capture the original file content before applying changes |
| 46 | const originalFileContent = activeTextEditor.document.getText(); |
| 47 | |
| 48 | await this.webviewProtocol.request("updateApplyState", { |
| 49 | streamId, |
| 50 | status: "streaming", |
| 51 | fileContent: text, |
| 52 | originalFileContent, |
| 53 | toolCallId, |
| 54 | }); |
| 55 | |
| 56 | const hasExistingDocument = !!activeTextEditor.document.getText().trim(); |
| 57 | if (hasExistingDocument) { |
| 58 | // Currently `isSearchAndReplace` will always provide a full file rewrite |
| 59 | // as the contents of `text`, so we can just instantly apply |
| 60 | if (isSearchAndReplace) { |
| 61 | await this.verticalDiffManager.instantApplyDiff( |
| 62 | originalFileContent, |
| 63 | text, |
| 64 | streamId, |
| 65 | toolCallId, |
| 66 | ); |
| 67 | } else { |
| 68 | await this.handleExistingDocument( |
| 69 | activeTextEditor, |
| 70 | text, |
| 71 | streamId, |
| 72 | toolCallId, |
| 73 | ); |
| 74 | } |
| 75 | } else { |
| 76 | await this.handleEmptyDocument( |
| 77 | activeTextEditor, |
| 78 | text, |
| 79 | streamId, |
| 80 | toolCallId, |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private async ensureFileOpen(filepath: string): Promise<void> { |
no test coverage detected