| 1 | import { Core } from "core/core"; |
| 2 | import { DataLogger } from "core/data/log"; |
| 3 | import { myersDiff } from "core/diff/myers"; |
| 4 | |
| 5 | import { ContinueGUIWebviewViewProvider } from "../ContinueGUIWebviewViewProvider"; |
| 6 | import { editOutcomeTracker } from "../extension/EditOutcomeTracker"; |
| 7 | import { VsCodeIde } from "../VsCodeIde"; |
| 8 | |
| 9 | import { VerticalDiffManager } from "./vertical/manager"; |
| 10 | |
| 11 | export async function processDiff( |
| 12 | action: "accept" | "reject", |
| 13 | sidebar: ContinueGUIWebviewViewProvider, |
| 14 | ide: VsCodeIde, |
| 15 | core: Core, |
| 16 | verticalDiffManager: VerticalDiffManager, |
| 17 | newFileUri?: string, |
| 18 | streamId?: string, |
| 19 | toolCallId?: string, |
| 20 | ) { |
| 21 | let newOrCurrentUri = newFileUri; |
| 22 | if (!newOrCurrentUri) { |
| 23 | const currentFile = await ide.getCurrentFile(); |
| 24 | newOrCurrentUri = currentFile?.path; |
| 25 | } |
| 26 | if (!newOrCurrentUri) { |
| 27 | console.warn( |
| 28 | `No file provided or current file open while attempting to resolve diff`, |
| 29 | ); |
| 30 | return; |
| 31 | } |
| 32 | |
| 33 | await ide.openFile(newOrCurrentUri); |
| 34 | |
| 35 | // If streamId is not provided, try to get it from the VerticalDiffManager |
| 36 | if (!streamId) { |
| 37 | streamId = verticalDiffManager.getStreamIdForFile(newOrCurrentUri); |
| 38 | } |
| 39 | |
| 40 | // Clear vertical diffs depending on action |
| 41 | verticalDiffManager.clearForfileUri(newOrCurrentUri, action === "accept"); |
| 42 | if (action === "reject") { |
| 43 | // this is so that IDE reject diff command can also cancel apply |
| 44 | core.invoke("cancelApply", undefined); |
| 45 | } |
| 46 | |
| 47 | if (streamId) { |
| 48 | // Capture file content before save to detect autoformatting |
| 49 | const preSaveContent = await ide.readFile(newOrCurrentUri); |
| 50 | |
| 51 | // Record the edit outcome before updating the apply state |
| 52 | await editOutcomeTracker.recordEditOutcome( |
| 53 | streamId, |
| 54 | action === "accept", |
| 55 | DataLogger.getInstance(), |
| 56 | ); |
| 57 | |
| 58 | // Save the file |
| 59 | await ide.saveFile(newOrCurrentUri); |
| 60 | |
| 61 | // Capture file content after save to detect autoformatting |
| 62 | const postSaveContent = await ide.readFile(newOrCurrentUri); |
| 63 | |
| 64 | // Detect autoformatting by comparing normalized content |
| 65 | let autoFormattingDiff: string | undefined; |
| 66 | const normalizedPreSave = preSaveContent.trim(); |
| 67 | const normalizedPostSave = postSaveContent.trim(); |
| 68 | |
| 69 | if (normalizedPreSave !== normalizedPostSave) { |
| 70 | // Auto-formatting was applied by the editor |
| 71 | const diffLines = myersDiff(preSaveContent, postSaveContent); |
| 72 | autoFormattingDiff = diffLines |
| 73 | .map((line) => { |
| 74 | switch (line.type) { |
| 75 | case "old": |
| 76 | return `-${line.line}`; |
| 77 | case "new": |
| 78 | return `+${line.line}`; |
| 79 | case "same": |
| 80 | return ` ${line.line}`; |
| 81 | } |
| 82 | }) |
| 83 | .join("\n"); |
| 84 | } |
| 85 | |
| 86 | await sidebar.webviewProtocol.request("updateApplyState", { |
| 87 | fileContent: postSaveContent, // Use post-save content |
| 88 | filepath: newOrCurrentUri, |
| 89 | streamId, |
| 90 | status: "closed", |
| 91 | numDiffs: 0, |
| 92 | toolCallId, |
| 93 | autoFormattingDiff, // Include autoformatting diff |
| 94 | }); |
| 95 | } else { |
| 96 | // Save the file even if no streamId |
| 97 | await ide.saveFile(newOrCurrentUri); |
| 98 | } |
| 99 | } |