显示与VS Code源代码控制扩展的差异

问题描述 投票:0回答:1

我认为设置quickDiffProvider至少会为在UI中选择文件以显示差异提供一个入口点。但我甚至无法让代码对我的自定义源代码控制提供程序的源代码管理面板中的文件单击作出反应。

extension.ts

export function activate(context: vscode.ExtensionContext) {

    // Use the console to output diagnostic information (console.log) and errors (console.error)
    // This line of code will only be executed once when your extension is activated
    console.log('Extension "AccuRev" is now active.');
    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("accurev", "AccuRev", rootUri);
        folder = rootUri.fsPath;
    }

    const repo = new AccuRevRepo(getOutputChannel(), folder);
    if (scm) {
        scm.quickDiffProvider = repo;
        let modified = scm.createResourceGroup("modified", "Modified");
        repo.getResourceStates().then((result) => {
            modified.resourceStates = result;
        });
        context.subscriptions.push(modified);
    }

    // The command has been defined in the package.json file
    // Now provide the implementation of the command with registerCommand
    // The commandId parameter must match the command field in package.json
    let disposable = vscode.commands.registerCommand('accurev.refresh', () => {
        // The code you place here will be executed every time your command is executed

        // Display a message box to the user
        getOutputChannel().appendLine('Hello World!');
        repo.getPending();
    });

repository.ts

export class AccuRevRepo {
[...]
    public provideOriginalResource?(uri: vscode.Uri, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Uri> {
        return this.provideOriginalResourceAsync(uri);
    }

    public async provideOriginalResourceAsync(uri: vscode.Uri): Promise<vscode.Uri | null> {
        let originalText = await this.execute(`cat -v ${this.basisName} \"${uri.fsPath}\"`);
        let tempExists = await new Promise<boolean>((resolve) => {
[...]

我在源代码控制视图中获得了正确的文件列表,但是当我点击一个时,没有任何事情发生。我希望能够在provideOriginalResource中放置一个断点并停在那里,但没有任何反应。如何实现显示与最新签入文件的差异的能力 - 我在哪里挂钩API?

typescript visual-studio-code vscode-extensions
1个回答
1
投票

QuickDiff不涉及源控制面板,而是在编辑器中显示任何源控制文件时应用。因此,在源控件视图中选择文件时,您将看不到任何快速差异相关代码,而是在编辑器中激活不同文件时。 QuickDiff信息在源代码左侧显示为彩色条,表示已更改的代码与源代码管理中的版本相比:

Source code with a bluish bar on the left

但是,与QuickDiff一起使用的provideOriginalResource函数可以用于问题中提到的功能(单击源控件视图中的文件以显示差异)。首先,您需要在package.json块的contributes commands部分中定义一个可以引用的命令来激活此行为:

{
    "command": "accurev.openDiffBasis",
    "category": "AccuRev",
    "title": "Open diff with basis",
    "icon": {
        "dark": "icons/dark/undo2.svg",
        "light": "icons/light/undo2.svg"
    }
},

然后你需要注册命令,通常从extension.ts完成,代码如下:

let diff = vscode.commands.registerCommand('accurev.openDiffBasis', async (file: vscode.Uri) => {
    try {
        let original = await repo.provideOriginalResource(file);
        if (original !== null) {
            let filename = vscode.workspace.asRelativePath(file);
            vscode.commands.executeCommand('vscode.diff', original, file,  `${repo.basisName}\\${filename} ↔ ${filename}`);
        }
    }
    catch(err) {
        getOutputChannel().appendLine(err);
    }
});

请注意,此处使用了provideOriginalResource,与QuickDiff隐式调用的函数相同。另请注意,调用vscode.diff命令实际上是diff查看器,并且可以响应任何操作来完成 - 这不仅仅是一个隐含的反应。

最后,getResourceStates返回的项需要实现SourceControlResourceState接口,该接口允许将命令链接到每个接口。这是diff命令可以链接到每个项目的选择:

export class AccuRevFile implements vscode.SourceControlResourceState {
    readonly resourceUri: vscode.Uri;
    readonly command?: vscode.Command | undefined;
    readonly decorations?: vscode.SourceControlResourceDecorations | undefined;
    public readonly elementId: number;

    constructor(uri: vscode.Uri, elementId: number, state: AccuRevState) {
        this.resourceUri = uri;
        this.decorations = state;
        this.command = { title: "diff", command: "accurev.openDiffBasis", tooltip: "Diff against basis version", arguments: [uri]};
        this.elementId = elementId;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.