1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00
jdhao be862a83d6
Use new lsp configuration structure (#403)
In this new structure, the main configuration for a LSP server is
provided by the plugin nvim-lspconfig.
Some of the config may be overridden by the configuration under
directory `after/lsp/xxx.lua`, where `xxx` is the lsp server name used
by nvim-lspconfig.

Note that it is necessary to put the custom lsp server configuration
under `after/` directory, in order to correctly override the config
provided by nvim-lspconfig.
2025-04-26 17:19:46 +02:00

100 lines
3.4 KiB
Lua

local utils = require("utils")
vim.api.nvim_create_autocmd("LspAttach", {
group = vim.api.nvim_create_augroup("lsp_buf_conf", { clear = true }),
callback = function(event_context)
local client = vim.lsp.get_client_by_id(event_context.data.client_id)
-- vim.print(client.name, client.server_capabilities)
if not client then
return
end
local bufnr = event_context.buf
-- Mappings.
local map = function(mode, l, r, opts)
opts = opts or {}
opts.silent = true
opts.buffer = bufnr
vim.keymap.set(mode, l, r, opts)
end
map("n", "gd", vim.lsp.buf.definition, { desc = "go to definition" })
map("n", "<C-]>", vim.lsp.buf.definition)
map("n", "K", function()
vim.lsp.buf.hover { border = "single", max_height = 25, max_width = 120 }
end)
map("n", "<C-k>", vim.lsp.buf.signature_help)
map("n", "<space>rn", vim.lsp.buf.rename, { desc = "varialbe rename" })
map("n", "<space>ca", vim.lsp.buf.code_action, { desc = "LSP code action" })
map("n", "<space>wa", vim.lsp.buf.add_workspace_folder, { desc = "add workspace folder" })
map("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, { desc = "remove workspace folder" })
map("n", "<space>wl", function()
vim.print(vim.lsp.buf.list_workspace_folders())
end, { desc = "list workspace folder" })
-- Set some key bindings conditional on server capabilities
if client.server_capabilities.documentFormattingProvider and client.name ~= "lua_ls" then
map({ "n", "x" }, "<space>f", vim.lsp.buf.format, { desc = "format code" })
end
-- Disable ruff hover feature in favor of Pyright
if client.name == "ruff" then
client.server_capabilities.hoverProvider = false
end
-- Uncomment code below to enable inlay hint from language server, some LSP server supports inlay hint,
-- but disable this feature by default, so you may need to enable inlay hint in the LSP server config.
-- vim.lsp.inlay_hint.enable(true, {buffer=bufnr})
-- The blow command will highlight the current variable and its usages in the buffer.
if client.server_capabilities.documentHighlightProvider then
local gid = vim.api.nvim_create_augroup("lsp_document_highlight", { clear = true })
vim.api.nvim_create_autocmd("CursorHold", {
group = gid,
buffer = bufnr,
callback = function()
vim.lsp.buf.document_highlight()
end,
})
vim.api.nvim_create_autocmd("CursorMoved", {
group = gid,
buffer = bufnr,
callback = function()
vim.lsp.buf.clear_references()
end,
})
end
end,
nested = true,
desc = "Configure buffer keymap and behavior based on LSP",
})
-- Enable lsp servers when they are available
-- A mapping from lsp server name to the executable name
local enabled_lsp_servers = {
pyright = "delance-langserver",
ruff = "ruff",
ltex = "ltex-ls",
clangd = "clangd",
vimls = "vim-language-server",
bashls = "bash-language-server",
lua_ls = "lua-language-server",
}
for server_name, lsp_executable in pairs(enabled_lsp_servers) do
if utils.executable(lsp_executable) then
vim.lsp.enable(server_name)
else
local msg = string.format(
"Executable '%s' for server '%s' not found! Server will not be enabled",
lsp_executable,
server_name
)
vim.notify(msg, vim.log.levels.WARN, { title = "Nvim-config" })
end
end