vscode的code_lens相关

简介: vscode的code_lens相关
https://code.visualstudio.com/blogs/2017/02/12/code-lens-roundup
https://marketplace.visualstudio.com/items?itemName=abierbaum.vscode-file-peek
https://github.com/xxMUROxx/vscode.code_call_lens/blob/master/src/extension.ts
https://code.visualstudio.com/docs/extensionAPI/language-support#_codelens-show-actionable-context-information-within-source-code
https://marketplace.visualstudio.com/items?itemName=waderyan.code-lens-roundup
https://mp.csdn.net/mdeditor#
https://pub.dartlang.org/documentation/vscode/latest/vscode/CodeLens-class.html
https://www.google.com.sg/search?rlz=1C1GCEU_zh-CNMY820MY820&biw=1600&bih=709&ei=mEfYW7jWD6i2ggeDl7_YDw&q=codeLens.method&oq=codeLens.method&gs_l=psy-ab.3...84573.84573.0.85474.1.1.0.0.0.0.353.353.3-1.1.0....0...1c.1.64.psy-ab..0.0.0....0.FSpszPXfY_w
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206863215-Implement-CodeLens-in-the-code-editor
https://github.com/Microsoft/vscode/commit/770ede158afea0293f4c66448e0effb8d2788a4b

It depends on how complex your code lenses are to compute.

If the code lenses are very simple and can always be created in a fixed amount of time, you only need to implement provideCodeLens.

If creating the code lenses may involve intensive computation or any sort of non-deterministic behavior – such as network – then provideCodeLens should only return skeleton Code Lenses with ranges. You would then complete the CodeLens’ command in resolveCodeLens, which will only be called when the CodeLens actually needs to be displayed.

Overall, splitting your CodeLens implementation between provideCodeLenses and resolveCodeLens is the safest choice.

======================================================================

CodeLens

A code lens represents a command that should be shown along with source text, like the number of references, a way to run tests, etc.

A code lens is unresolved when no command is associated to it. For performance reasons the creation of a code lens and resolving should be done to two stages:“CodeLensProvider.provideCodeLenses” and “CodeLensProvider.resolveCodeLens”

参数说明:

command ↔ Command

The command this code lens represents.

isResolved ↔ bool
  true when there is a command associated. @readonly
  range ↔ Range
  The range in which this code lens is valid. Should only span a single line.

CodeLensProvider:

A code lens provider adds commands to source text. The commands will be shown as dedicated horizontal lines in between the source text.

一个扩展codelens的例子:

// The module 'vscode' contains the VS Code extensibility API
import * as vscode from 'vscode'; 
export function activate(context: vscode.ExtensionContext) {
    console.log('Congratulations, your extension "my-extension2" is now active!'); 
    var disposable1 = vscode.commands.registerCommand('extension.sayHello', () => {
        console.log("searchData");
    });
    // var s1 = vscode.languages.registerCodeLensProvider({scheme: 'file', language: 'csharp'}, {
    //     provideCodeLenses: (doc, ct) => {
    //         console.log('provideCodeLenses1'); 
    //         var codeLenses: vscode.CodeLens[] = [];
    //         codeLenses.push( {
    //             range: doc.lineAt(0).range,
    //             // command: '',
    //             isResolved: true
    //         });
    //         return codeLenses;
    //     },
    //     resolveCodeLens: (codeLens: vscode.CodeLens, token: vscode.CancellationToken) => {
    //         return codeLens;
    //     }
    // });
    var s1 =  vscode.languages.registerCodeLensProvider({scheme: 'file', language: 'csharp'}, {
        provideCodeLenses(document, token) {
            const result: vscode.CodeLens[] = [];
            let idx = -1;
            let count = 0;
            while ((idx = document.getText().indexOf('abc', idx + 1)) >= 0) {
                count ++ ;
            }
            while ((idx = document.getText().indexOf('abc', idx + 1)) >= 0) {
                const pos = document.positionAt(idx);
                const range = document.getWordRangeAtPosition(pos);
                const titleInfo = (count === 1 ? `${count} reference` : `${count} references`);
                result.push(new vscode.CodeLens(range, { title: titleInfo, command: 'extension.sayHello', arguments: ["123", "456", "789"]}));
            }
            return result;
        },
        resolveCodeLens: (codeLens: vscode.CodeLens, token: vscode.CancellationToken) => {
            return codeLens;
        },
    });
    var s2 =  vscode.languages.registerCodeLensProvider({ pattern: '**/*.abc' }, {
        provideCodeLenses(document, token) {
            const result: vscode.CodeLens[] = [];
            let idx = -1;
            let count = 0;
            while ((idx = document.getText().indexOf('abc', idx + 1)) >= 0) {
                count ++ ;
            }
            while ((idx = document.getText().indexOf('abc', idx + 1)) >= 0) {
                const pos = document.positionAt(idx);
                const range = document.getWordRangeAtPosition(pos);
                const titleInfo = (count === 1 ? `${count} reference` : `${count} references`);
                result.push(new vscode.CodeLens(range, { title: titleInfo, command: 'extension.sayHello', arguments: ["abc abc abc"] }));
            }
            return result;
        },
        resolveCodeLens: (codeLens: vscode.CodeLens, token: vscode.CancellationToken) => {
            codeLens.command.command = 'extension.sayHello';
            codeLens.command.arguments = ["aaa bbb ccc"];
            return codeLens;
        }
    });
    context.subscriptions.push(s1);
    context.subscriptions.push(s2);
    context.subscriptions.push(disposable1);
}

显示效果:

github有许多开源项目,可以在上面找相关项目参考。

相关文章
Visual Studio Code开发常用的工具栏选项,查看源码技巧以及【vscode常用的快捷键】
Visual Studio Code开发常用的工具栏选项,查看源码技巧以及【vscode常用的快捷键】
573 0
|
7月前
|
IDE API 开发工具
在 VSCode 中实现 Jupyter Debug Adapter
通过本文的介绍,我们可以了解 VSCode 的 Debug Adapter 的实现原理,并以 Jupyter 为例,成功在 VSCode 中实现 Jupyter Debug Adapter,并实现完全的调试能力。 得益于 VSCode 灵活的调试注入能力,我们可以方便地为更多的语言,甚至是一些自定义框架实现调试能力,这可能可以为更多的小众语言或框架的开发者带来工作效率上的帮助。
|
8月前
|
JSON JavaScript 前端开发
【vscode】 VsCode 代码片段
【vscode】 VsCode 代码片段
67 0
|
8月前
|
Go
VsCode(Visual Studio Code) 安装插件教程
VsCode(Visual Studio Code) 安装插件教程
230 0
|
8月前
|
Linux Windows
VsCode(Visual Studio Code) 安装教程
VsCode(Visual Studio Code) 安装教程
137 0
vscode code-runner运行没效果
vscode code-runner运行没效果
179 0
|
Android开发
[√]addr2line vscode插件
[√]addr2line vscode插件
98 0
|
Web App开发 JavaScript 前端开发
Visual Studio Code 常见的配置、常用好用插件以及【vsCode 开发相应项目推荐安装的插件】
Visual Studio Code 常见的配置、常用好用插件以及【vsCode 开发相应项目推荐安装的插件】
361 0
|
存储 JSON Linux
Visual Studio Code 插件安装
Visual Studio Code 插件安装
Visual Studio Code 插件安装
|
前端开发
VSCODE快速生成代码tips
VSCODE快速生成代码tips