【问题标题】:VSCode Extension: get a files old and new versionsVSCode 扩展:获取文件的新旧版本
【发布时间】:2021-07-09 03:03:15
【问题描述】:

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

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


sn-p 来自 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 格式,而不是显示为 diff 与并排比较。

【问题讨论】:

    标签: typescript git visual-studio-code diff vscode-extensions


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 2015-03-01
      相关资源
      最近更新 更多