From 65f5e1107b7bbd2e3e6759145f6efa23c2750e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9ana=20=E6=B1=9F?= Date: Tue, 8 Jul 2025 22:53:00 +0200 Subject: [PATCH] nvim: move out lsp on_attach --- .config/nvim/lua/lsp.lua | 32 +++++++++++++++++++++++++++++ .config/nvim/plugin/lsp.lua | 40 ++++++++----------------------------- 2 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 .config/nvim/lua/lsp.lua diff --git a/.config/nvim/lua/lsp.lua b/.config/nvim/lua/lsp.lua new file mode 100644 index 00000000..34cfcacf --- /dev/null +++ b/.config/nvim/lua/lsp.lua @@ -0,0 +1,32 @@ +local M = {} + +M.on_attach = function(client, bufnr) + local telescope = require("telescope.builtin") + local methods = vim.lsp.protocol.Methods + local function nnoremap(bind, callback, userargs) + local args = userargs or {} + args.buffer = bufnr + vim.keymap.set("n", bind, callback, args) + end + + nnoremap("K", vim.lsp.buf.hover) + nnoremap("", vim.lsp.buf.signature_help) + nnoremap("gd", telescope.lsp_definitions) + nnoremap("gu", telescope.lsp_references) + nnoremap("ca", vim.lsp.buf.code_action) + nnoremap("cl", vim.lsp.codelens.run) + nnoremap("r", vim.lsp.buf.rename) + nnoremap("f", function() vim.lsp.buf.format { async = true } end) + + local filetype = vim.api.nvim_get_option_value("filetype", { buf = bufnr }) + if + client:supports_method(methods.textDocument_inlayHint) + -- Never start cabal with inlay hint request + -- Related: https://github.com/mrcjkb/haskell-tools.nvim/discussions/485 + and filetype ~= "cabal" + then + vim.lsp.inlay_hint.enable(true, { bufnr = bufnr }) + end +end + +return M diff --git a/.config/nvim/plugin/lsp.lua b/.config/nvim/plugin/lsp.lua index 08e22394..4eed6980 100644 --- a/.config/nvim/plugin/lsp.lua +++ b/.config/nvim/plugin/lsp.lua @@ -1,5 +1,4 @@ local map = vim.keymap.set -local methods = vim.lsp.protocol.Methods ---------------------- -- Language servers -- @@ -76,32 +75,6 @@ local servers = { ------------- -- Helpers -- ------------- -local on_attach = function(client, bufnr) - vim.api.nvim_set_option_value("omnifunc", "v:lua.vim.lsp.omnifunc", { buf = bufnr }) - local telescope = require("telescope.builtin") - - map("n", "K", vim.lsp.buf.hover, { buffer = bufnr }) - map("n", "", vim.lsp.buf.signature_help, { buffer = bufnr }) - map("n", "gd", telescope.lsp_definitions, { buffer = bufnr }) - -- conflicts with tabs - -- map("n", "gtd", vim.lsp.buf.type_definition, { buffer = bufnr }) - -- map("n", "gi", vim.lsp.buf.implementation, { buffer = bufnr }) - map("n", "gu", telescope.lsp_references, { buffer = bufnr }) - map("n", "ca", vim.lsp.buf.code_action, { buffer = bufnr }) - map("n", "cl", vim.lsp.codelens.run, { buffer = bufnr }) - map("n", "r", vim.lsp.buf.rename, { buffer = bufnr }) - map("n", "f", function() vim.lsp.buf.format { async = true } end, { buffer = bufnr }) - - local filetype = vim.api.nvim_get_option_value("filetype", { buf = bufnr }) - if - client:supports_method(methods.textDocument_inlayHint) - -- Never start cabal with inlay hint request - -- Related: https://github.com/mrcjkb/haskell-tools.nvim/discussions/485 - and filetype ~= "cabal" - then - vim.lsp.inlay_hint.enable(true, { bufnr = bufnr }) - end -end -- Helix style border local border = { @@ -142,6 +115,9 @@ capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities) ---------- require("fidget").setup() + +local mylsp = require('lsp') + for name, config in pairs(servers) do require("lspconfig")[name].setup { capabilities = capabilities, @@ -149,7 +125,7 @@ for name, config in pairs(servers) do -- NOTE: https://github.com/neovim/neovim/issues/30675 offset_encoding = config.offset_encoding or "utf-8", on_attach = function(client, bufno) - on_attach(client, bufno); + mylsp.on_attach(client, bufno); (config.on_attach or function(...) end)(client, bufno) end, } @@ -160,7 +136,7 @@ end ------------------------ -- Java local config = { - on_attach = on_attach, + on_attach = mylsp.on_attach, capabilities = capabilities, cmd = { -- https://github.com/NixOS/nixpkgs/issues/232822#issuecomment-1564243667 @@ -212,7 +188,7 @@ metals_config.on_attach = function(client, bufnr) map("n", "dsi", require("dap").step_into) map("n", "dl", require("dap").run_last) - on_attach(client, bufnr) + mylsp.on_attach(client, bufnr) end local nvim_metals_group = vim.api.nvim_create_augroup("nvim-metals", { clear = true }) vim.api.nvim_create_autocmd("FileType", { @@ -240,7 +216,7 @@ vim.g.haskell_tools = { map("n", "hhr", ht.repl.toggle, opts) vim.opt_local.shiftwidth = 2 - on_attach(client, bufnr) + mylsp.on_attach(client, bufnr) end, default_settings = { haskell = { @@ -267,7 +243,7 @@ vim.g.haskell_tools = { ---------- vim.g.rustaceanvim = { server = { - on_attach = on_attach, + on_attach = mylsp.on_attach, settings = { ["rust-analyzer"] = { files = { excludeDirs = { ".direnv/" } } }, },