Elffuss BraidHub

kikocis/continue public

Add autoformatting detection for edit tools

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 <noreply@anthropic.com>
Changeset9c134d8c1202e7aa8d2bee46f77b25bd2ee2d36bb495ca1ff0ea95ce76d3b44d
AuthorPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-09-09 11:11 -07:00
CommitterPatrick Erichsen <patrick.a.erichsen@gmail.com> · 2025-09-09 11:11 -07:00
Parents825624809c09

Browse the files at this changeset

5 files changed

modified core/config/types.ts +1 −0
@@ -1061,6 +1061,7 @@
10611061 filepath?: string;
10621062 fileContent?: string;
10631063 originalFileContent?: string;
1064+ autoFormattingDiff?: string;
10641065 }
10651066
10661067 export interface RangeInFileWithContents {
modified core/index.d.ts +1 −0
@@ -1382,6 +1382,7 @@
13821382 fileContent?: string;
13831383 originalFileContent?: string;
13841384 toolCallId?: string;
1385+ autoFormattingDiff?: string;
13851386 }
13861387
13871388 export interface StreamDiffLinesPayload {
modified extensions/vscode/src/diff/processDiff.ts +29 −5
@@ -1,7 +1,9 @@
11 import { Core } from "core/core";
22 import { DataLogger } from "core/data/log";
3+
34 import { ContinueGUIWebviewViewProvider } from "../ContinueGUIWebviewViewProvider";
45 import { editOutcomeTracker } from "../extension/EditOutcomeTracker";
6+import { createPrettyPatch, normalizeContent } from "../util/diffUtils";
57 import { VsCodeIde } from "../VsCodeIde";
68 import { VerticalDiffManager } from "./vertical/manager";
79
@@ -42,7 +44,8 @@
4244 }
4345
4446 if (streamId) {
45- const fileContent = await ide.readFile(newOrCurrentUri);
47+ // Capture file content before save to detect autoformatting
48+ const preSaveContent = await ide.readFile(newOrCurrentUri);
4649
4750 // Record the edit outcome before updating the apply state
4851 await editOutcomeTracker.recordEditOutcome(
@@ -51,16 +54,37 @@
5154 DataLogger.getInstance(),
5255 );
5356
57+ // Save the file
58+ await ide.saveFile(newOrCurrentUri);
59+
60+ // Capture file content after save to detect autoformatting
61+ const postSaveContent = await ide.readFile(newOrCurrentUri);
62+
63+ // Detect autoformatting by comparing normalized content
64+ let autoFormattingDiff: string | undefined;
65+ const normalizedPreSave = normalizeContent(preSaveContent);
66+ const normalizedPostSave = normalizeContent(postSaveContent);
67+
68+ if (normalizedPreSave !== normalizedPostSave) {
69+ // Auto-formatting was applied by the editor
70+ autoFormattingDiff = createPrettyPatch(
71+ newOrCurrentUri,
72+ preSaveContent,
73+ postSaveContent,
74+ );
75+ }
76+
5477 await sidebar.webviewProtocol.request("updateApplyState", {
55- fileContent,
78+ fileContent: postSaveContent, // Use post-save content
5679 filepath: newOrCurrentUri,
5780 streamId,
5881 status: "closed",
5982 numDiffs: 0,
6083 toolCallId,
84+ autoFormattingDiff, // Include autoformatting diff
6185 });
86+ } else {
87+ // Save the file even if no streamId
88+ await ide.saveFile(newOrCurrentUri);
6289 }
63-
64- // Save the file
65- await ide.saveFile(newOrCurrentUri);
6690 }
added extensions/vscode/src/util/diffUtils.ts +41 −0
@@ -0,0 +1,41 @@
1+import { myersDiff } from "core/diff/myers";
2+
3+/**
4+ * Creates a pretty patch diff string similar to unified diff format
5+ * Uses Continue's existing myersDiff function for diff calculation
6+ */
7+export function createPrettyPatch(
8+ filename: string = "file",
9+ oldStr: string = "",
10+ newStr: string = "",
11+): string {
12+ if (oldStr === newStr) {
13+ return "";
14+ }
15+
16+ const diffLines = myersDiff(oldStr, newStr);
17+ const patchLines: string[] = [];
18+
19+ for (const diffLine of diffLines) {
20+ switch (diffLine.type) {
21+ case "old":
22+ patchLines.push(`-${diffLine.line}`);
23+ break;
24+ case "new":
25+ patchLines.push(`+${diffLine.line}`);
26+ break;
27+ case "same":
28+ patchLines.push(` ${diffLine.line}`);
29+ break;
30+ }
31+ }
32+
33+ return patchLines.join("\n");
34+}
35+
36+/**
37+ * Normalizes content by trimming whitespace for comparison
38+ */
39+export function normalizeContent(content: string): string {
40+ return content.trim();
41+}
No newline at the end of the file
modified gui/src/redux/thunks/handleApplyStateUpdate.ts +18 −1
@@ -83,6 +83,24 @@
8383 }
8484
8585 if (accepted) {
86+ // Add autoformatting diff to tool output if present
87+ if (applyState.autoFormattingDiff) {
88+ dispatch(
89+ updateToolCallOutput({
90+ toolCallId: applyState.toolCallId,
91+ contextItems: [
92+ {
93+ icon: "info",
94+ name: "Auto-formatting Applied",
95+ description: "Editor auto-formatting changes",
96+ 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.)`,
97+ hidden: false,
98+ },
99+ ],
100+ }),
101+ );
102+ }
103+
86104 if (toolCallState.status !== "errored") {
87105 dispatch(
88106 acceptToolCall({
@@ -98,7 +116,6 @@
98116 );
99117 }
100118 }
101- // TODO return output from edit tools so the model knows the result
102119 }
103120 }
104121 }