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

Feature Request: Thread multiple clients as different providers #74

Open
paulodiovani opened this issue Aug 23, 2024 · 0 comments
Open

Comments

@paulodiovani
Copy link

paulodiovani commented Aug 23, 2024

If we have multiple providers with hover capability for the same buffer, the hover will ramdonly display one of them (same with vim.lsp.hover).

I found a way to work around this by implementing a custom provider that takes the server_name as argument, using it to both print the title and request textDocument/hover for a single client each time.
The trick was to create and register a new provider on server attach, to be able to split the results while keeping a reference to the name.

Since this happens on on_attach event, I'm not sure it can be easily included in the original code, but it would be nice to have.

Here is the implementation and result:

local util = vim.lsp.util
local get_clients = vim.lsp.get_clients or vim.lsp.get_active_clients

local LSPServerHover = function(server_name)
  return {
    name = string.format('LSP[%s]', server_name),
    priority = 1002, -- above diagnostics

    enabled = function(bufnr)
      return #get_clients({ bufnr = bufnr, name = server_name, method = 'textDocument/hover' }) > 0
    end,

    execute = function(opts, done)
      local params = util.make_position_params()
      local client = get_clients({ bufnr = opts.bufnr, name = server_name, method = 'textDocument/hover' })[1]

      client.request('textDocument/hover', params, function(err, result)
        if result and result.contents and result.contents.value then
          local value = result.contents.value
          done({ lines = vim.split(value, '\n', true), filetype = 'markdown' })
        else
          print(err)
          done()
        end
      end)
    end,
  }
end
-- USAGE:
-- when configuring LSP Servers
    on_attach = function(_, bufnr)
      hover.register(LSPServerHover(server_name))
      vim.keymap.set({ 'n', 'i' }, '<F9>', hover.hover)
    end,

https://github.com/paulodiovani/dotfiles/blob/4eb17e19b113b32f3fd6da2c7357d26958056edb/home/user/.config/nvim/lua/completion.lua#L5-L29

Screenshots:
image

image

First posted on neovim/nvim-lspconfig#3282 (comment)

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

No branches or pull requests

1 participant