Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,7 @@ declare global {
filepath?: string;
fileContent?: string;
originalFileContent?: string;
autoFormattingDiff?: string;
}

export interface RangeInFileWithContents {
Expand Down
1 change: 1 addition & 0 deletions core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@ export interface ApplyState {
fileContent?: string;
originalFileContent?: string;
toolCallId?: string;
autoFormattingDiff?: string;
}

export interface StreamDiffLinesPayload {
Expand Down
43 changes: 38 additions & 5 deletions extensions/vscode/src/diff/processDiff.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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);
}
19 changes: 18 additions & 1 deletion gui/src/redux/thunks/handleApplyStateUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -98,7 +116,6 @@ export const handleApplyStateUpdate = createAsyncThunk<
);
}
}
// TODO return output from edit tools so the model knows the result
}
}
}
Expand Down
Loading