import { arePathsEqual } from "@/utils/path" import * as path from "path" import * as vscode from "vscode" import { DecorationController } from "@integrations/editor/DecorationController" import { DIFF_VIEW_URI_SCHEME, DiffViewProvider } from "@integrations/editor/DiffViewProvider" export class VscodeDiffViewProvider extends DiffViewProvider { override async openDiffEditor(): Promise { if (!this.absolutePath) { throw new Error("No file path set") } // get diagnostics before editing the file, we'll compare to diagnostics after editing to see if cline needs to fix anything this.preDiagnostics = vscode.languages.getDiagnostics() // if the file was already open, close it (must happen after showing the diff view since if it's the only tab the column will close) this.documentWasOpen = false // close the tab if it's open (it's already been saved) const tabs = vscode.window.tabGroups.all .map((tg) => tg.tabs) .flat() .filter((tab) => tab.input instanceof vscode.TabInputText && arePathsEqual(tab.input.uri.fsPath, this.absolutePath)) for (const tab of tabs) { if (!tab.isDirty) { await vscode.window.tabGroups.close(tab) } this.documentWasOpen = true } const uri = vscode.Uri.file(this.absolutePath) // If this diff editor is already open (ie if a previous write file was interrupted) then we should activate that instead of opening a new diff const diffTab = vscode.window.tabGroups.all .flatMap((group) => group.tabs) .find( (tab) => tab.input instanceof vscode.TabInputTextDiff && tab.input?.original?.scheme === DIFF_VIEW_URI_SCHEME && arePathsEqual(tab.input.modified.fsPath, uri.fsPath), ) if (diffTab && diffTab.input instanceof vscode.TabInputTextDiff) { // Use already open diff editor. this.activeDiffEditor = await vscode.window.showTextDocument(diffTab.input.modified, { preserveFocus: true, }) } else { // Open new diff editor. this.activeDiffEditor = await new Promise((resolve, reject) => { const fileName = path.basename(uri.fsPath) const fileExists = this.editType === "modify" const disposable = vscode.window.onDidChangeActiveTextEditor((editor) => { if (editor && arePathsEqual(editor.document.uri.fsPath, uri.fsPath)) { disposable.dispose() resolve(editor) } }) vscode.commands.executeCommand( "vscode.diff", vscode.Uri.parse(`${DIFF_VIEW_URI_SCHEME}:${fileName}`).with({ query: Buffer.from(this.originalContent ?? "").toString("base64"), }), uri, `${fileName}: ${fileExists ? "Original ↔ Cline's Changes" : "New File"} (Editable)`, { preserveFocus: true, }, ) // This may happen on very slow machines ie project idx setTimeout(() => { disposable.dispose() reject(new Error("Failed to open diff editor, please try again...")) }, 10_000) }) } this.fadedOverlayController = new DecorationController("fadedOverlay", this.activeDiffEditor) this.activeLineController = new DecorationController("activeLine", this.activeDiffEditor) // Apply faded overlay to all lines initially this.fadedOverlayController.addLines(0, this.activeDiffEditor.document.lineCount) } override async replaceText( content: string, rangeToReplace: { startLine: number; endLine: number }, currentLine: number, ): Promise { const document = this.activeDiffEditor?.document if (!document) { throw new Error("User closed text editor, unable to edit file...") } const edit = new vscode.WorkspaceEdit() const range = new vscode.Range(rangeToReplace.startLine, 0, rangeToReplace.endLine, 0) edit.replace(document.uri, range, content) await vscode.workspace.applyEdit(edit) // Update decorations for the entire changed section this.activeLineController?.setActiveLine(currentLine) this.fadedOverlayController?.updateOverlayAfterLine(currentLine, document.lineCount) } override async scrollEditorToLine(line: number): Promise { if (!this.activeDiffEditor) { return } const scrollLine = line + 4 this.activeDiffEditor.revealRange(new vscode.Range(scrollLine, 0, scrollLine, 0), vscode.TextEditorRevealType.InCenter) } override async scrollAnimation(startLine: number, endLine: number): Promise { const totalLines = endLine - startLine const numSteps = 10 // Adjust this number to control animation speed const stepSize = Math.max(1, Math.floor(totalLines / numSteps)) // Create and await the smooth scrolling animation for (let line = startLine; line <= endLine; line += stepSize) { this.activeDiffEditor?.revealRange(new vscode.Range(line, 0, line, 0), vscode.TextEditorRevealType.InCenter) await new Promise((resolve) => setTimeout(resolve, 16)) // ~60fps } } }