My current project involves creating a VSCode extension that needs to access the current open file and the same file from the previous git revision/commit. This is essentially what happens when you click the open changes button in vscode.
https://i.stack.imgur.com/KyGIP.png
I have attempted to utilize SCM and QuickDiffProvider, following the example provided in this sample extension. However, I am encountering an issue where it gives an error message "unable to resolve resource" when trying to open the old file in vscode.
https://i.stack.imgur.com/JpgJd.png
A snippet from extension.ts:
let folder: string = vscode.env.appRoot;
let scm: vscode.SourceControl | undefined;
if (vscode.workspace.workspaceFolders) {
let rootUri = vscode.workspace.workspaceFolders[0].uri;
scm = vscode.scm.createSourceControl("MyDiff", "MyDiff", rootUri);
folder = rootUri.fsPath;
var repo = new Repository(vscode.workspace.workspaceFolders[0]);
scm.quickDiffProvider = repo;
let changedResources = scm.createResourceGroup("workingTree", "Changes");
// repo.getResourceStates().then((result) => {
// changedResources.resourceStates = result;
// });
// context.subscriptions.push(changedResources);
var currentlyOpenTabfileUri = vscode.window.activeTextEditor?.document.uri;
if(currentlyOpenTabfileUri){
if(repo.provideOriginalResource){
const repositoryUri = repo.provideOriginalResource(currentlyOpenTabfileUri, null);
console.log(repositoryUri);
console.log(currentlyOpenTabfileUri);
try{
vscode.commands.executeCommand('vscode.open', currentlyOpenTabfileUri);
vscode.commands.executeCommand('vscode.open', repositoryUri);
vscode.commands.executeCommand('vscode.diff', repositoryUri, currentlyOpenTabfileUri, `Old - New`);
}
catch(err){
console.error(err);
}
}
}
}
Repository.ts:
export const JSFIDDLE_SCHEME = 'MyDiff';
import { QuickDiffProvider, Uri, CancellationToken, ProviderResult, WorkspaceFolder, workspace, window, env } from "vscode";
import * as path from 'path';
export class Repository implements QuickDiffProvider {
constructor(private workspaceFolder: WorkspaceFolder) { }
provideOriginalResource?(uri: Uri, token: CancellationToken|null): ProviderResult<Uri> {
// converts the local file uri to jsfiddle:file.ext
const relativePath = workspace.asRelativePath(uri.fsPath);
return Uri.parse(`${JSFIDDLE_SCHEME}:${relativePath}`);
}
/**
* Enumerates the resources under source control.
*/
provideSourceControlledResources(): Uri[] {
return [
Uri.file(this.createLocalResourcePath('json'))
];
}
/**
* Creates a local file path in the local workspace that corresponds to the part of the
* fiddle denoted by the given extension.
*
* @param extension fiddle part, which is also used as a file extension
* @returns path of the locally cloned fiddle resource ending with the given extension
*/
createLocalResourcePath(extension: string) {
return path.join(this.workspaceFolder.uri.fsPath, extension);
}
}
Debug Console Output:
Congratulations, your extension "vscode-test-diff" is now active!
h {scheme: 'MyDiff', authority: '', path: 'test', query: '', fragment: '', …}
h {scheme: 'file', authority: '', path: '/c:/dummy/test', query: '', fragment: '', …}
In essence, my goal is:
I aim to retrieve the file contents of the file in the left side view (old) and right side view (new), similar to how openChange functions in vscode. The objective is to implement a custom comparison method and store the result in HTML format instead of displaying it as a side-by-side comparison.