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
This commit is contained in:
yiliang114
2025-12-09 15:52:52 +08:00
parent fcd4bb9c03
commit 09cefbcf67

View File

@@ -157,11 +157,21 @@ export class DiffManager {
/** /**
* Creates and shows a new diff view. * Creates and shows a new diff view.
* @param filePath Path to the file being diffed * - Overload 1: showDiff(filePath, newContent)
* @param oldContent The original content (left side) * - Overload 2: showDiff(filePath, oldContent, newContent)
* @param newContent The modified content (right side) * 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<void>;
async showDiff(
filePath: string,
oldContent: string,
newContent: string,
): Promise<void>;
async showDiff(filePath: string, a: string, b?: string): Promise<void> {
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 normalizedPath = path.normalize(filePath);
const key = this.makeKey(normalizedPath, oldContent, newContent); 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<string> {
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) { private makeKey(filePath: string, oldContent: string, newContent: string) {
// Simple stable key; content could be large but kept transiently // Simple stable key; content could be large but kept transiently
return `${filePath}\u241F${oldContent}\u241F${newContent}`; return `${filePath}\u241F${oldContent}\u241F${newContent}`;