diff options
author | Abouzar Parvan <[email protected]> | 2021-09-04 12:23:00 +0430 |
---|---|---|
committer | GitHub <[email protected]> | 2021-09-04 09:53:00 +0200 |
commit | 2fa5c64cd08889a21c677e7bef25260fa2b3b878 (patch) | |
tree | f2a7508b7a1799c48221748f3aca5947c50f6c56 /lua/lsp/handlers.lua | |
parent | a77e36483b39c09ed39a4cd78de7c240e0f9e9a5 (diff) |
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
Diffstat (limited to 'lua/lsp/handlers.lua')
-rw-r--r-- | lua/lsp/handlers.lua | 80 |
1 files changed, 63 insertions, 17 deletions
diff --git a/lua/lsp/handlers.lua b/lua/lsp/handlers.lua index 2322e76a..6d813bd7 100644 --- a/lua/lsp/handlers.lua +++ b/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 |