diff options
| author | 2026-04-17 10:53:14 +0100 | |
|---|---|---|
| committer | 2026-04-17 10:53:14 +0100 | |
| commit | 355573f9f8632779843bf1d59de94c68a0d07b8b (patch) | |
| tree | 08dfe87efcb12cab26104d3b588f9a3bd1e114f0 | |
| parent | 64dec769914d485a5b54b996f0650fe6662083dd (diff) | |
| download | dotfiles-355573f9f8632779843bf1d59de94c68a0d07b8b.tar.gz dotfiles-355573f9f8632779843bf1d59de94c68a0d07b8b.tar.bz2 dotfiles-355573f9f8632779843bf1d59de94c68a0d07b8b.zip | |
refactor: move LspAttach handler from autocmds.lua to plugins/lsp.lua
The 120-line LspAttach handler (fzf-lua navigation, document highlighting,
codelens, inlay hints) belongs with the LSP plugin configuration, not in
generic autocmds. This puts all LSP behavior in one file.
| -rw-r--r-- | home/.config/nvim/lua/config/autocmds.lua | 122 | ||||
| -rw-r--r-- | home/.config/nvim/lua/plugins/lsp.lua | 138 |
2 files changed, 138 insertions, 122 deletions
diff --git a/home/.config/nvim/lua/config/autocmds.lua b/home/.config/nvim/lua/config/autocmds.lua index c97389f..5b2aa4b 100644 --- a/home/.config/nvim/lua/config/autocmds.lua +++ b/home/.config/nvim/lua/config/autocmds.lua @@ -122,128 +122,6 @@ autocmd("BufWritePost", { command = "!fc-cache", }) -autocmd("LspAttach", { - group = augroup("lsp-attach"), - callback = function(event) - local bufnr = event.buf - - local function map(mode, l, r, desc) - vim.keymap.set(mode, l, r, { buffer = bufnr, desc = "LSP: " .. desc }) - end - local function nmap(l, r, desc) - map("n", l, r, desc) - end - nmap("<c-]>", vim.lsp.buf.definition, "Goto definition") - nmap("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration") - - -- fzf-lua LSP navigation - local fzf = require("fzf-lua") - nmap("gd", fzf.lsp_definitions, "[G]oto [D]efinition") - nmap("gvd", function() - fzf.lsp_definitions({ jump1_action = fzf.actions.file_vsplit }) - end, "[G]oto in a [V]ertical split to [D]efinition") - nmap("gxd", function() - fzf.lsp_definitions({ jump1_action = fzf.actions.file_split }) - end, "[G]oto in a [X]horizontal split to [D]efinition") - nmap("gtd", function() - fzf.lsp_definitions({ jump1_action = fzf.actions.file_tabedit }) - end, "[G]oto in a [T]ab to [D]efinition") - nmap("<leader>D", fzf.lsp_typedefs, "Type [D]efinition") - nmap("<leader>vD", function() - fzf.lsp_typedefs({ jump1_action = fzf.actions.file_vsplit }) - end, "Open in a [V]ertical split Type [D]efinition") - nmap("<leader>xD", function() - fzf.lsp_typedefs({ jump1_action = fzf.actions.file_split }) - end, "Open in a [X]horizontal split Type [D]efinition") - nmap("<leader>tD", function() - fzf.lsp_typedefs({ jump1_action = fzf.actions.file_tabedit }) - end, "Open in a [T]ab Type [D]efinition") - nmap("gri", fzf.lsp_implementations, "[G]oto [I]mplementation") - nmap("grvi", function() - fzf.lsp_implementations({ jump1_action = fzf.actions.file_vsplit }) - end, "[G]oto in a [V]ertical split to [I]mplementation") - nmap("grxi", function() - fzf.lsp_implementations({ jump1_action = fzf.actions.file_split }) - end, "[G]oto in a [X]horizontal split to [I]mplementation") - nmap("grti", function() - fzf.lsp_implementations({ jump1_action = fzf.actions.file_tabedit }) - end, "[G]oto in a [T]ab to [I]mplementation") - nmap("grr", fzf.lsp_references, "[G]oto [R]eferences") - nmap("<leader>ic", fzf.lsp_incoming_calls, "[I]ncoming [C]alls") - nmap("<leader>oc", fzf.lsp_outgoing_calls, "[O]utgoing [C]alls") - nmap("gO", fzf.lsp_document_symbols, "d[O]ocument symbols") - nmap("<leader>ws", fzf.lsp_live_workspace_symbols, "[W]orkspace [S]ymbols") - nmap("<leader>wd", fzf.diagnostics_workspace, "[W]orkspace [D]iagnostics") - - -- Highlight references under cursor - local client = vim.lsp.get_client_by_id(event.data.client_id) - if - client - and client:supports_method( - vim.lsp.protocol.Methods.textDocument_documentHighlight, - event.buf - ) - then - local highlight_augroup = - vim.api.nvim_create_augroup("lsp-highlight", { clear = false }) - vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { - buffer = event.buf, - group = highlight_augroup, - callback = vim.lsp.buf.document_highlight, - }) - - vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { - buffer = event.buf, - group = highlight_augroup, - callback = vim.lsp.buf.clear_references, - }) - - vim.api.nvim_create_autocmd("LspDetach", { - group = vim.api.nvim_create_augroup("lsp-detach", { clear = true }), - callback = function(event2) - vim.lsp.buf.clear_references() - vim.api.nvim_clear_autocmds({ - group = "lsp-highlight", - buffer = event2.buf, - }) - end, - }) - end - - if - client - and client:supports_method( - vim.lsp.protocol.Methods.textDocument_codeLens, - event.buf - ) - then - vim.api.nvim_create_autocmd( - { "CursorHold", "CursorHoldI", "InsertLeave" }, - { - buffer = bufnr, - group = vim.api.nvim_create_augroup("codelens", { clear = true }), - callback = vim.lsp.codelens.refresh, - } - ) - end - - -- Toggle inlay hints - if - client - and client:supports_method( - vim.lsp.protocol.Methods.textDocument_inlayHint, - event.buf - ) - then - nmap("<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, -}) - autocmd("FileType", { group = augroup("treesitter_start"), pattern = { "*" }, diff --git a/home/.config/nvim/lua/plugins/lsp.lua b/home/.config/nvim/lua/plugins/lsp.lua index 585c285..5a0cfaa 100644 --- a/home/.config/nvim/lua/plugins/lsp.lua +++ b/home/.config/nvim/lua/plugins/lsp.lua @@ -79,6 +79,144 @@ return { config = function() vim.lsp.enable("just") vim.lsp.enable("tblgen_lsp_server") + + vim.api.nvim_create_autocmd("LspAttach", { + group = vim.api.nvim_create_augroup("lsp-attach", { clear = true }), + callback = function(event) + local bufnr = event.buf + + local function map(mode, l, r, desc) + vim.keymap.set( + mode, + l, + r, + { buffer = bufnr, desc = "LSP: " .. desc } + ) + end + local function nmap(l, r, desc) + map("n", l, r, desc) + end + nmap("<c-]>", vim.lsp.buf.definition, "Goto definition") + nmap("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration") + + local fzf = require("fzf-lua") + nmap("gd", fzf.lsp_definitions, "[G]oto [D]efinition") + nmap("gvd", function() + fzf.lsp_definitions({ jump1_action = fzf.actions.file_vsplit }) + end, "[G]oto in a [V]ertical split to [D]efinition") + nmap("gxd", function() + fzf.lsp_definitions({ jump1_action = fzf.actions.file_split }) + end, "[G]oto in a [X]horizontal split to [D]efinition") + nmap("gtd", function() + fzf.lsp_definitions({ jump1_action = fzf.actions.file_tabedit }) + end, "[G]oto in a [T]ab to [D]efinition") + nmap("<leader>D", fzf.lsp_typedefs, "Type [D]efinition") + nmap("<leader>vD", function() + fzf.lsp_typedefs({ jump1_action = fzf.actions.file_vsplit }) + end, "Open in a [V]ertical split Type [D]efinition") + nmap("<leader>xD", function() + fzf.lsp_typedefs({ jump1_action = fzf.actions.file_split }) + end, "Open in a [X]horizontal split Type [D]efinition") + nmap("<leader>tD", function() + fzf.lsp_typedefs({ jump1_action = fzf.actions.file_tabedit }) + end, "Open in a [T]ab Type [D]efinition") + nmap("gri", fzf.lsp_implementations, "[G]oto [I]mplementation") + nmap("grvi", function() + fzf.lsp_implementations({ jump1_action = fzf.actions.file_vsplit }) + end, "[G]oto in a [V]ertical split to [I]mplementation") + nmap("grxi", function() + fzf.lsp_implementations({ jump1_action = fzf.actions.file_split }) + end, "[G]oto in a [X]horizontal split to [I]mplementation") + nmap("grti", function() + fzf.lsp_implementations({ jump1_action = fzf.actions.file_tabedit }) + end, "[G]oto in a [T]ab to [I]mplementation") + nmap("grr", fzf.lsp_references, "[G]oto [R]eferences") + nmap("<leader>ic", fzf.lsp_incoming_calls, "[I]ncoming [C]alls") + nmap("<leader>oc", fzf.lsp_outgoing_calls, "[O]utgoing [C]alls") + nmap("gO", fzf.lsp_document_symbols, "d[O]ocument symbols") + nmap( + "<leader>ws", + fzf.lsp_live_workspace_symbols, + "[W]orkspace [S]ymbols" + ) + nmap( + "<leader>wd", + fzf.diagnostics_workspace, + "[W]orkspace [D]iagnostics" + ) + + local client = vim.lsp.get_client_by_id(event.data.client_id) + if + client + and client:supports_method( + vim.lsp.protocol.Methods.textDocument_documentHighlight, + event.buf + ) + then + local highlight_augroup = + vim.api.nvim_create_augroup("lsp-highlight", { clear = false }) + vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, { + buffer = event.buf, + group = highlight_augroup, + callback = vim.lsp.buf.document_highlight, + }) + + vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, { + buffer = event.buf, + group = highlight_augroup, + callback = vim.lsp.buf.clear_references, + }) + + vim.api.nvim_create_autocmd("LspDetach", { + group = vim.api.nvim_create_augroup( + "lsp-detach", + { clear = true } + ), + callback = function(event2) + vim.lsp.buf.clear_references() + vim.api.nvim_clear_autocmds({ + group = "lsp-highlight", + buffer = event2.buf, + }) + end, + }) + end + + if + client + and client:supports_method( + vim.lsp.protocol.Methods.textDocument_codeLens, + event.buf + ) + then + vim.api.nvim_create_autocmd( + { "CursorHold", "CursorHoldI", "InsertLeave" }, + { + buffer = bufnr, + group = vim.api.nvim_create_augroup( + "codelens", + { clear = true } + ), + callback = vim.lsp.codelens.refresh, + } + ) + end + + if + client + and client:supports_method( + vim.lsp.protocol.Methods.textDocument_inlayHint, + event.buf + ) + then + nmap("<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, + }) end, }, { |
