2021-07-28 23:13:50 +02:00
|
|
|
-- Set Default Prefix.
|
|
|
|
-- Note: You can set a prefix per lsp server in the lv-globals.lua file
|
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M.setup()
|
2021-09-08 01:40:40 +02:00
|
|
|
local config = { -- your config
|
|
|
|
virtual_text = lvim.lsp.diagnostics.virtual_text,
|
|
|
|
signs = lvim.lsp.diagnostics.signs,
|
|
|
|
underline = lvim.lsp.diagnostics.underline,
|
|
|
|
update_in_insert = lvim.lsp.diagnostics.update_in_insert,
|
|
|
|
severity_sort = lvim.lsp.diagnostics.severity_sort,
|
2021-12-06 17:04:46 +01:00
|
|
|
float = lvim.lsp.diagnostics.float,
|
2021-09-08 01:40:40 +02:00
|
|
|
}
|
2021-12-06 17:04:46 +01:00
|
|
|
if vim.fn.has "nvim-0.6" == 1 then
|
|
|
|
vim.diagnostic.config(config)
|
2021-09-08 01:40:40 +02:00
|
|
|
else
|
|
|
|
vim.lsp.handlers["textDocument/publishDiagnostics"] = function(_, _, params, client_id, _)
|
|
|
|
local uri = params.uri
|
|
|
|
local bufnr = vim.uri_to_bufnr(uri)
|
|
|
|
if not bufnr then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local diagnostics = params.diagnostics
|
|
|
|
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)
|
2021-07-31 14:52:25 +02:00
|
|
|
end
|
2021-07-28 23:13:50 +02:00
|
|
|
|
2021-12-06 17:04:46 +01:00
|
|
|
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, {
|
|
|
|
border = lvim.lsp.popup_border,
|
|
|
|
})
|
2021-07-28 23:13:50 +02:00
|
|
|
|
2021-12-06 17:04:46 +01:00
|
|
|
vim.lsp.handlers["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, {
|
|
|
|
border = lvim.lsp.popup_border,
|
|
|
|
})
|
2021-09-22 09:26:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-04 09:53:00 +02:00
|
|
|
function M.show_line_diagnostics()
|
2021-12-06 17:04:46 +01:00
|
|
|
if vim.fn.has "nvim-0.6" == 1 then
|
|
|
|
return vim.diagnostic.open_float(0, { scope = "line" })
|
|
|
|
end
|
|
|
|
|
|
|
|
local function split_by_chunk(text, chunkSize)
|
|
|
|
local s = {}
|
|
|
|
for i = 1, #text, chunkSize do
|
|
|
|
s[#s + 1] = text:sub(i, i + chunkSize - 1)
|
|
|
|
end
|
|
|
|
return s
|
|
|
|
end
|
2021-09-04 09:53:00 +02:00
|
|
|
local diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
|
2021-09-18 10:53:30 +02:00
|
|
|
local severity_highlight = {
|
|
|
|
"LspDiagnosticsFloatingError",
|
|
|
|
"LspDiagnosticsFloatingWarning",
|
|
|
|
"LspDiagnosticsFloatingInformation",
|
|
|
|
"LspDiagnosticsFloatingHint",
|
|
|
|
}
|
|
|
|
local ok, vim_diag = pcall(require, "vim.diagnostic")
|
|
|
|
if ok then
|
|
|
|
local buf_id = vim.api.nvim_win_get_buf(0)
|
|
|
|
local win_id = vim.api.nvim_get_current_win()
|
|
|
|
local cursor_position = vim.api.nvim_win_get_cursor(win_id)
|
|
|
|
severity_highlight = {
|
|
|
|
"DiagnosticFloatingError",
|
|
|
|
"DiagnosticFloatingWarn",
|
|
|
|
"DiagnosticFloatingInfo",
|
|
|
|
"DiagnosticFloatingHint",
|
|
|
|
}
|
|
|
|
diagnostics = vim_diag.get(buf_id, { lnum = cursor_position[1] - 1 })
|
|
|
|
end
|
2021-09-22 09:26:52 +02:00
|
|
|
local lines = {}
|
|
|
|
local max_width = vim.fn.winwidth(0) - 5
|
2021-09-04 09:53:00 +02:00
|
|
|
local height = #diagnostics
|
|
|
|
local width = 0
|
|
|
|
local opts = {}
|
|
|
|
local close_events = { "CursorMoved", "CursorMovedI", "BufHidden", "InsertCharPre" }
|
|
|
|
if height == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local bufnr = vim.api.nvim_create_buf(false, true)
|
2021-09-22 09:26:52 +02:00
|
|
|
local diag_message
|
|
|
|
table.sort(diagnostics, function(a, b)
|
|
|
|
return a.severity < b.severity
|
|
|
|
end)
|
2021-11-07 18:13:45 +01:00
|
|
|
|
|
|
|
local hash = {}
|
|
|
|
local diagnostics_no_dupes = {}
|
|
|
|
for _, v in ipairs(diagnostics) do
|
|
|
|
if not hash[v["message"]] then
|
|
|
|
diagnostics_no_dupes[#diagnostics_no_dupes + 1] = v -- you could print here instead of saving to result table if you wanted
|
|
|
|
hash[v["message"]] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- print(vim.inspect(diagnostics_no_dupes))
|
|
|
|
|
|
|
|
for i, diagnostic in ipairs(diagnostics_no_dupes) do
|
2021-09-04 09:53:00 +02:00
|
|
|
local source = diagnostic.source
|
2021-09-22 09:26:52 +02:00
|
|
|
diag_message = diagnostic.message:gsub("[\n\r]", " ")
|
2021-09-04 09:53:00 +02:00
|
|
|
if source then
|
|
|
|
if string.find(source, "/") then
|
|
|
|
source = string.sub(diagnostic.source, string.find(diagnostic.source, "([%w-_]+)$"))
|
|
|
|
end
|
2021-09-22 09:26:52 +02:00
|
|
|
diag_message = string.format("%d. %s: %s", i, source, diag_message)
|
2021-09-04 09:53:00 +02:00
|
|
|
else
|
2021-09-22 09:26:52 +02:00
|
|
|
diag_message = string.format("%d. %s", i, diag_message)
|
2021-09-04 09:53:00 +02:00
|
|
|
end
|
|
|
|
if diagnostic.code then
|
2021-09-22 09:26:52 +02:00
|
|
|
diag_message = string.format("%s [%s]", diag_message, diagnostic.code)
|
|
|
|
end
|
|
|
|
local msgs = split_by_chunk(diag_message, max_width)
|
|
|
|
for _, diag in ipairs(msgs) do
|
|
|
|
table.insert(lines, { message = diag, severity = diagnostic.severity })
|
|
|
|
width = math.max(diag:len(), width)
|
2021-09-04 09:53:00 +02:00
|
|
|
end
|
|
|
|
end
|
2021-09-22 09:26:52 +02:00
|
|
|
height = #lines
|
2021-09-04 09:53:00 +02:00
|
|
|
opts = vim.lsp.util.make_floating_popup_options(width, height, opts)
|
|
|
|
opts["style"] = "minimal"
|
|
|
|
opts["border"] = "rounded"
|
2021-09-21 10:59:32 +02:00
|
|
|
opts["focusable"] = true
|
2021-09-04 09:53:00 +02:00
|
|
|
|
|
|
|
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)
|
2021-09-22 09:26:52 +02:00
|
|
|
for i, diag in ipairs(lines) do
|
|
|
|
vim.api.nvim_buf_set_lines(bufnr, i - 1, i - 1, 0, { diag.message })
|
2021-09-18 10:53:30 +02:00
|
|
|
vim.api.nvim_buf_add_highlight(bufnr, -1, severity_highlight[diag.severity], i - 1, 0, diag.message:len())
|
2021-09-04 09:53:00 +02:00
|
|
|
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
|
|
|
|
|
2021-07-28 23:13:50 +02:00
|
|
|
return M
|