2022-03-08 20:20:22 +01:00
|
|
|
local M = {}
|
|
|
|
|
2023-02-27 15:53:31 +01:00
|
|
|
-- Simplified LSP formatting configuration
|
|
|
|
local lsp_format = require("lsp-format")
|
|
|
|
|
2023-09-14 13:14:04 +02:00
|
|
|
--- Move to the next/previous diagnostic, automatically showing the diagnostics
|
|
|
|
--- float if necessary.
|
|
|
|
--- @param forward whether to go forward or backwards
|
2023-05-31 16:00:32 +02:00
|
|
|
local function goto_diagnostic(forward)
|
|
|
|
vim.validate({
|
|
|
|
forward = { forward, "boolean" },
|
|
|
|
})
|
|
|
|
|
|
|
|
local opts = {
|
|
|
|
float = false,
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Only show floating diagnostics if they are otherwise not displayed
|
|
|
|
local config = vim.diagnostic.config()
|
|
|
|
if not (config.virtual_text or config.virtual_lines) then
|
|
|
|
opts.float = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if forward then
|
|
|
|
vim.diagnostic.goto_next(opts)
|
|
|
|
else
|
|
|
|
vim.diagnostic.goto_prev(opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-14 13:14:04 +02:00
|
|
|
--- Move to the next diagnostic, automatically showing the diagnostics float if
|
|
|
|
--- necessary.
|
2023-05-31 16:00:32 +02:00
|
|
|
M.goto_next_diagnostic = function()
|
|
|
|
goto_diagnostic(true)
|
|
|
|
end
|
|
|
|
|
2023-09-14 13:14:04 +02:00
|
|
|
--- Move to the previous diagnostic, automatically showing the diagnostics float
|
|
|
|
--- if necessary.
|
2023-05-31 16:00:32 +02:00
|
|
|
M.goto_prev_diagnostic = function()
|
|
|
|
goto_diagnostic(false)
|
|
|
|
end
|
|
|
|
|
2023-09-14 13:14:04 +02:00
|
|
|
--- shared LSP configuration callback
|
|
|
|
--- @param client native client configuration
|
|
|
|
--- @param bufnr int? buffer number of the attched client
|
2022-03-08 20:20:22 +01:00
|
|
|
M.on_attach = function(client, bufnr)
|
|
|
|
-- Format on save
|
2023-02-27 15:53:31 +01:00
|
|
|
lsp_format.on_attach(client, bufnr)
|
2022-03-08 20:20:22 +01:00
|
|
|
|
|
|
|
-- Mappings
|
|
|
|
local wk = require("which-key")
|
|
|
|
|
|
|
|
local function list_workspace_folders()
|
2024-03-12 16:11:11 +01:00
|
|
|
vim.print(vim.lsp.buf.list_workspace_folders())
|
2022-03-08 20:20:22 +01:00
|
|
|
end
|
|
|
|
|
2023-05-02 22:31:06 +02:00
|
|
|
local function cycle_diagnostics_display()
|
|
|
|
-- Cycle from:
|
|
|
|
-- * nothing displayed
|
|
|
|
-- * single diagnostic at the end of the line (`virtual_text`)
|
|
|
|
-- * full diagnostics using virtual text (`virtual_lines`)
|
2023-05-06 19:58:30 +02:00
|
|
|
local text = vim.diagnostic.config().virtual_text
|
2023-05-02 22:31:06 +02:00
|
|
|
local lines = vim.diagnostic.config().virtual_lines
|
|
|
|
|
|
|
|
-- Text -> Lines transition
|
|
|
|
if text then
|
|
|
|
text = false
|
|
|
|
lines = true
|
|
|
|
-- Lines -> Nothing transition
|
|
|
|
elseif lines then
|
|
|
|
text = false
|
|
|
|
lines = false
|
|
|
|
-- Nothing -> Text transition
|
|
|
|
else
|
|
|
|
text = true
|
|
|
|
lines = false
|
|
|
|
end
|
|
|
|
|
|
|
|
vim.diagnostic.config({
|
|
|
|
virtual_text = text,
|
|
|
|
virtual_lines = lines,
|
|
|
|
})
|
2022-03-08 20:20:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function show_buffer_diagnostics()
|
2023-05-06 19:58:30 +02:00
|
|
|
vim.diagnostic.open_float(nil, { scope = "buffer" })
|
2022-03-08 20:20:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local keys = {
|
|
|
|
K = { vim.lsp.buf.hover, "Show symbol information" },
|
2022-04-25 14:31:20 +02:00
|
|
|
["<C-k>"] = { vim.lsp.buf.signature_help, "Show signature information" },
|
2022-03-08 20:20:22 +01:00
|
|
|
["gd"] = { vim.lsp.buf.definition, "Go to definition" },
|
|
|
|
["gD"] = { vim.lsp.buf.declaration, "Go to declaration" },
|
|
|
|
["gi"] = { vim.lsp.buf.implementation, "Go to implementation" },
|
|
|
|
["gr"] = { vim.lsp.buf.references, "List all references" },
|
|
|
|
|
|
|
|
["<leader>c"] = {
|
|
|
|
name = "Code",
|
|
|
|
a = { vim.lsp.buf.code_action, "Code actions" },
|
2023-05-02 22:31:06 +02:00
|
|
|
d = { cycle_diagnostics_display, "Cycle diagnostics display" },
|
2022-03-08 20:20:22 +01:00
|
|
|
D = { show_buffer_diagnostics, "Show buffer diagnostics" },
|
|
|
|
r = { vim.lsp.buf.rename, "Rename symbol" },
|
|
|
|
s = { vim.lsp.buf.signature_help, "Show signature" },
|
|
|
|
t = { vim.lsp.buf.type_definition, "Go to type definition" },
|
|
|
|
w = {
|
|
|
|
name = "Workspace",
|
|
|
|
a = { vim.lsp.buf.add_workspace_folder, "Add folder to workspace" },
|
|
|
|
l = { list_workspace_folders, "List folders in workspace" },
|
|
|
|
r = { vim.lsp.buf.remove_workspace_folder, "Remove folder from workspace" },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
wk.register(keys, { buffer = bufnr })
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|