显示与VS Code源代码管理扩展的区别

时间:2019-03-01 20:56:32

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

我认为,设置quickDiffProvider至少会为在UI中选择文件以显示差异提供一个入口点。但是我什至无法获得代码来响应自定义源代码控制提供程序中“源代码控制”面板中的文件单击。

extension.ts

external-parameter-entities

repository.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();
    });

我在源代码管理视图中得到了正确的文件列表,但是当我单击一个时,什么也没有发生。我希望能够在ProvideOriginalResource中设置一个断点并在此停下来,但是什么也没有发生。如何实现与最新签入文件显示差异的功能-我应该在哪里挂接API?

1 个答案:

答案 0 :(得分:0)

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;
    }
}