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

<think>好的,我现在需要帮助用户了解VSCode中的GitLens插件,包括使用指南、功能配置和常见问题。首先,我得回忆一下自己关于GitLens的知识,或者查看相关的参考资料。用户提供的引用中有提到引用[1]、[4]涉及到GitLens,特别是引用[4]提到用GitLens对比不同分支的代码,这可能是一个重要功能点。 首先,我应该从安装开始,因为使用任何插件的第一步都是安装。但用户可能已经安装了,所以可能需要跳过,但指南里还是应该包含。然后,基本功能需要涵盖,比如代码行注释、提交历史、分支比较等。引用[4]里提到在VS Code中使用GitLens对比不同分支代码,这部分需要详细说明步骤,比如如何通过GitLens查看分支差异。 接下来是功能配置部分,用户可能想知道如何调整设置,比如是否显示行注释,如何自定义时间格式,或者集成到代码审查流程中。这里可能需要参考引用[1]中的ESLint配置,但GitLens的配置可能类似,在设置里搜索GitLens进行调整。 常见问题部分,用户可能遇到的问题比如插件冲突、性能问题,或者无法显示Git信息。比如,如果Git仓库过大,GitLens可能会变慢,这时候可以建议排除某些目录。或者如果用户没有正确配置Git路径,需要检查设置。 另外,用户引用的资料里提到Git Flow的使用,比如从develop分支创建feature分支,进行代码审查后合并,这部分如何在GitLens中实现可能需要说明,比如通过分支比较功能进行Code Review。 需要确保回答结构清晰,分点说明,使用中文,并且正确应用LaTeX格式。例如,如果有数学表达式,需要用$...$,不过在这个问题中可能用不到太多数学公式,但如果有版本号之类的,可能需要用代码块或者正确格式。 可能还需要考虑用户的实际使用场景,比如开发新功能时如何利用GitLens跟踪变更,或者在团队协作中如何通过查看提交历史来理解代码改动。引用[4]中提到的代码审查流程,结合GitLens的功能,可以详细说明如何对比分支,查看提交差异。 最后,常见问题部分需要解决用户可能遇到的典型问题,比如插件无法正常工作,可能需要检查Git安装、仓库权限,或者重置插件设置。另外,性能优化建议也很重要,比如限制历史记录加载数量。 需要确保每个部分都有具体的步骤或配置方法,让用户能够逐步操作。同时,引用相关的参考资料,比如引用[4]在讲解分支对比时添加引用标识。注意不要遗漏用户提到的任何方面,并且保持回答的真实可靠,如果有不确定的地方,可能需要查阅更多资料,但根据提供的引用,应该足够覆盖主要内容。</think>### VSCode GitLens 插件使用指南 #### 一、基础功能与使用场景 1. **代码行级注释** 在代码编辑器中,GitLens 会直接在代码行末尾显示最后修改者、提交时间和提交信息。例如: ```javascript function calculate(a, b) { // 由张三于2023-10-05提交:优化算法性能 return a * b + Math.sqrt(a); } ``` 点击注释可直接查看完整提交详情,包括文件差异对比。 2. **提交历史追溯** - 右键文件选择 **「GitLens: Open File History」** 查看完整修改记录 - 使用快捷键 `Alt+G` 快速调出提交搜索面板 - 支持按作者、时间范围过滤提交记录[^4] 3. **分支对比(关键功能)** 按以下步骤对比 `feature/login` 与 `develop` 分支差异: ``` 1. 打开命令面板 (Ctrl+Shift+P) 2. 输入「GitLens: Compare References」 3. 选择目标分支 → 选择对比分支 ``` 系统会生成差异视图,支持逐行代码比对(如图示红绿色标记) #### 二、核心配置项 1. **基础设置** 通过 `Ctrl+,` 打开设置,搜索 `gitlens`: - `gitlens.currentLine.enabled`: 关闭/开启当前行注释 - `gitlens.hovers.currentLine.over`: 设置悬停显示内容(提交信息/代码差异) - `gitlens.dateFormat`: 自定义时间格式,如 `YYYY-MM-DD HH:mm` 2. **代码审查集成** 团队协作时可配置: ```json "gitlens.views.repositories.autoRefresh": true, "gitlens.keymap": "chorded", "gitlens.codeLens.recentChange.enabled": false // 关闭频繁刷新的CodeLens ``` #### 三、典型问题解决方案 1. **无法显示Git信息** - 检查VSCode是否识别Git仓库:底部状态栏应显示分支名称 - 验证Git路径:在设置中确认 `git.path` 指向正确可执行文件[^2] - 重置缓存:执行命令 **「GitLens: Reset GitLens Cache」** 2. **性能优化建议** - 添加 `.gitignore` 文件排除大型二进制文件 - 修改历史加载限制: ```json "gitlens.history.maxCommits": 200 ``` - 禁用实时标注: ```json "gitlens.gutterChanges.enabled": false ``` 3. **插件冲突排查** 如遇快捷键冲突: - 执行 **「GitLens: Show GitLens Keybindings」** 查看绑定关系 - 在 `keybindings.json` 中重新映射冲突快捷键
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值