From ebded1fc6710a9d07090efcd7a2857dadf26e63b Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Tue, 9 Sep 2025 11:11:04 -0700 Subject: [PATCH] Add autoformatting detection for edit tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implement autoformatting detection similar to Cline's approach to improve reliability of edit operations. When edit tools (multiEdit/editFile) are executed, the system now captures file content before and after save operations to detect editor autoformatting changes. Key changes: - Add `createPrettyPatch` utility using existing `myersDiff` function - Capture pre/post save content in `processDiff.ts` - Add `autoFormattingDiff` field to `ApplyState` protocol - Include autoformatting diff in tool responses with guidance for language model - Help prevent future edit failures due to undetected formatting changes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- core/config/types.ts | 1 + core/index.d.ts | 1 + extensions/vscode/src/diff/processDiff.ts | 34 ++++++++++++--- extensions/vscode/src/util/diffUtils.ts | 41 +++++++++++++++++++ .../redux/thunks/handleApplyStateUpdate.ts | 19 ++++++++- 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 extensions/vscode/src/util/diffUtils.ts diff --git a/core/config/types.ts b/core/config/types.ts index 6048cf4d3..0b76419df 100644 --- a/core/config/types.ts +++ b/core/config/types.ts @@ -1061,6 +1061,7 @@ declare global { filepath?: string; fileContent?: string; originalFileContent?: string; + autoFormattingDiff?: string; } export interface RangeInFileWithContents { diff --git a/core/index.d.ts b/core/index.d.ts index a31e02c67..815fd9448 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -1382,6 +1382,7 @@ export interface ApplyState { fileContent?: string; originalFileContent?: string; toolCallId?: string; + autoFormattingDiff?: string; } export interface StreamDiffLinesPayload { diff --git a/extensions/vscode/src/diff/processDiff.ts b/extensions/vscode/src/diff/processDiff.ts index f350e12b4..d46b4ef9d 100644 --- a/extensions/vscode/src/diff/processDiff.ts +++ b/extensions/vscode/src/diff/processDiff.ts @@ -1,7 +1,9 @@ import { Core } from "core/core"; import { DataLogger } from "core/data/log"; + import { ContinueGUIWebviewViewProvider } from "../ContinueGUIWebviewViewProvider"; import { editOutcomeTracker } from "../extension/EditOutcomeTracker"; +import { createPrettyPatch, normalizeContent } from "../util/diffUtils"; import { VsCodeIde } from "../VsCodeIde"; import { VerticalDiffManager } from "./vertical/manager"; @@ -42,7 +44,8 @@ export async function processDiff( } if (streamId) { - const fileContent = await ide.readFile(newOrCurrentUri); + // Capture file content before save to detect autoformatting + const preSaveContent = await ide.readFile(newOrCurrentUri); // Record the edit outcome before updating the apply state await editOutcomeTracker.recordEditOutcome( @@ -51,16 +54,37 @@ export async function processDiff( DataLogger.getInstance(), ); + // Save the file + await ide.saveFile(newOrCurrentUri); + + // Capture file content after save to detect autoformatting + const postSaveContent = await ide.readFile(newOrCurrentUri); + + // Detect autoformatting by comparing normalized content + let autoFormattingDiff: string | undefined; + const normalizedPreSave = normalizeContent(preSaveContent); + const normalizedPostSave = normalizeContent(postSaveContent); + + if (normalizedPreSave !== normalizedPostSave) { + // Auto-formatting was applied by the editor + autoFormattingDiff = createPrettyPatch( + newOrCurrentUri, + preSaveContent, + postSaveContent, + ); + } + await sidebar.webviewProtocol.request("updateApplyState", { - fileContent, + fileContent: postSaveContent, // Use post-save content filepath: newOrCurrentUri, streamId, status: "closed", numDiffs: 0, toolCallId, + autoFormattingDiff, // Include autoformatting diff }); + } else { + // Save the file even if no streamId + await ide.saveFile(newOrCurrentUri); } - - // Save the file - await ide.saveFile(newOrCurrentUri); } diff --git a/extensions/vscode/src/util/diffUtils.ts b/extensions/vscode/src/util/diffUtils.ts new file mode 100644 index 000000000..4e53fb8c6 --- /dev/null +++ b/extensions/vscode/src/util/diffUtils.ts @@ -0,0 +1,41 @@ +import { myersDiff } from "core/diff/myers"; + +/** + * Creates a pretty patch diff string similar to unified diff format + * Uses Continue's existing myersDiff function for diff calculation + */ +export function createPrettyPatch( + filename: string = "file", + oldStr: string = "", + newStr: string = "", +): string { + if (oldStr === newStr) { + return ""; + } + + const diffLines = myersDiff(oldStr, newStr); + const patchLines: string[] = []; + + for (const diffLine of diffLines) { + switch (diffLine.type) { + case "old": + patchLines.push(`-${diffLine.line}`); + break; + case "new": + patchLines.push(`+${diffLine.line}`); + break; + case "same": + patchLines.push(` ${diffLine.line}`); + break; + } + } + + return patchLines.join("\n"); +} + +/** + * Normalizes content by trimming whitespace for comparison + */ +export function normalizeContent(content: string): string { + return content.trim(); +} \ No newline at end of file diff --git a/gui/src/redux/thunks/handleApplyStateUpdate.ts b/gui/src/redux/thunks/handleApplyStateUpdate.ts index 25532e750..8d7c1bec0 100644 --- a/gui/src/redux/thunks/handleApplyStateUpdate.ts +++ b/gui/src/redux/thunks/handleApplyStateUpdate.ts @@ -83,6 +83,24 @@ export const handleApplyStateUpdate = createAsyncThunk< } if (accepted) { + // Add autoformatting diff to tool output if present + if (applyState.autoFormattingDiff) { + dispatch( + updateToolCallOutput({ + toolCallId: applyState.toolCallId, + contextItems: [ + { + icon: "info", + name: "Auto-formatting Applied", + description: "Editor auto-formatting changes", + content: `Along with your edits, the editor applied the following auto-formatting:\n\n${applyState.autoFormattingDiff}\n\n(Note: Pay close attention to changes such as single quotes being converted to double quotes, semicolons being removed or added, long lines being broken into multiple lines, adjusting indentation style, adding/removing trailing commas, etc. This will help you ensure future SEARCH/REPLACE operations to this file are accurate.)`, + hidden: false, + }, + ], + }), + ); + } + if (toolCallState.status !== "errored") { dispatch( acceptToolCall({ @@ -98,7 +116,6 @@ export const handleApplyStateUpdate = createAsyncThunk< ); } } - // TODO return output from edit tools so the model knows the result } } }