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

相关文章
|
iOS开发 MacOS
还在为 iTerm 多窗口操作烦恼?tmux 这款神器轻松帮你解决(下)
粉在之前文章中教过大家如何结合 zsh 让 iterm2 发挥最佳效果。 什么还没有看过?赶紧看下补一下前提知识:收集了这么多实用技巧,帮助你的 iterm2 成为最帅的那个! 上篇文中,阿粉提到每次上线发布的时候,都会打开很多 iTerm 窗口,通过 tab 页拖拽方式让所有窗口可以同时显示。
2209 0
还在为 iTerm 多窗口操作烦恼?tmux 这款神器轻松帮你解决(下)
|
人工智能 并行计算 机器人
未来人工智能如何重构”时间“?
时间是数学还是幻觉?从熵增到几何,从人类意识到AI智能,本文探讨时间的本质。线性、循环与拓扑模型揭示其多维可能;热力学箭头与认知局限引发哲学思辨;而AI的并行预测与信息压缩,或将重构时间本身。未来智能或不再线性行走,而是编织多维时间之网,重塑我们对存在的理解。(238字)
814 0
|
SQL 自然语言处理 数据挖掘
当销售额下降时,ChatBI 如何实现多维下钻、归因分析?
ChatBI 的归因分析能力,能够通过自动化拆解指标、关联业务维度、量化因子贡献度,将“数据表象”转化为“可解释的决策依据”。
|
人工智能 运维 前端开发
【CodeBuddy】三分钟开发一个实用小功能之:爆炸式模态框弹出
本文介绍了如何借助CodeBuddy实现一个点击按钮后带有粒子爆炸效果的模态框功能。通过自然语言描述需求,CodeBuddy不仅快速生成代码框架,还主动优化细节,如粒子随机颜色、运动轨迹、模态框关闭动画等。整个过程无需手动编写代码,AI根据需求逐步完善交互效果,最终实现流畅自然的粒子爆炸与模态框弹出动画。附带完整HTML、CSS和JS代码,适合新手学习和开发者参考。
540 3
【CodeBuddy】三分钟开发一个实用小功能之:爆炸式模态框弹出
|
搜索推荐 Android开发 UED
信息检索系统评估指标的层级分析:从单点精确度到整体性能度量
本文深入探讨了信息检索系统(如搜索引擎)的评估机制,从用户行为特征出发,设计了一系列量化指标以衡量搜索结果的相关性和有效性。核心内容包括精确度(Precision)、Precision@K(聚焦前K个结果)、Average Precision@K(考虑位置权重)以及MAP@K(系统整体性能评估)。通过实际案例分析,展示了如何用这些指标评估搜索系统的质量,并强调高质量系统需在多维度上表现优异,以契合用户真实需求和行为模式。文章为优化信息检索系统提供了科学指导框架。
967 7
信息检索系统评估指标的层级分析:从单点精确度到整体性能度量
|
机器学习/深度学习 安全 数据安全/隐私保护
Jupyter Notebook本地部署并实现公网远程访问内网Jupyter服务器【内网穿透】
Jupyter Notebook本地部署并实现公网远程访问内网Jupyter服务器【内网穿透】
2560 0
|
数据采集 数据可视化 大数据
揭秘Dataphin:一站式企业级数据治理平台的全景体验!
Dataphin是阿里巴巴推出的全生命周期数据治理平台,基于OneData方法论,覆盖数据采集、管理到消费的全流程。它支持多种计算平台,提供灵活可扩展的能力,助力企业构建高效的数据中台。其强大的自动化与可视化功能,显著提升数据治理效率和质量,适用于大规模数据处理场景。未来可加强AI预测能力,为企业带来更多智能决策支持。
772 2
|
人工智能 测试技术 Apache
SmolVLM:Hugging Face推出的轻量级视觉语言模型
SmolVLM是Hugging Face推出的轻量级视觉语言模型,专为设备端推理设计。以20亿参数量,实现了高效内存占用和快速处理速度。SmolVLM提供了三个版本以满足不同需求,并完全开源,所有模型检查点、VLM数据集、训练配方和工具均在Apache 2.0许可证下发布。
2105 7
SmolVLM:Hugging Face推出的轻量级视觉语言模型
|
算法 C++ 开发者
【QML文件结构】理解QML中 多层嵌套控件之间的关系
【QML文件结构】理解QML中 多层嵌套控件之间的关系
1485 2