如何在 Visual Studio 代码中正确实施 CodeLens

How to properly implement CodeLens in Visual Studio Code

我正在为 Visual Studio 代码开发扩展,需要在编辑器中显示 CodeLens。

实现它的最佳方法是什么?

我应该在 provideCodeLenses 中使用命令创建 CodeLens 还是在 resolveCodeLens 中更新它?

这取决于您的代码镜头计算的复杂程度。

  • 如果代码镜头非常简单,并且总是可以在固定时间内创建,则只需要实现provideCodeLens.

  • 如果创建代码镜头可能涉及密集计算或任何类型的非确定性行为——例如网络——那么provideCodeLens应该只return具有范围的骨架代码镜头.然后,您将在 resolveCodeLens 中完成 CodeLens 的 command,只有在实际需要显示 CodeLens 时才会调用它。

总体而言,将您的 CodeLens 实现拆分为 provideCodeLensesresolveCodeLens 是最安全的选择。