| 1 | import { ConfigHandler } from "core/config/ConfigHandler"; |
| 2 | import { applyCodeBlock } from "core/edit/lazy/applyCodeBlock"; |
| 3 | import { getUriPathBasename } from "core/util/uri"; |
| 4 | import * as vscode from "vscode"; |
| 5 | |
| 6 | import { ApplyToFilePayload } from "core"; |
| 7 | import { myersDiff } from "core/diff/myers"; |
| 8 | import { generateLines } from "core/diff/util"; |
| 9 | import { ApplyAbortManager } from "core/edit/applyAbortManager"; |
| 10 | import { VerticalDiffManager } from "../diff/vertical/manager"; |
| 11 | import { VsCodeIde } from "../VsCodeIde"; |
| 12 | import { VsCodeWebviewProtocol } from "../webviewProtocol"; |
| 13 | |
| 14 | /** |
| 15 | * Handles applying text/code to files including diff generation and streaming |
| 16 | */ |
| 17 | export class ApplyManager { |
| 18 | constructor( |
| 19 | private readonly ide: VsCodeIde, |
| 20 | private readonly webviewProtocol: VsCodeWebviewProtocol, |
| 21 | private readonly verticalDiffManager: VerticalDiffManager, |
| 22 | private readonly configHandler: ConfigHandler, |
| 23 | ) {} |
| 24 | |
| 25 | async applyToFile({ |
| 26 | streamId, |
| 27 | filepath, |
| 28 | text, |
| 29 | toolCallId, |
| 30 | isSearchAndReplace, |
| 31 | }: ApplyToFilePayload) { |
| 32 | if (filepath) { |
| 33 | await this.ensureFileOpen(filepath); |
| 34 | } |
| 35 | |
| 36 | const { activeTextEditor } = vscode.window; |
| 37 | if (!activeTextEditor) { |
| 38 | void vscode.window.showErrorMessage("No active editor to apply edits to"); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | // Capture the original file content before applying changes |
| 43 | const originalFileContent = activeTextEditor.document.getText(); |
| 44 | |
| 45 | await this.webviewProtocol.request("updateApplyState", { |
| 46 | streamId, |
| 47 | status: "streaming", |
| 48 | fileContent: text, |
| 49 | originalFileContent, |
| 50 | toolCallId, |
| 51 | }); |
| 52 | |
| 53 | const hasExistingDocument = !!activeTextEditor.document.getText().trim(); |
| 54 | if (hasExistingDocument) { |
| 55 | // Currently `isSearchAndReplace` will always provide a full file rewrite |
| 56 | // as the contents of `text`, so we can just instantly apply |
| 57 | if (isSearchAndReplace) { |
| 58 | const diffLinesGenerator = generateLines( |
| 59 | myersDiff(activeTextEditor.document.getText(), text), |
| 60 | ); |
| 61 | |
| 62 | await this.verticalDiffManager.streamDiffLines( |
| 63 | diffLinesGenerator, |
| 64 | true, |
| 65 | streamId, |
| 66 | toolCallId, |
| 67 | ); |
| 68 | } else { |
| 69 | await this.handleExistingDocument( |
| 70 | activeTextEditor, |
| 71 | text, |
| 72 | streamId, |
| 73 | toolCallId, |
| 74 | ); |
| 75 | } |
| 76 | } else { |
| 77 | await this.handleEmptyDocument( |
| 78 | activeTextEditor, |
| 79 | text, |
| 80 | streamId, |
| 81 | toolCallId, |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private async ensureFileOpen(filepath: string): Promise<void> { |
| 87 | const fileExists = await this.ide.fileExists(filepath); |
| 88 | if (!fileExists) { |
| 89 | await this.ide.writeFile(filepath, ""); |
| 90 | await this.ide.openFile(filepath); |
| 91 | } |
| 92 | await this.ide.openFile(filepath); |
| 93 | } |
| 94 | |
| 95 | private async handleEmptyDocument( |
| 96 | editor: vscode.TextEditor, |
| 97 | text: string, |
| 98 | streamId: string, |
| 99 | toolCallId?: string, |
| 100 | ) { |
| 101 | await editor.edit((builder) => |
| 102 | builder.insert(new vscode.Position(0, 0), text), |
| 103 | ); |
| 104 | |
| 105 | await this.webviewProtocol.request("updateApplyState", { |
| 106 | streamId, |
| 107 | status: "closed", |
| 108 | numDiffs: 0, |
| 109 | fileContent: text, |
| 110 | toolCallId, |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | private async handleExistingDocument( |
| 115 | editor: vscode.TextEditor, |
| 116 | text: string, |
| 117 | streamId: string, |
| 118 | toolCallId?: string, |
| 119 | ) { |
| 120 | const { config } = await this.configHandler.loadConfig(); |
| 121 | if (!config) { |
| 122 | void vscode.window.showErrorMessage("Config not loaded"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | const llm = |
| 127 | config.selectedModelByRole.apply ?? config.selectedModelByRole.chat; |
| 128 | if (!llm) { |
| 129 | void vscode.window.showErrorMessage( |
| 130 | `No model with roles "apply" or "chat" found in config.`, |
| 131 | ); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | const fileUri = editor.document.uri.toString(); |
| 136 | const abortManager = ApplyAbortManager.getInstance(); |
| 137 | const abortController = abortManager.get(fileUri); |
| 138 | |
| 139 | const { isInstantApply, diffLinesGenerator } = await applyCodeBlock( |
| 140 | editor.document.getText(), |
| 141 | text, |
| 142 | getUriPathBasename(fileUri), |
| 143 | llm, |
| 144 | abortController, |
| 145 | ); |
| 146 | |
| 147 | if (isInstantApply) { |
| 148 | await this.verticalDiffManager.streamDiffLines( |
| 149 | diffLinesGenerator, |
| 150 | isInstantApply, |
| 151 | streamId, |
| 152 | toolCallId, |
| 153 | ); |
| 154 | } else { |
| 155 | await this.handleNonInstantDiff( |
| 156 | editor, |
| 157 | text, |
| 158 | llm, |
| 159 | streamId, |
| 160 | this.verticalDiffManager, |
| 161 | toolCallId, |
| 162 | ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Creates a prompt for applying code edits |
| 168 | */ |
| 169 | private getApplyPrompt(text: string): string { |
| 170 | return `The following code was suggested as an edit:\n\`\`\`\n${text}\n\`\`\`\nPlease apply it to the previous code.`; |
| 171 | } |
| 172 | |
| 173 | private async handleNonInstantDiff( |
| 174 | editor: vscode.TextEditor, |
| 175 | text: string, |
| 176 | llm: any, |
| 177 | streamId: string, |
| 178 | verticalDiffManager: VerticalDiffManager, |
| 179 | toolCallId?: string, |
| 180 | ) { |
| 181 | const { config } = await this.configHandler.loadConfig(); |
| 182 | if (!config) { |
| 183 | void vscode.window.showErrorMessage("Config not loaded"); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | const prompt = this.getApplyPrompt(text); |
| 188 | const fullEditorRange = new vscode.Range( |
| 189 | 0, |
| 190 | 0, |
| 191 | editor.document.lineCount - 1, |
| 192 | editor.document.lineAt(editor.document.lineCount - 1).text.length, |
| 193 | ); |
| 194 | const rangeToApplyTo = editor.selection.isEmpty |
| 195 | ? fullEditorRange |
| 196 | : editor.selection; |
| 197 | |
| 198 | await verticalDiffManager.streamEdit({ |
| 199 | input: prompt, |
| 200 | llm, |
| 201 | streamId, |
| 202 | range: rangeToApplyTo, |
| 203 | newCode: text, |
| 204 | toolCallId, |
| 205 | rulesToInclude: undefined, // No rules for apply |
| 206 | }); |
| 207 | } |
| 208 | } |