Procházet zdrojové kódy

use a separate func instead of replacing the diagnostics messages (#1462)

* use a separate func instead of replacing the diagnostics messages

* fix the styling, also return if no diagnostic is available
Abouzar Parvan před 3 roky
rodič
revize
2fa5c64cd0
2 změnil soubory, kde provedl 64 přidání a 18 odebrání
  1. 63 17
      lua/lsp/handlers.lua
  2. 1 1
      lua/lsp/init.lua

+ 63 - 17
lua/lsp/handlers.lua

@@ -26,28 +26,11 @@ function M.setup()
 
     local diagnostics = params.diagnostics
 
-    for i, v in ipairs(diagnostics) do
-      local source = v.source
-      if source then
-        if string.find(source, "/") then
-          source = string.sub(v.source, string.find(v.source, "([%w-_]+)$"))
-        end
-        diagnostics[i].message = string.format("%s: %s", source, v.message)
-      else
-        diagnostics[i].message = string.format("%s", v.message)
-      end
-
-      if vim.tbl_contains(vim.tbl_keys(v), "code") then
-        diagnostics[i].message = diagnostics[i].message .. string.format(" [%s]", v.code)
-      end
-    end
-
     vim.lsp.diagnostic.save(diagnostics, bufnr, client_id)
 
     if not vim.api.nvim_buf_is_loaded(bufnr) then
       return
     end
-
     vim.lsp.diagnostic.display(diagnostics, bufnr, client_id, config)
   end
 
@@ -60,4 +43,67 @@ function M.setup()
   })
 end
 
+function M.show_line_diagnostics()
+  local diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
+  local diags = vim.deepcopy(diagnostics)
+  local height = #diagnostics
+  local width = 0
+  local opts = {}
+  local close_events = { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" }
+  local diagnostic_severities = {
+    "Error",
+    "Warning",
+    "Information",
+    "Hint",
+  }
+  if height == 0 then
+    return
+  end
+  local bufnr = vim.api.nvim_create_buf(false, true)
+
+  for i, diagnostic in ipairs(diagnostics) do
+    local source = diagnostic.source
+    if source then
+      if string.find(source, "/") then
+        source = string.sub(diagnostic.source, string.find(diagnostic.source, "([%w-_]+)$"))
+      end
+      diags[i].message = string.format("%s: %s", source, diagnostic.message)
+    else
+      diags[i].message = string.format("%s", diagnostic.message)
+    end
+
+    if diagnostic.code then
+      diags[i].message = string.format("%s [%s]", diags[i].message, diagnostic.code)
+    end
+    if diags[i].message:len() > width then
+      width = string.len(diags[i].message)
+    end
+  end
+
+  opts = vim.lsp.util.make_floating_popup_options(width, height, opts)
+  opts["style"] = "minimal"
+  opts["border"] = "rounded"
+
+  vim.api.nvim_buf_set_option(bufnr, "bufhidden", "wipe")
+  local winnr = vim.api.nvim_open_win(bufnr, false, opts)
+  vim.api.nvim_win_set_option(winnr, "winblend", 0)
+  vim.api.nvim_buf_set_var(bufnr, "lsp_floating_window", winnr)
+  for i, diag in ipairs(diags) do
+    vim.api.nvim_buf_set_lines(bufnr, i - 1, i - 1, 0, { diag.message })
+    vim.api.nvim_buf_add_highlight(
+      bufnr,
+      -1,
+      "LspDiagnosticsFloating" .. diagnostic_severities[diag.severity],
+      i - 1,
+      0,
+      diag.message:len()
+    )
+  end
+
+  vim.api.nvim_command(
+    "autocmd QuitPre <buffer> ++nested ++once lua pcall(vim.api.nvim_win_close, " .. winnr .. ", true)"
+  )
+  vim.lsp.util.close_preview_autocmd(close_events, winnr)
+end
+
 return M

+ 1 - 1
lua/lsp/init.lua

@@ -48,7 +48,7 @@ local function add_lsp_buffer_keybindings(bufnr)
     ["gs"] = { "<cmd>lua vim.lsp.buf.signature_help()<CR>", "show signature help" },
     ["gp"] = { "<cmd>lua require'lsp.peek'.Peek('definition')<CR>", "Peek definition" },
     ["gl"] = {
-      "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics({ show_header = false, border = 'single' })<CR>",
+      "<cmd>lua require'lsp.handlers'.show_line_diagnostics()<CR>",
       "Show line diagnostics",
     },
   }