Files
nvim-config/lua/languages/_shared.lua
T
Aaron Gorodetzky a0d1e6ecaf Add shared LSP infrastructure (LspAttach autocmd)
Extract on_attach logic into a global LspAttach autocmd. This is the modern
Neovim pattern ??? no per-server on_attach needed. Includes keymaps, inlay
hints, format-on-save, and a make_capabilities() helper that properly
integrates cmp_nvim_lsp with per-server snippet support control.
2026-09-14 10:10:02 -04:00

91 lines
3.1 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Shared LSP configuration applied to all language servers via LspAttach.
-- Individual language modules should NOT define on_attach — this handles it globally.
-- Diagnostic signs (global, only needs to be set once)
vim.diagnostic.config({
signs = {
text = {
[vim.diagnostic.severity.ERROR] = "🛑",
[vim.diagnostic.severity.WARN] = "⚠️",
[vim.diagnostic.severity.HINT] = "💡",
[vim.diagnostic.severity.INFO] = "ℹ️",
},
},
})
-- LspAttach autocmd: runs whenever any LSP server attaches to a buffer
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(ev)
local client = vim.lsp.get_client_by_id(ev.data.client_id)
local buf = ev.buf
local opts = {buffer = buf,}
-- Enable inlay hints if supported
if client and client.server_capabilities.inlayHintProvider then
vim.lsp.inlay_hint.enable(true)
end
-- LSP Keymaps
vim.keymap.set('n', 'g[', vim.diagnostic.goto_prev, opts)
vim.keymap.set('n', 'g]', vim.diagnostic.goto_next, opts)
vim.keymap.set('n', 'ga', vim.lsp.buf.code_action, opts)
vim.keymap.set('n', 'gh', vim.lsp.buf.hover, opts)
vim.keymap.set('i', '<C-Space>', vim.lsp.buf.hover, opts)
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
vim.keymap.set('n', 'gs', vim.lsp.buf.signature_help, opts)
vim.keymap.set('n', 'gD', vim.lsp.buf.type_definition, opts)
vim.keymap.set('n', 'gR', vim.lsp.buf.references, opts)
vim.keymap.set('n', 'gr', vim.lsp.buf.rename, opts)
vim.keymap.set('n', 'g0', vim.lsp.buf.document_symbol, opts)
vim.keymap.set('n', 'gW', vim.lsp.buf.workspace_symbol, opts)
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
-- Toggle inlay hints
vim.keymap.set('n', 'g-', function()
vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled())
end, opts)
-- Format on save
if client and client.supports_method('textDocument/formatting') then
local fmt_group = vim.api.nvim_create_augroup('LspFormatOnSave_' .. buf, {clear = true,})
vim.api.nvim_create_autocmd('BufWritePre', {
group = fmt_group,
buffer = buf,
callback = function()
local ft_format_opts = {
lua = {tabSize = 2, insertSpaces = true,},
}
vim.lsp.buf.format({
async = false,
formatting_options = ft_format_opts[vim.bo.filetype],
})
end,
})
end
-- Manual format
vim.keymap.set('n', 'gf', function()
vim.lsp.buf.format {async = true,}
end, opts)
end,
})
local M = {}
--- Build base capabilities for LSP servers (with cmp support).
--- @param snippet_support boolean? Whether to advertise snippet support (default false)
--- @return table capabilities
function M.make_capabilities(snippet_support)
local capabilities = vim.lsp.protocol.make_client_capabilities()
local ok, cmp_lsp = pcall(require, 'cmp_nvim_lsp')
if ok then
capabilities = cmp_lsp.default_capabilities(capabilities)
end
if not snippet_support then
capabilities.textDocument.completion.completionItem.snippetSupport = false
end
return capabilities
end
return M