hover.nvim icon indicating copy to clipboard operation
hover.nvim copied to clipboard

LSP error and warnings provider

Open AntoinePrv opened this issue 2 years ago • 4 comments

Hello,

Would you consider a provider for LSP warnings and LSP errors? I think the provider should be straightforward (although I have little knowledge about this). However, for it to play nicely with the existing LSP provider, one would probably need some form of priority among providers, or a way to display multiple hover windows (more complicated).

AntoinePrv avatar Apr 25 '23 08:04 AntoinePrv

Sounds like a good idea. Instead of LSP warning/errors, it can be a general diagnostics hover.

lewis6991 avatar Apr 25 '23 08:04 lewis6991

hey, I've got this.

here's a screenshot.

image

feel free to use it! I wanted to have this combined so bad, as it's the way "quick info" works in visual studio, and I really like that, so I decided to see about doing it here, which totally works.

here's how I do it from within my plugins spec for Lazyvim, but should be easily re-interpreted for other types of setups. this is in my plugins folder as hover.lua, though for lazyvim it really only matters what "spec" table you return.

local LSPWithDiagSource = {
  name = "LSPWithDiag",
  priority = 1000,
  enabled = function()
    return true
  end,
  execute = function(done)
    local util = require("vim.lsp.util")

    local params = util.make_position_params()
    ---@type table<string>
    local lines = {}
    vim.lsp.buf_request_all(0, "textDocument/hover", params, function(responses)
      -- vim.notify("responses for hover request " .. vim.inspect(responses))
      local lang = "markdown"
      for _, response in pairs(responses) do
        if response.result and response.result.contents then
          lang = response.result.contents.language or "markdown"
          lines = util.convert_input_to_markdown_lines(response.result.contents or { kind = "markdown", value = "" })
          lines = util.trim_empty_lines(lines or {})
        end
      end

      local unused
      local _, row = unpack(vim.fn.getpos("."))
      row = row - 1
      -- vim.notify("row " .. row)
      ---@type Diagnostic[]
      local lineDiag = vim.diagnostic.get(0, { lnum = row })
      -- vim.notify("curently " .. #lineDiag .. " diagnostics")
      if #lineDiag > 0 then
        for _, d in pairs(lineDiag) do
          if d.message then
            lines = util.trim_empty_lines(util.convert_input_to_markdown_lines({
              language = lang,
              value = string.format("[%s] - %s:%s", d.source, diagnostic_severities[d.severity], d.message),
            }, lines or {}))
          end
        end
      end
      for _, l in pairs(lines) do
        l = l:gsub("\r", "")
      end

      if not vim.tbl_isempty(lines) then
        done({ lines = lines, filetype = "markdown" })
        return
      end
      -- no results
      done()
    end)
  end,
}
return {
  "lewis6991/hover.nvim",
  opts = {
    init = function()
      --then in your init function simply register it instead of the default lsp one. 
      require("hover").register(LSPWithDiagSource)
      require("hover.providers.gh")
    end,
    preview_opts = {
      border = nil,
    },
    -- Whether the contents of a currently open hover window should be moved
    -- to a :h preview-window when pressing the hover keymap.
    preview_window = false,
    title = true,
    -- Setup keymaps
    -- vim.keymap.set("n", "K", function()
    -- require("hover").hover()
    -- end, { desc = "hover.nvim" }),
    -- vim.keymap.set("n", "gK", require("hover").hover_select, { desc = "hover.nvim (select)" })
  },
}


here's how I do it from within my plugins spec for Lazyvim.

WillEhrendreich avatar Jul 07 '23 16:07 WillEhrendreich

Combining multiple LSP hovers, following the above snippet: https://github.com/SichangHe/.config/blob/e7baf000ac6042d0a3ddf767b68f0b71e6d14d3d/nvim/lua/hovering.lua

Edit: updated link for newer version of hover.nvim.

SichangHe avatar Aug 24 '23 14:08 SichangHe

Combining multiple LSP hovers, following the above snippet: https://github.com/SichangHe/.config/blob/23803ef973f097fc44dce5fa1500efbad2a3f2e9/nvim/lua/hovering.lua.

It might not support mouse event

nullptr-yxw avatar Jan 22 '24 08:01 nullptr-yxw

So, how would the above example be changed now that diagnostic provider is here? Only the parts about getting the diagnostics, right?

SichangHe avatar Jun 04 '24 23:06 SichangHe