The Wayback Machine - https://web.archive.org/web/20210105163230/https://github.com/neovim/neovim/pull/13165
Skip to content
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

[WIP] lsp: Add codelens support #13165

Draft
wants to merge 2 commits into
base: master
from
Draft

Conversation

@mfussenegger
Copy link
Contributor

@mfussenegger mfussenegger commented Oct 26, 2020

Adds some basic support for codelens.

See codeLens specification

Opening this to get some feedback about the UI for them.

vim-lsp implemented them similar to code actions - the user gets prompted for which codelens to run.
See https://github.com/prabirshrestha/vim-lsp/pull/831/files

I think that may be a bit hard to use, because many code lens have the same title, so it will be hard for users to choose the right one.

LanguageClient-neovim implements them using virtualtext. I followed the same approach for now.

Example:

codelens

TODO:

  • Decide how to display them / on the UI → virtual text
  • Trigger codelens retrieval automatically ? (on document update with debounce?) Or should users set up autocmds?
  • Support Code Lens resolve request
  • Tests

Example usage:

api.nvim_command [[autocmd CursorHold,CursorHoldI,InsertLeave <buffer> lua vim.lsp.codelens.refresh()]]
api.nvim_buf_set_keymap(bufnr, "n", "<leader>l", "<Cmd>lua vim.lsp.codelens.run()<CR>", {silent = true;})
@marvim marvim added the WIP label Oct 26, 2020
@teto
Copy link
Member

@teto teto commented Oct 26, 2020

in one of your previous gif, you showed some haskell code: is this motivated by haskell-language-server ? (I was not sure wether it does this via a codeAction or codeLens)

@mfussenegger
Copy link
Contributor Author

@mfussenegger mfussenegger commented Oct 26, 2020

in one of your previous gif, you showed some haskell code: is this motivated by haskell-language-server ? (I was not sure wether it does this via a codeAction or codeLens)

Yes it's motivated by haskell-language-server for the evaluate feature https://github.com/haskell/haskell-language-server#features

There's also an issue in the repo to support it via code action, but currently it only works with codelens

@fsouza
Copy link
Contributor

@fsouza fsouza commented Oct 26, 2020

ocaml-lsp uses codelens for type information, which I find very useful too. I haven't been doing a lot of ocaml lately, but I really appreciate this being added to neovim! 😬

@jan-xyz
Copy link

@jan-xyz jan-xyz commented Nov 1, 2020

gopls (go language server) also supports a bunch of code lenses which are quite helpful: https://github.com/golang/tools/blob/master/gopls/doc/settings.md#code-lenses

@mfussenegger mfussenegger force-pushed the mfussenegger:codelens branch 2 times, most recently from e1731f1 to 9cd3a3a Dec 6, 2020
@mfussenegger
Copy link
Contributor Author

@mfussenegger mfussenegger commented Dec 6, 2020

Pushed a couple of changes. It now is in its own module similar to diagnostic. Not sure if it is warranted but felt better than to spread it out among handlers, buf and utils.

Should be functional now.
Will use this for a while in it's current state to see how it's working. I'm a bit concerned that the refresh may put too much load on the language servers.

@jubnzv
Copy link
Contributor

@jubnzv jubnzv commented Dec 6, 2020

It would be very convenient to have this API out of the box.

At the moment, codeLens information may be obtained using lsp.buf_request_sync(). Here is my plugin, that uses this approach to add type annotations to functions in virtual text: https://github.com/jubnzv/virtual-types.nvim/. It works well and the result looks like this:

image

local chunks = { {lens.command.title, 'LspCodeLens'} }
api.nvim_buf_set_virtual_text(bufnr, ns, line, chunks, {})
else
-- todo: Should there be a different logic to decide whether to resolve the codelens?

This comment has been minimized.

@fsouza

fsouza Dec 6, 2020
Contributor

I've implemented codelenses in my vimfiles and there I resolve just before executing (would be on M.run here). It makes execution slower, but afaik I don't use any language server that supports codeLens/resolve, so not sure which approach is better or how to measure it :(

This comment has been minimized.

@mfussenegger

mfussenegger Dec 7, 2020
Author Contributor

I did that in a first version as well, but it doesn't really work for unresolved lenses.

If the lens isn't resolved already there isn't anything sensible that could be displayed to the user.
For example, language servers like eclipse.jdt.ls provide the option to enable reference counts via code lens.

The response just contains lens like this:

data = { "file:///path/to/file.java", {
        character = 13,
        line = 145
      }, "references" },
    range = {
      end = {
        character = 20,
        line = 145
      },
      start = {
        character = 13,
        line = 145
      }

The data part is arbitrary.

Once resolved it receives the command part in addition:

{
  command = {
    arguments = { "file:///path/to/file.java", {
        character = 16,
        line = 557
      }, { {
          range = {
            end = {
              character = 64,
              line = 55
            },
            start = {
              character = 23,
              line = 55
            }
          },
          uri = "file:///path/to/file.java"
        } } },
    command = "java.show.references",
    title = "1 reference"
  }
}

image

@mfussenegger mfussenegger force-pushed the mfussenegger:codelens branch from 9cd3a3a to 81f016b Jan 1, 2021
@mfussenegger mfussenegger force-pushed the mfussenegger:codelens branch from 81f016b to 5468711 Jan 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants
You can’t perform that action at this time.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.