lewis6991 / hover.nvim

Hover plugin framework for Neovim
MIT License
516 stars 35 forks source link

Feature Request: Thread multiple clients as different providers #74

Open paulodiovani opened 2 months ago

paulodiovani commented 2 months ago

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 https://github.com/neovim/nvim-lspconfig/issues/3282#issuecomment-2307786225