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>
This commit is contained in:
parent
fcc3bddc4f
commit
ebded1fc67
5 changed files with 90 additions and 6 deletions
|
|
@ -1061,6 +1061,7 @@ declare global {
|
|||
filepath?: string;
|
||||
fileContent?: string;
|
||||
originalFileContent?: string;
|
||||
autoFormattingDiff?: string;
|
||||
}
|
||||
|
||||
export interface RangeInFileWithContents {
|
||||
|
|
|
|||
1
core/index.d.ts
vendored
1
core/index.d.ts
vendored
|
|
@ -1382,6 +1382,7 @@ export interface ApplyState {
|
|||
fileContent?: string;
|
||||
originalFileContent?: string;
|
||||
toolCallId?: string;
|
||||
autoFormattingDiff?: string;
|
||||
}
|
||||
|
||||
export interface StreamDiffLinesPayload {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
41
extensions/vscode/src/util/diffUtils.ts
Normal file
41
extensions/vscode/src/util/diffUtils.ts
Normal file
|
|
@ -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();
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue