Elffuss BraidHub

kikocis/continue public

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