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有许多开源项目,可以在上面找相关项目参考。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vscode-gitlens 是一个非常实用的 Git 工具插件,它可以帮助我们更好地管理 Git 仓库。使用 vscode-gitlens 插件,我们可以方便地查看 Git 历史记录、分支、标签等信息,还可以比较不同版本之间的差异,甚至可以直接在编辑器中进行代码提交和推送等操作。 下面是 vscode-gitlens 插件的使用方法: 1. 安装插件:在 VS Code 中搜索 gitlens 并安装。 2. 打开 GitLens 面板:在 VS Code 左侧边栏中点击 GitLens 图标,或者使用快捷键 Ctrl+Shift+P 并输入 GitLens: Toggle GitLens。 3. 查看 Git 历史记录:在 GitLens 面板中选择 History 选项卡,即可查看当前文件的 Git 历史记录。可以通过单击某个提交记录来查看该提交的详细信息,包括作者、提交时间、提交信息等。 4. 查看分支和标签:在 GitLens 面板中选择 Branches 或 Tags 选项卡,即可查看当前仓库的分支或标签列表。可以通过单击某个分支或标签来查看该分支或标签的详细信息。 5. 比较不同版本之间的差异:在 GitLens 面板中选择 Compare 选项卡,选择要比较的两个版本,即可查看它们之间的差异。可以通过单击某个文件来查看该文件的详细差异信息。 6. 提交和推送代码:在 GitLens 面板中选择当前文件的提交记录,然后单击右侧的 + 按钮,即可打开提交面板。在提交面板中输入提交信息,然后单击提交按钮即可提交代码。如果需要推送代码到远程仓库,可以在提交面板中选择 Push 选项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值