gpt4 book ai didi

How to force the update of the code lenses in vscode(如何在vscode中强制更新代码镜头)

转载 作者:bug小助手 更新时间:2023-10-26 20:26:51 28 4
gpt4 key购买 nike



I am working on "run code" actions using code lenses in Metals (Scala Language Server). In our use case, we can provide those code lenses only once the compilation finishes. If the user is not modifying the source file during or after the compilation, vscode won't send a request for code lenses. This can lead to an ugly state of "stale code lenses" from before the compilation.

我正在使用Metals(Scala Language Server)中的代码透镜进行“运行代码”操作。在我们的用例中,我们只能在编译完成后提供这些代码镜头。如果用户在编译期间或之后没有修改源文件,vscode将不会发送代码镜头请求。这可能会导致编译前的“陈旧代码镜头”的丑陋状态。



Digging into the internals of vscode-languageserver-node I have noticed that the CodeLensProvider can emit an onDidChangeCodeLenses event but it is not reflected in the LSP (https://github.com/microsoft/language-server-protocol/issues/192), hence I cannot send this event to vscode.

深入研究vscode语言服务器节点的内部结构,我注意到CodeLensProvider可以发出onDidChangeCodeLens事件,但它没有反映在LSP(https://github.com/microsoft/language-server-protocol/issues/192),中,因此我不能将该事件发送给vscode。



The other thing which would probably force reloading code lenses is the ICodeEditor::setModel method but I am not sure if this can be done directly from the vscode extension, since I cannot get a reference to the code editor.

另一件可能会强制重新加载代码镜头的事情是ICodeEditor::setModel方法,但我不确定这是否可以直接从vscode扩展中完成,因为我无法获得对代码编辑器的引用。



How can this be done from either the Language Server or vscode extension? Or is there another, preferred way?

如何从语言服务器或vscode扩展来实现这一点?还是有其他更受欢迎的方式?


更多回答
优秀答案推荐

Unfortunately, the language server protocol indeed doesn't support this yet. The corresponding feature request I opened a while ago can be found here.

不幸的是,语言服务器协议确实还不支持这一点。我前段时间打开的对应功能请求可以在这里找到。



I was able to work around this by implementing a dummy CodeLensProvider on the extension side for the same language ID (the VSCode API generally allows registering multiple providers for language features). It does nothing but call the onDidChangeCodeLenses event when necessary, the actual implementation is still on the language server side.

我能够通过在扩展端为相同的语言ID实现一个虚拟的CodeLensProvider来解决这个问题(VSCode API通常允许为语言功能注册多个提供程序)。它只在必要时调用onDidChangeCodeLens事件,实际实现仍在语言服务器端。



Two years later, it looks like there is still no implementation.

两年后,看起来仍然没有实施。


I managed to work around this by disposing of the codelens provider and immediately registering it again in my command (which is run by Codelens).

我设法解决了这个问题,方法是处理Codelens提供程序,并立即在我的命令(由Codelens运行)中再次注册它。


// NOTE: this is a hack to force the codelens to update
codelensDisposable.dispose();
codelensDisposable = vscode.languages.registerCodeLensProvider('*', codelensProvider);

EDIT: I just found out this appears to break GitLens extension, Codelens actions are missing when my extension is running as well...

编辑:我刚刚发现这似乎破坏了GitLens扩展,当我的扩展运行时,Codelens操作也丢失了…



LSP 3.16 introduced a new request: workspace/codeLens/refresh and a new capability:

LSP 3.16引入了新的请求:工作空间/代码镜头/刷新和新功能:


export interface CodeLensWorkspaceClientCapabilities {
/**
* Whether the client implementation supports a refresh request send from the server
* to the client. This is useful if a server detects a change which requires a
* re-calculation of all code lenses.
*/
refreshSupport?: boolean;
}

You can read the whole patch and full documentation here: https://github.com/microsoft/language-server-protocol/commit/558f1e114a3dc53da7c3686a657ebef070275d63

您可以在此处阅读整个补丁程序和完整文档:https://github.com/microsoft/language-server-protocol/commit/558f1e114a3dc53da7c3686a657ebef070275d63


I am able to request a refresh in a pygls-based language server using:

我可以使用以下命令在基于PYGLS的语言服务器中请求更新:


await ls.lsp.send_request_async(WORKSPACE_CODE_LENS_REFRESH)

In VSCode I have found that code lens refresh requests are not processed immediately. My experiments indicate that there needs to be a pause of ~400-500ms with no document edits before the code lens is updated.

在VSCode中,我发现代码镜头刷新请求不会立即处理。我的实验表明,在更新代码镜头之前,需要有大约400-500ms的停顿,没有文档编辑。


更多回答

Support for workspace/codeLens/refresh was added in LSP 3.16, see my answer.

LSP 3.16中添加了对工作空间/代码镜头/刷新的支持,请参阅我的答案。

Did you find a new solution to this?

你找到解决这个问题的新办法了吗?

@Sincostan unfortunately, I didn't finish my extension, so no. :/

@辛科斯坦不幸的是,我没有完成我的分机,所以没有。:/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com