【问题标题】:How to provide arguments to resolveCodeLens in Visual Studio Code?如何在 Visual Studio Code 中为 resolveCodeLens 提供参数?
【发布时间】:2020-09-29 12:24:20
【问题描述】:

根据 Visual Studio Code 扩展 (https://github.com/microsoft/vscode-extension-samples/tree/master/codelens-sample) 的示例,可以解析代码镜头:

public resolveCodeLens(codeLens: vscode.CodeLens, token: vscode.CancellationToken) {
        if (vscode.workspace.getConfiguration("codelens-sample").get("enableCodeLens", true)) {
            codeLens.command = {
                title: "Codelens provided by sample extension",
                tooltip: "Tooltip provided by sample extension",
                command: "codelens-sample.codelensAction",
                arguments: ["Argument 1", false]
            };
            return codeLens;
        }
        return null;
    }

代码镜头显示在编辑器窗口中的一行文本旁边。

但它不显示任何依赖于该行的信息。

我想传递要在 codeLens 命令标题中显示的文本行。创建多个自定义消息。

如何向 codeLens 显示提供文本“next”以解析CodeLens?

【问题讨论】:

    标签: visual-studio-code


    【解决方案1】:

    我同样需要显示行上下文信息。检查 CodeLens 的构造函数,它接受第二个参数中的命令对象。

    Codelens constructor

    这里有一个基于我的实现的 sn-p 来解决这个问题:

        provideCodeLenses(document, token) {
            this.codeLenses = [];
            const regex = new RegExp("^[156789].*$");
            let lines = document.getText().split("\n");
            lines.forEach((lineText, lineNumber) => {
                if (regex.test(lineText)) {
                    const position = new vscode.Position(lineNumber, 0);
                    const range = new vscode.Range(position, position);
                    let command = {
                       command = "",
                       title = "<your custom title based on lineText>"
                    };
                    this.codeLenses.push(new vscode.CodeLens(range, command));
                }
            });
            return this.codeLenses;
        }
    

    【讨论】:

    • 但是,根据官方示例 (github.com/microsoft/vscode-extension-samples/tree/main/…) 和文档,出于性能原因,代码镜头的创建和解析应该分两个阶段进行,但由于 resolveCodeLens 似乎只处理一个阶段codelens,如果我有多个 codelens,我会感到困惑。
    猜你喜欢
    • 2021-03-18
    • 2019-05-12
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多