代码之家  ›  专栏  ›  技术社区  ›  BlueMonkMN

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

  •  0
  • BlueMonkMN  · 技术社区  · 5 年前

    我认为设置quickdiffprovider至少可以在ui中选择一个文件来显示diff时提供一个入口点。但我甚至不能让代码对我的自定义源代码管理提供程序的源代码管理面板中的文件进行单击。

    伸展运动

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

    存储库.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?

    0 回复  |  直到 5 年前
        1
  •  0
  •   BlueMonkMN    5 年前

    quickdiff不涉及源代码控制面板,而是在编辑器中显示任何源代码控制文件时应用。因此,在源代码管理视图中选择文件时,不会看到任何与diff相关的快速代码执行,而是在编辑器中激活其他文件时执行。QuickDiff信息显示为源代码左侧的彩色条,指示与源代码管理中的版本相比,哪些代码已更改:

    Source code with a bluish bar on the left

    相同的 provideOriginalResource 但是,使用QuasDIFF函数可以用于问题中提到的功能(单击源代码管理视图中的文件以显示差异)。首先,您需要定义一个可以引用的命令来激活该行为。 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接口,该接口允许将命令链接到每个命令。这是一个差异指令可以链接到每个项目的选择:

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