Merge pull request #7660 from continuedev/pe/autoformat-results

feat: add autoformatting detection for edit tools
This commit is contained in:
Nate Sesti 2025-09-11 10:36:09 -07:00 • committed by GitHub
commit bcc5600f29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 58 additions and 6 deletions

View file

@ -1061,6 +1061,7 @@ declare global {
filepath?: string;
fileContent?: string;
originalFileContent?: string;
autoFormattingDiff?: string;
}
export interface RangeInFileWithContents {

1
core/index.d.ts vendored
View file

@ -1387,6 +1387,7 @@ export interface ApplyState {
fileContent?: string;
originalFileContent?: string;
toolCallId?: string;
autoFormattingDiff?: string;
}
export interface StreamDiffLinesPayload {

View file

@ -1,8 +1,11 @@
import { Core } from "core/core";
import { DataLogger } from "core/data/log";
import { myersDiff } from "core/diff/myers";
import { ContinueGUIWebviewViewProvider } from "../ContinueGUIWebviewViewProvider";
import { editOutcomeTracker } from "../extension/EditOutcomeTracker";
import { VsCodeIde } from "../VsCodeIde";
import { VerticalDiffManager } from "./vertical/manager";
export async function processDiff(
@ -42,7 +45,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 +55,45 @@ 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 = preSaveContent.trim();
const normalizedPostSave = postSaveContent.trim();
if (normalizedPreSave !== normalizedPostSave) {
// Auto-formatting was applied by the editor
const diffLines = myersDiff(preSaveContent, postSaveContent);
autoFormattingDiff = diffLines
.map((line) => {
switch (line.type) {
case "old":
return `-${line.line}`;
case "new":
return `+${line.line}`;
case "same":
return ` ${line.line}`;
}
})
.join("\n");
}
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);
}

View file

@ -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({
@ -125,7 +143,6 @@ export const handleApplyStateUpdate = createAsyncThunk<
);
}
}
// TODO return output from edit tools so the model knows the result
}
}
}