Elffuss BraidHub

kikocis/continue public

code cleanup
Changeset21cf25b4ed37f7f3546ba6665ff66afb2228db7236704be2d4ccbce08c9dcf13
AuthorPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-06-26 17:45 -07:00
CommitterPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-06-26 17:45 -07:00
Parents9517700465b5

Browse the files at this changeset

9 files changed

deleted core/edit/searchAndReplace/generateSearchReplaceDiffLines.test.ts +0 −143
@@ -1,143 +0,0 @@
1-import { SearchMatchResult } from "./findSearchMatch";
2-import { generateSearchReplaceDiffLines } from "./generateSearchReplaceDiffLines";
3-
4-describe("generateSearchReplaceDiffLines", () => {
5- async function collectDiffLines(generator: AsyncGenerator<any>) {
6- const lines = [];
7- for await (const line of generator) {
8- lines.push(line);
9- }
10- return lines;
11- }
12-
13- it("should generate valid diff lines", async () => {
14- const fileContent = `const a = 1;
15-const b = 2;
16-const c = 3;`;
17-
18- const searchMatch: SearchMatchResult = {
19- startIndex: 13, // Start of "const b = 2;"
20- endIndex: 25, // End of "const b = 2;"
21- };
22-
23- const replaceContent = "const b = 42;";
24-
25- const diffLines = await collectDiffLines(
26- generateSearchReplaceDiffLines(fileContent, searchMatch, replaceContent),
27- );
28-
29- // Verify we get valid diff lines
30- expect(diffLines.length).toBeGreaterThan(0);
31-
32- // Verify all lines have valid types
33- diffLines.forEach((line) => {
34- expect(["old", "new", "same"]).toContain(line.type);
35- expect(typeof line.line).toBe("string");
36- });
37-
38- // Verify we have both old and new lines (indicating a change)
39- const hasOldLines = diffLines.some((line) => line.type === "old");
40- const hasNewLines = diffLines.some((line) => line.type === "new");
41- expect(hasOldLines).toBe(true);
42- expect(hasNewLines).toBe(true);
43- });
44-
45- it("should handle empty replacement (deletion)", async () => {
46- const fileContent = `const a = 1;
47-const b = 2;
48-const c = 3;`;
49-
50- const searchMatch: SearchMatchResult = {
51- startIndex: 12, // Start of "\nconst b = 2;"
52- endIndex: 25, // End of "const b = 2;"
53- };
54-
55- const replaceContent = "";
56-
57- const diffLines = await collectDiffLines(
58- generateSearchReplaceDiffLines(fileContent, searchMatch, replaceContent),
59- );
60-
61- // Should have old lines for the deleted content
62- expect(diffLines.length).toBeGreaterThan(0);
63- const hasOldLines = diffLines.some((line) => line.type === "old");
64- expect(hasOldLines).toBe(true);
65- });
66- it("should handle whole file replacement", async () => {
67- const fileContent = `old content`;
68- const searchMatch: SearchMatchResult = {
69- startIndex: 0,
70- endIndex: fileContent.length,
71- };
72-
73- const replaceContent = `completely new content`;
74- const diffLines = await collectDiffLines(
75- generateSearchReplaceDiffLines(fileContent, searchMatch, replaceContent),
76- );
77-
78- // Should have both old and new lines
79- const hasOldLines = diffLines.some((line) => line.type === "old");
80- const hasNewLines = diffLines.some((line) => line.type === "new");
81- expect(hasOldLines).toBe(true);
82- expect(hasNewLines).toBe(true);
83- });
84-
85- it("should handle empty file with insertion", async () => {
86- const fileContent = "";
87- const searchMatch: SearchMatchResult = {
88- startIndex: 0,
89- endIndex: 0,
90- };
91-
92- const replaceContent = `new content
93-second line`;
94-
95- const diffLines = await collectDiffLines(
96- generateSearchReplaceDiffLines(fileContent, searchMatch, replaceContent),
97- );
98-
99- // Should have new lines for the inserted content
100- expect(diffLines.length).toBeGreaterThan(0);
101- const hasNewLines = diffLines.some((line) => line.type === "new");
102- expect(hasNewLines).toBe(true);
103-
104- // Note: Empty file split by newlines gives [''] so there will be one "old" empty line
105- // This is correct behavior from the diff algorithm's perspective
106- });
107-
108- it("should produce correct final content when applied", async () => {
109- const fileContent = `line1
110-line2
111-line3`;
112-
113- const searchMatch: SearchMatchResult = {
114- startIndex: 6, // Start of "line2"
115- endIndex: 11, // End of "line2"
116- };
117-
118- const replaceContent = "replaced";
119-
120- // The expected final content
121- const expectedContent =
122- fileContent.substring(0, searchMatch.startIndex) +
123- replaceContent +
124- fileContent.substring(searchMatch.endIndex);
125-
126- const diffLines = await collectDiffLines(
127- generateSearchReplaceDiffLines(fileContent, searchMatch, replaceContent),
128- );
129-
130- // Reconstruct content from diff lines
131- const newLines = diffLines
132- .filter((line) => line.type === "new" || line.type === "same")
133- .map((line) => line.line);
134-
135- const reconstructedContent = newLines.join("\n");
136-
137- // Should exactly match expected content, including line structure
138- expect(reconstructedContent).toBe(expectedContent);
139-
140- // Also verify we have the correct number of lines
141- expect(newLines).toEqual(["line1", "replaced", "line3"]);
142- });
143-});
deleted core/edit/searchAndReplace/generateSearchReplaceDiffLines.ts +0 −39
@@ -1,39 +0,0 @@
1-import { DiffLine } from "../..";
2-import { streamDiff } from "../../diff/streamDiff";
3-import { SearchMatchResult } from "./findSearchMatch";
4-
5-/**
6- * Generates a stream of DiffLine objects for a search and replace operation.
7- * Uses the existing streamDiff algorithm to calculate the actual differences.
8- *
9- * @param fileContent - The complete content of the file
10- * @param searchMatch - The match result with start/end character positions
11- * @param replaceContent - The content to replace the matched section with
12- * @returns AsyncGenerator of DiffLine objects for streaming to VerticalDiffManager
13- */
14-export async function* generateSearchReplaceDiffLines(
15- fileContent: string,
16- searchMatch: SearchMatchResult,
17- replaceContent: string,
18-): AsyncGenerator<DiffLine> {
19- // Apply the replacement to get the new file content
20- const newFileContent =
21- fileContent.substring(0, searchMatch.startIndex) +
22- replaceContent +
23- fileContent.substring(searchMatch.endIndex);
24-
25- // Split into lines for the diff algorithm
26- const oldLines = fileContent.split("\n");
27- const newLines = newFileContent.split("\n");
28-
29- // Create a simple async generator for the new lines
30- // Don't use streamLines since we already have complete lines
31- async function* newLinesGenerator() {
32- for (const line of newLines) {
33- yield line;
34- }
35- }
36-
37- // Stream the diff using existing infrastructure
38- yield* streamDiff(oldLines, newLinesGenerator());
39-}
modified core/edit/searchAndReplace/parseSearchReplaceBlock.ts +6 −1
@@ -96,6 +96,7 @@
9696 *
9797 * @param content - The content containing multiple search/replace blocks
9898 * @returns Array of complete search/replace blocks found
99+ * @throws Error if any malformed blocks are encountered
99100 */
100101 export function parseAllSearchReplaceBlocks(
101102 content: string,
@@ -115,7 +116,11 @@
115116 const result = parseSearchReplaceBlock(blockContent);
116117 if (result.isComplete) {
117118 blocks.push(result);
119+ } else if (result.error) {
120+ // Fail fast on any parsing error
121+ throw new Error(result.error);
118122 }
123+ // If incomplete but no error, it's just a partial block - continue processing
119124 }
120125 currentBlockLines = [line];
121126 inBlock = true;
@@ -129,7 +134,7 @@
129134 if (result.isComplete) {
130135 blocks.push(result);
131136 } else if (result.error) {
132- // If there's an error, throw it immediately
137+ // Fail fast on any parsing error
133138 throw new Error(result.error);
134139 }
135140 currentBlockLines = [];
modified core/index.d.ts +7 −0
@@ -1290,6 +1290,13 @@
12901290 filepath: string;
12911291 }
12921292
1293+export interface ApplyToFilePayload {
1294+ streamId: string;
1295+ filepath?: string;
1296+ text: string;
1297+ toolCallId?: string;
1298+}
1299+
12931300 export interface RangeInFileWithContents {
12941301 filepath: string;
12951302 range: {
modified core/protocol/ideWebview.ts +2 −11
@@ -4,6 +4,7 @@
44 import {
55 AcceptOrRejectDiffPayload,
66 ApplyState,
7+ ApplyToFilePayload,
78 HighlightedCodePayload,
89 MessageContent,
910 RangeInFileWithContents,
@@ -13,17 +14,7 @@
1314
1415 export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & {
1516 openUrl: [string, void];
16- applyToFile: [
17- {
18- text: string;
19- streamId: string;
20- filepath?: string;
21- toolCallId?: string;
22- searchContent?: string;
23- isSearchReplace?: boolean;
24- },
25- void,
26- ];
17+ applyToFile: [ApplyToFilePayload, void];
2718 overwriteFile: [{ filepath: string; prevFileContent: string | null }, void];
2819 showTutorial: [undefined, void];
2920 showFile: [ShowFilePayload, void];
modified extensions/vscode/src/apply/ApplyManager.ts +3 −62
@@ -3,23 +3,12 @@
33 import { getUriPathBasename } from "core/util/uri";
44 import * as vscode from "vscode";
55
6+import { ApplyToFilePayload } from "core";
67 import { ApplyAbortManager } from "core/edit/applyAbortManager";
7-import { findSearchMatch } from "core/edit/searchAndReplace/findSearchMatch";
8-import { generateSearchReplaceDiffLines } from "core/edit/searchAndReplace/generateSearchReplaceDiffLines";
98 import { VerticalDiffManager } from "../diff/vertical/manager";
109 import { VsCodeIde } from "../VsCodeIde";
1110 import { VsCodeWebviewProtocol } from "../webviewProtocol";
1211
13-export interface ApplyToFileOptions {
14- streamId: string;
15- filepath?: string;
16- text: string;
17- toolCallId?: string;
18- // NEW: Search/replace specific fields
19- searchContent?: string;
20- isSearchReplace?: boolean;
21-}
22-
2312 /**
2413 * Handles applying text/code to files including diff generation and streaming
2514 */
@@ -36,9 +25,7 @@
3625 filepath,
3726 text,
3827 toolCallId,
39- searchContent,
40- isSearchReplace,
41- }: ApplyToFileOptions) {
28+ }: ApplyToFilePayload) {
4229 await this.webviewProtocol.request("updateApplyState", {
4330 streamId,
4431 status: "streaming",
@@ -56,19 +43,7 @@
5643 return;
5744 }
5845
59- // NEW: Handle search/replace mode
60- if (isSearchReplace && searchContent !== undefined) {
61- await this.handleSearchReplace(
62- activeTextEditor,
63- searchContent,
64- text, // This is the replace content
65- streamId,
66- toolCallId,
67- );
68- return;
69- }
70-
71- // Existing logic for regular apply
46+ // Regular apply logic
7247 const hasExistingDocument = !!activeTextEditor.document.getText().trim();
7348
7449 if (hasExistingDocument) {
@@ -168,40 +143,6 @@
168143 }
169144 }
170145
171- /**
172- * NEW: Handle search and replace operations
173- */
174- private async handleSearchReplace(
175- editor: vscode.TextEditor,
176- searchContent: string,
177- replaceContent: string,
178- streamId: string,
179- toolCallId?: string,
180- ) {
181- const fileContent = editor.document.getText();
182-
183- // Find the search content in the file
184- const match = findSearchMatch(fileContent, searchContent);
185- if (!match) {
186- throw new Error(`Search content not found in file:\n${searchContent}`);
187- }
188-
189- // Generate diff lines using our search/replace diff generator
190- const diffLinesGenerator = generateSearchReplaceDiffLines(
191- fileContent,
192- match,
193- replaceContent,
194- );
195-
196- // Stream through existing VerticalDiffManager infrastructure
197- await this.verticalDiffManager.streamDiffLines(
198- diffLinesGenerator,
199- true, // isInstantApply = true (deterministic, no LLM involved)
200- streamId,
201- toolCallId,
202- );
203- }
204-
205146 /**
206147 * Creates a prompt for applying code edits
207148 */
modified gui/src/pages/gui/ToolCallDiv/EditFile.tsx +3 −2
@@ -11,8 +11,9 @@
1111 type EditToolCallProps = {
1212 relativeFilePath: string;
1313 changes: string;
14- toolCallId?: string;
1514 historyIndex: number;
15+ toolCallId?: string;
16+ expandCodeblocks?: boolean;
1617 };
1718
1819 export function EditFile(props: EditToolCallProps) {
@@ -42,7 +43,7 @@
4243
4344 return (
4445 <StyledMarkdownPreview
45- expandCodeblocks // TODO: Conditional based on edit tool or search & replace
46+ expandCodeblocks={props.expandCodeblocks ?? false}
4647 isRenderingInStepContainer
4748 disableManualApply
4849 source={src}
modified gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx +1 −0
@@ -36,6 +36,7 @@
3636 case BuiltInToolNames.SearchAndReplaceInFile:
3737 return (
3838 <EditFile
39+ expandCodeblocks
3940 relativeFilePath={args.filepath ?? ""}
4041 changes={args.diff ?? ""}
4142 toolCallId={toolCall.id}
modified gui/src/util/clientTools/searchReplaceImpl.ts +0 −2
@@ -58,13 +58,11 @@
5858 }
5959
6060 // Single applyToFile call with all accumulated changes
61- // Use regular apply mode (not search/replace mode) to leverage existing diff logic
6261 await extras.ideMessenger.request("applyToFile", {
6362 text: currentContent,
6463 streamId: extras.streamId,
6564 filepath: resolvedFilepath,
6665 toolCallId,
67- // Don't use search/replace mode since we're providing the complete final content
6866 });
6967
7068 // Return success - applyToFile will handle the completion state