This commit adds support for https://microsoft.github.io/language-server-protocol/specification#textDocument_codeLens.
See also https://github.com/clangd/clangd/issues/442 and https://github.com/lightmelodies/vscode-clangd.
CodeLens is a popular feature in Visual Studio Code, which provides actionable contextual information interspersed when opening a file:
- Contextual: codelens is displayed as a decoration and contains information of nearby code, e.g. show how many usages of a function without invoking findReferences manually.
- Actionable: codelens has an associate command, so you can click it and tiggers some action.
Here is a simple gif to show how it works.
Implementation details:
- getDocumentCodeLens: This function implements the textDocument/codeLens API.
- use documentSymbol to determine where codelens can occur.
- For each symbol, add a reference codelens which displays the count of references. To avoid unnecessary lookup, only resolve data will be returned to the client in this step. The actual command will be filled by codeLens/resolve when the symbol getting into the viewport.
- For class and virtual methods, use getTypeHierarchy and getMemberHierarchy to get hierarchy codelens which displays the count of base and derived. We resolve the command directly since the overhead is little in general and '0 derived' seems to be weird and useless.
- resolveCodeLens: This function implements the codeLens/resolve API. Just use findReferences to get the count of references according to the resolve data.
- onCodeLensCommand: This function extends the onCommand function to support codeLens command. It just returns the location of related symbols. VSCode use editor.action.showReferences to show them, so the whole action will work like invoking Peek References.