From 09cefbcf67a955b4f6ac2be0aa97b115d2c37230 Mon Sep 17 00:00:00 2001 From: yiliang114 <1204183885@qq.com> Date: Tue, 9 Dec 2025 15:52:52 +0800 Subject: [PATCH] feat(vscode-ide-companion): add showDiff overload and file reading capability - Add overloaded showDiff method to support calling with only newContent - Implement readOldContentFromFs helper to read existing file content - Simplify IDE server openDiff tool to use minimal call site - Improve diff manager documentation and code clarity --- .../vscode-ide-companion/src/diff-manager.ts | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/vscode-ide-companion/src/diff-manager.ts b/packages/vscode-ide-companion/src/diff-manager.ts index 8abc7315..9a32769c 100644 --- a/packages/vscode-ide-companion/src/diff-manager.ts +++ b/packages/vscode-ide-companion/src/diff-manager.ts @@ -157,11 +157,21 @@ export class DiffManager { /** * Creates and shows a new diff view. - * @param filePath Path to the file being diffed - * @param oldContent The original content (left side) - * @param newContent The modified content (right side) + * - Overload 1: showDiff(filePath, newContent) + * - Overload 2: showDiff(filePath, oldContent, newContent) + * If only newContent is provided, the old content will be read from the + * filesystem (empty string when file does not exist). */ - async showDiff(filePath: string, oldContent: string, newContent: string) { + async showDiff(filePath: string, newContent: string): Promise; + async showDiff( + filePath: string, + oldContent: string, + newContent: string, + ): Promise; + async showDiff(filePath: string, a: string, b?: string): Promise { + const haveOld = typeof b === 'string'; + const oldContent = haveOld ? a : await this.readOldContentFromFs(filePath); + const newContent = haveOld ? (b as string) : a; const normalizedPath = path.normalize(filePath); const key = this.makeKey(normalizedPath, oldContent, newContent); @@ -400,6 +410,17 @@ export class DiffManager { } } + // Read the current content of file from the workspace; return empty string if not found + private async readOldContentFromFs(filePath: string): Promise { + try { + const fileUri = vscode.Uri.file(filePath); + const document = await vscode.workspace.openTextDocument(fileUri); + return document.getText(); + } catch { + return ''; + } + } + private makeKey(filePath: string, oldContent: string, newContent: string) { // Simple stable key; content could be large but kept transiently return `${filePath}\u241F${oldContent}\u241F${newContent}`;