Files
nvim/lua/plugins/lsp.lua

155 lines
5.4 KiB
Lua

-- lua/plugins/lsp.lua
-- Language Server Protocol: mason + nvim-lspconfig + lazydev + fidget
return {
{
-- Lua LSP annotations for Neovim API
'folke/lazydev.nvim',
ft = 'lua',
opts = {
library = {
{ path = '${3rd}/luv/library', words = { 'vim%.uv' } },
},
},
},
{
'neovim/nvim-lspconfig',
dependencies = {
{ 'mason-org/mason.nvim', opts = {} },
'mason-org/mason-lspconfig.nvim',
'WhoIsSethDaniel/mason-tool-installer.nvim',
{ 'j-hui/fidget.nvim', opts = {} },
'saghen/blink.cmp',
},
config = function()
-- Keymaps attached per-buffer when an LSP connects
vim.api.nvim_create_autocmd('LspAttach', {
group = vim.api.nvim_create_augroup('user-lsp-attach', { clear = true }),
callback = function(event)
local map = function(keys, func, desc, mode)
vim.keymap.set(mode or 'n', keys, func, { buffer = event.buf, desc = 'LSP: ' .. desc })
end
local tb = require 'telescope.builtin'
map('grn', vim.lsp.buf.rename, '[R]e[n]ame')
map('gra', vim.lsp.buf.code_action, '[G]oto Code [A]ction', { 'n', 'x' })
map('grr', tb.lsp_references, '[G]oto [R]eferences')
map('gri', tb.lsp_implementations, '[G]oto [I]mplementation')
map('grd', tb.lsp_definitions, '[G]oto [D]efinition')
map('grD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
map('gO', tb.lsp_document_symbols, 'Open Document Symbols')
map('gW', tb.lsp_dynamic_workspace_symbols, 'Open Workspace Symbols')
map('grt', tb.lsp_type_definitions, '[G]oto [T]ype Definition')
-- Compat helper for 0.10 vs 0.11
local function client_supports(client, method, bufnr)
if vim.fn.has 'nvim-0.11' == 1 then
return client:supports_method(method, bufnr)
else
return client.supports_method(method, { bufnr = bufnr })
end
end
local client = vim.lsp.get_client_by_id(event.data.client_id)
-- Document highlight on CursorHold
if client and client_supports(client, vim.lsp.protocol.Methods.textDocument_documentHighlight, event.buf) then
local hl_group = vim.api.nvim_create_augroup('user-lsp-highlight', { clear = false })
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
buffer = event.buf,
group = hl_group,
callback = vim.lsp.buf.document_highlight,
})
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
buffer = event.buf,
group = hl_group,
callback = vim.lsp.buf.clear_references,
})
vim.api.nvim_create_autocmd('LspDetach', {
group = vim.api.nvim_create_augroup('user-lsp-detach', { clear = true }),
callback = function(ev)
vim.lsp.buf.clear_references()
vim.api.nvim_clear_autocmds { group = 'user-lsp-highlight', buffer = ev.buf }
end,
})
end
-- Inlay hints toggle
if client and client_supports(client, vim.lsp.protocol.Methods.textDocument_inlayHint, event.buf) then
map('<leader>th', function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled { bufnr = event.buf })
end, '[T]oggle Inlay [H]ints')
end
end,
})
-- Diagnostics appearance
vim.diagnostic.config {
severity_sort = true,
float = { border = 'rounded', source = 'if_many' },
underline = { severity = vim.diagnostic.severity.ERROR },
signs = vim.g.have_nerd_font and {
text = {
[vim.diagnostic.severity.ERROR] = '󰅚 ',
[vim.diagnostic.severity.WARN] = '󰀪 ',
[vim.diagnostic.severity.INFO] = '󰋽 ',
[vim.diagnostic.severity.HINT] = '󰌶 ',
},
} or {},
virtual_text = {
source = 'if_many',
spacing = 2,
format = function(d)
return d.message
end,
},
}
-- Capabilities enhanced by blink.cmp
local capabilities = require('blink.cmp').get_lsp_capabilities()
-- Language servers to install and configure
-- Add more servers here as needed; see `:help lspconfig-all`
local servers = {
lua_ls = {
settings = {
Lua = {
completion = { callSnippet = 'Replace' },
},
},
},
gopls = {},
intelephense = {},
ts_ls = {},
pyright = {},
cssls = {},
html = {},
jsonls = {},
}
local ensure_installed = vim.tbl_keys(servers)
vim.list_extend(ensure_installed, {
'stylua',
-- formatters (used by conform.nvim)
'black',
'prettier',
'biome',
})
require('mason-tool-installer').setup { ensure_installed = ensure_installed }
require('mason-lspconfig').setup {
ensure_installed = {},
automatic_installation = false,
handlers = {
function(server_name)
local server = servers[server_name] or {}
server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {})
require('lspconfig')[server_name].setup(server)
end,
},
}
end,
},
}