Elffuss BraidHub

kikocis/continue public

9c134d8c1202 · 2.8 KiBRawPermalink
1import { Core } from "core/core";
2import { DataLogger } from "core/data/log";
3
4import { ContinueGUIWebviewViewProvider } from "../ContinueGUIWebviewViewProvider";
5import { editOutcomeTracker } from "../extension/EditOutcomeTracker";
6import { createPrettyPatch, normalizeContent } from "../util/diffUtils";
7import { VsCodeIde } from "../VsCodeIde";
8import { VerticalDiffManager } from "./vertical/manager";
9
10export async function processDiff(
11 action: "accept" | "reject",
12 sidebar: ContinueGUIWebviewViewProvider,
13 ide: VsCodeIde,
14 core: Core,
15 verticalDiffManager: VerticalDiffManager,
16 newFileUri?: string,
17 streamId?: string,
18 toolCallId?: string,
19) {
20 let newOrCurrentUri = newFileUri;
21 if (!newOrCurrentUri) {
22 const currentFile = await ide.getCurrentFile();
23 newOrCurrentUri = currentFile?.path;
24 }
25 if (!newOrCurrentUri) {
26 console.warn(
27 `No file provided or current file open while attempting to resolve diff`,
28 );
29 return;
30 }
31
32 await ide.openFile(newOrCurrentUri);
33
34 // If streamId is not provided, try to get it from the VerticalDiffManager
35 if (!streamId) {
36 streamId = verticalDiffManager.getStreamIdForFile(newOrCurrentUri);
37 }
38
39 // Clear vertical diffs depending on action
40 verticalDiffManager.clearForfileUri(newOrCurrentUri, action === "accept");
41 if (action === "reject") {
42 // this is so that IDE reject diff command can also cancel apply
43 core.invoke("cancelApply", undefined);
44 }
45
46 if (streamId) {
47 // Capture file content before save to detect autoformatting
48 const preSaveContent = await ide.readFile(newOrCurrentUri);
49
50 // Record the edit outcome before updating the apply state
51 await editOutcomeTracker.recordEditOutcome(
52 streamId,
53 action === "accept",
54 DataLogger.getInstance(),
55 );
56
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
77 await sidebar.webviewProtocol.request("updateApplyState", {
78 fileContent: postSaveContent, // Use post-save content
79 filepath: newOrCurrentUri,
80 streamId,
81 status: "closed",
82 numDiffs: 0,
83 toolCallId,
84 autoFormattingDiff, // Include autoformatting diff
85 });
86 } else {
87 // Save the file even if no streamId
88 await ide.saveFile(newOrCurrentUri);
89 }
90}