Skip to content

feat(ts_ls): implement code lens support #3938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

sybereal
Copy link
Contributor

@sybereal sybereal commented Jul 5, 2025

Implement the editor.action.showReferences client command, which is used by the references and implementation code lenses that TypeScript Language Server provides.1
I've mostly based the implementation on the existing code for vim.lsp.buf.references(). The main visible difference is that the latter includes the item being referenced at the top of the list. Although, if desired, this could be emulated by manually inserting the position passed along in the command arguments at the top of the list.

Footnotes

  1. https://github.com/typescript-language-server/typescript-language-server?tab=readme-ov-file#code-lenses-textdocumentcodelens

Implement the `editor.action.showReferences` client command, which is
used by the references and implementation code lenses that TypeScript
Language Server provides.
I've mostly based the implementation on the existing code for
`vim.lsp.buf.references()`. The main visible difference is that the
latter includes the item being referenced at the top of the list.
Although, if desired, this could be emulated by manually inserting the
position passed along in the command arguments at the top of the list.
@sybereal sybereal force-pushed the feat/ts_ls/support-references-codelens branch from e83ea95 to 409cf4c Compare July 5, 2025 12:43
@justinmk
Copy link
Member

justinmk commented Jul 6, 2025

Looks like the right approach. How does one use this? Does the default gra mapping (vim.lsp.buf.code_action()) include extra items after this? I'm guessing not, but it probably should. (Not a blocker for this PR, of course.)

@sybereal
Copy link
Contributor Author

sybereal commented Jul 6, 2025

This does not interact with code actions, but rather code lenses.

The TypeScript language server provides code lenses that look like "N references M implementations". Triggering these via vim.lsp.codelens.run() invokes a LSP command named editor.action.showReferences, as described in the link. This is a command that has to be implemented on the LSP client side, which is what this PR does.

Compared to vim.lsp.buf.references(), it avoids a server roundtrip, as the reference data is already part of the code lens object.

@justinmk
Copy link
Member

justinmk commented Jul 6, 2025

This does not interact with code actions, but rather code lenses.

Yes, but it might make sense to include codelenses in the "code actions" menu.

@justinmk justinmk merged commit 7fac902 into neovim:master Jul 6, 2025
6 checks passed
@sybereal
Copy link
Contributor Author

sybereal commented Jul 6, 2025

Is that how code lenses usually handled? I'm sorry, I wasn't aware. I just have a mapping for vim.lsp.codelens.run() and an autocmd to regularly refresh them, like the help suggests.

What would that action item do then? Would it just be an alias to vim.lsp.codelens.run() as well?

@justinmk
Copy link
Member

justinmk commented Jul 6, 2025

I just have a mapping for vim.lsp.codelens.run() and an autocmd to regularly refresh them, like the help suggests.

Yeah that's not very discoverable, I doubt most users are aware of that :)

What would that action item do then? Would it just be an alias to vim.lsp.codelens.run() as well?

I assume it would run the codelens that the user chooses from the gra menu. neovim/neovim#34813

@sybereal
Copy link
Contributor Author

sybereal commented Jul 6, 2025

Yeah that's not very discoverable, I doubt most users are aware of that :)

Very good point. I did create a mapping for vim.lsp.codelens.run() when I first switched to native LSP, because I already had it previously, but it took me until recently to dig through the docs and finally figure out that I also need an autocmd running vim.lsp.codelens.refresh() to make code lenses actually show up.

I assume it would run the codelens that the user chooses from the gra menu.

That makes sense. Would be nice to have, yeah.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants