Neovim/lua/plugins/nvim-lspconfig.lua

128 lines
4.8 KiB
Lua

--
-- Start config for builtin Neovim language server protocol client
--
-- Source: https://github.com/RRethy/vim-illuminate
-- ..............................................
--
-- Author: Sven Vogel
-- Created: 08.11.2024
-- Edited: 08.11.2024
--
-- ==============================================
local ui = require 'config.ui'
local icons = require 'config.icons'
return {
{
'neovim/nvim-lspconfig',
dependencies = {
"williamboman/mason.nvim",
{
"williamboman/mason-lspconfig.nvim",
config = function()
end
},
},
opts = {
inlay_hints = {
enabled = true,
},
diagnostics = {
update_in_insert = false,
underline = true,
severity_sort = false,
float = {
border = ui.border,
source = 'always',
},
virtual_text = {
spacing = 4,
source = "if_many",
-- this will set set the prefix to a function that returns the diagnostics icon based on the severity
-- this only works on a recent 0.10.0 build. Will be set to "●" when not supported
prefix = "icons"
},
signs = {
text = {
[vim.diagnostic.severity.ERROR] = icons.diagnostics.error,
[vim.diagnostic.severity.WARN] = icons.diagnostics.warn,
[vim.diagnostic.severity.HINT] = icons.diagnostics.hint,
[vim.diagnostic.severity.INFO] = icons.diagnostics.info
}
}
}
},
config = function(_, opts)
require("mason").setup()
local handlers = {
-- The first entry (without a key) will be the default handler
-- and will be called for each installed server that doesn't have
-- a dedicated handler.
function(server_name) -- default handler (optional)
require("lspconfig")[server_name].setup({
on_attach = function(client, bufnr)
-- optionally enable inlay hints
-- Source: https://www.reddit.com/r/neovim/comments/14em0f8/how_to_use_the_new_lsp_inlay_hints/
-- ..............................................
if client.server_capabilities.inlayHintProvider and opts.inlay_hints.enabled then
vim.lsp.inlay_hint.enable(true, { bufnr })
end
end
})
end,
}
require("mason-lspconfig").setup({ handlers = handlers })
-- Setup diagnostic signs
--
-- Source: https://www.lazyvim.org/plugins/lsp#mason-lspconfignvim
-- ..............................................
-- diagnostics signs
if type(opts.diagnostics.signs) ~= "boolean" then
for severity, icon in pairs(opts.diagnostics.signs.text) do
local name = vim.diagnostic.severity[severity]:lower():gsub("^%l", string.upper)
name = "DiagnosticSign" .. name
vim.fn.sign_define(name, { text = icon, texthl = name, numhl = "" })
end
end
-- diagnostic sign as prefix for virtual text
if type(opts.diagnostics.virtual_text) == "table" and opts.diagnostics.virtual_text.prefix == "icons" then
opts.diagnostics.virtual_text.prefix = vim.fn.has("nvim-0.10.0") == 0 and "" or function(diagnostic)
for d, icon in pairs(icons.diagnostics) do
if diagnostic.severity == vim.diagnostic.severity[d:upper()] then
return icon
end
end
end
end
-- Setup diagnostic user interface
--
-- Source: https://vonheikemen.github.io/devlog/tools/setup-nvim-lspconfig-plus-nvim-cmp
-- ..............................................
vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(
vim.lsp.handlers.hover,
{
border = ui.border
})
vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(
vim.lsp.handlers.signature_help,
{
border = ui.border
})
vim.diagnostic.config(vim.deepcopy(opts.diagnostics))
end
}
}