Changesets · Branches · Tags · Compare
| Changeset | 21cf25b4ed37f7f3546ba6665ff66afb2228db7236704be2d4ccbce08c9dcf13 |
|---|---|
| Author | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-06-26 17:45 -07:00 |
| Committer | Patrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-06-26 17:45 -07:00 |
| Parents | 9517700465b5 |
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 @@ | ||
| 96 | 96 | * |
| 97 | 97 | * @param content - The content containing multiple search/replace blocks |
| 98 | 98 | * @returns Array of complete search/replace blocks found |
| 99 | + * @throws Error if any malformed blocks are encountered | |
| 99 | 100 | */ |
| 100 | 101 | export function parseAllSearchReplaceBlocks( |
| 101 | 102 | content: string, |
| @@ -115,7 +116,11 @@ | ||
| 115 | 116 | const result = parseSearchReplaceBlock(blockContent); |
| 116 | 117 | if (result.isComplete) { |
| 117 | 118 | blocks.push(result); |
| 119 | + } else if (result.error) { | |
| 120 | + // Fail fast on any parsing error | |
| 121 | + throw new Error(result.error); | |
| 118 | 122 | } |
| 123 | + // If incomplete but no error, it's just a partial block - continue processing | |
| 119 | 124 | } |
| 120 | 125 | currentBlockLines = [line]; |
| 121 | 126 | inBlock = true; |
| @@ -129,7 +134,7 @@ | ||
| 129 | 134 | if (result.isComplete) { |
| 130 | 135 | blocks.push(result); |
| 131 | 136 | } else if (result.error) { |
| 132 | - // If there's an error, throw it immediately | |
| 137 | + // Fail fast on any parsing error | |
| 133 | 138 | throw new Error(result.error); |
| 134 | 139 | } |
| 135 | 140 | currentBlockLines = []; |
modified core/index.d.ts +7 −0
| @@ -1290,6 +1290,13 @@ | ||
| 1290 | 1290 | filepath: string; |
| 1291 | 1291 | } |
| 1292 | 1292 | |
| 1293 | +export interface ApplyToFilePayload { | |
| 1294 | + streamId: string; | |
| 1295 | + filepath?: string; | |
| 1296 | + text: string; | |
| 1297 | + toolCallId?: string; | |
| 1298 | +} | |
| 1299 | + | |
| 1293 | 1300 | export interface RangeInFileWithContents { |
| 1294 | 1301 | filepath: string; |
| 1295 | 1302 | range: { |
modified core/protocol/ideWebview.ts +2 −11
| @@ -4,6 +4,7 @@ | ||
| 4 | 4 | import { |
| 5 | 5 | AcceptOrRejectDiffPayload, |
| 6 | 6 | ApplyState, |
| 7 | + ApplyToFilePayload, | |
| 7 | 8 | HighlightedCodePayload, |
| 8 | 9 | MessageContent, |
| 9 | 10 | RangeInFileWithContents, |
| @@ -13,17 +14,7 @@ | ||
| 13 | 14 | |
| 14 | 15 | export type ToIdeFromWebviewProtocol = ToIdeFromWebviewOrCoreProtocol & { |
| 15 | 16 | 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]; | |
| 27 | 18 | overwriteFile: [{ filepath: string; prevFileContent: string | null }, void]; |
| 28 | 19 | showTutorial: [undefined, void]; |
| 29 | 20 | showFile: [ShowFilePayload, void]; |
modified extensions/vscode/src/apply/ApplyManager.ts +3 −62
| @@ -3,23 +3,12 @@ | ||
| 3 | 3 | import { getUriPathBasename } from "core/util/uri"; |
| 4 | 4 | import * as vscode from "vscode"; |
| 5 | 5 | |
| 6 | +import { ApplyToFilePayload } from "core"; | |
| 6 | 7 | import { ApplyAbortManager } from "core/edit/applyAbortManager"; |
| 7 | -import { findSearchMatch } from "core/edit/searchAndReplace/findSearchMatch"; | |
| 8 | -import { generateSearchReplaceDiffLines } from "core/edit/searchAndReplace/generateSearchReplaceDiffLines"; | |
| 9 | 8 | import { VerticalDiffManager } from "../diff/vertical/manager"; |
| 10 | 9 | import { VsCodeIde } from "../VsCodeIde"; |
| 11 | 10 | import { VsCodeWebviewProtocol } from "../webviewProtocol"; |
| 12 | 11 | |
| 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 | - | |
| 23 | 12 | /** |
| 24 | 13 | * Handles applying text/code to files including diff generation and streaming |
| 25 | 14 | */ |
| @@ -36,9 +25,7 @@ | ||
| 36 | 25 | filepath, |
| 37 | 26 | text, |
| 38 | 27 | toolCallId, |
| 39 | - searchContent, | |
| 40 | - isSearchReplace, | |
| 41 | - }: ApplyToFileOptions) { | |
| 28 | + }: ApplyToFilePayload) { | |
| 42 | 29 | await this.webviewProtocol.request("updateApplyState", { |
| 43 | 30 | streamId, |
| 44 | 31 | status: "streaming", |
| @@ -56,19 +43,7 @@ | ||
| 56 | 43 | return; |
| 57 | 44 | } |
| 58 | 45 | |
| 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 | |
| 72 | 47 | const hasExistingDocument = !!activeTextEditor.document.getText().trim(); |
| 73 | 48 | |
| 74 | 49 | if (hasExistingDocument) { |
| @@ -168,40 +143,6 @@ | ||
| 168 | 143 | } |
| 169 | 144 | } |
| 170 | 145 | |
| 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 | - | |
| 205 | 146 | /** |
| 206 | 147 | * Creates a prompt for applying code edits |
| 207 | 148 | */ |
modified gui/src/pages/gui/ToolCallDiv/EditFile.tsx +3 −2
| @@ -11,8 +11,9 @@ | ||
| 11 | 11 | type EditToolCallProps = { |
| 12 | 12 | relativeFilePath: string; |
| 13 | 13 | changes: string; |
| 14 | - toolCallId?: string; | |
| 15 | 14 | historyIndex: number; |
| 15 | + toolCallId?: string; | |
| 16 | + expandCodeblocks?: boolean; | |
| 16 | 17 | }; |
| 17 | 18 | |
| 18 | 19 | export function EditFile(props: EditToolCallProps) { |
| @@ -42,7 +43,7 @@ | ||
| 42 | 43 | |
| 43 | 44 | return ( |
| 44 | 45 | <StyledMarkdownPreview |
| 45 | - expandCodeblocks // TODO: Conditional based on edit tool or search & replace | |
| 46 | + expandCodeblocks={props.expandCodeblocks ?? false} | |
| 46 | 47 | isRenderingInStepContainer |
| 47 | 48 | disableManualApply |
| 48 | 49 | source={src} |
modified gui/src/pages/gui/ToolCallDiv/FunctionSpecificToolCallDiv.tsx +1 −0
| @@ -36,6 +36,7 @@ | ||
| 36 | 36 | case BuiltInToolNames.SearchAndReplaceInFile: |
| 37 | 37 | return ( |
| 38 | 38 | <EditFile |
| 39 | + expandCodeblocks | |
| 39 | 40 | relativeFilePath={args.filepath ?? ""} |
| 40 | 41 | changes={args.diff ?? ""} |
| 41 | 42 | toolCallId={toolCall.id} |
modified gui/src/util/clientTools/searchReplaceImpl.ts +0 −2
| @@ -58,13 +58,11 @@ | ||
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | // Single applyToFile call with all accumulated changes |
| 61 | - // Use regular apply mode (not search/replace mode) to leverage existing diff logic | |
| 62 | 61 | await extras.ideMessenger.request("applyToFile", { |
| 63 | 62 | text: currentContent, |
| 64 | 63 | streamId: extras.streamId, |
| 65 | 64 | filepath: resolvedFilepath, |
| 66 | 65 | toolCallId, |
| 67 | - // Don't use search/replace mode since we're providing the complete final content | |
| 68 | 66 | }); |
| 69 | 67 | |
| 70 | 68 | // Return success - applyToFile will handle the completion state |