VSCode 扩展:获取一个文件的新旧版本

VSCode Extension: get a files old and new versions

我正在尝试开发一个 VSCode 扩展,它需要当前打开的文件和之前 git revision/commit 中的相同文件。这与在 vs 代码中单击打开更改按钮相同。

我尝试使用 SCM 和 QuickDiffProvider,如此 sample-extension 中所示,但在尝试打开 vscode 中的旧文件时出现“无法解析资源”。


片段来自 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);
    }
}

调试控制台输出:

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: '', …}

我要找的,简而言之:
我想在左侧视图(旧)和右侧视图(新)中获取文件的文件内容,如 openChange 中所示,在我的扩展中。目的是编写自定义比较方法并将结果存储为 html 格式,而不是显示为并排比较的差异。

在努力寻找替代解决方案来获取文件差异之后,我发现我们可以使用 vscode.editors 来访问所有打开的编辑器。

示例: 在 git diff 中我们将有 2 个编辑器(左:旧的和右:新的),因此我们可以使用以下代码访问旧的和新内容。

if (editors.length === 2) {
    oldContent = editors[0].document.getText();
    newContent = editors[1].document.getText();
}

如果我们使用 git 镜头,那么我们也可以使用相同的代码获得旧提交的差异,这是一个额外的好处(至少对我的扩展而言)。

这种方式的唯一问题是,如果在 2 列中打开 2 个编辑器,它也可以工作(这实际上对我的扩展没问题)