nvim: move out lsp on_attach

This commit is contained in:
Primrose 2025-07-08 22:53:00 +02:00
parent 3352b3b6e6
commit 65f5e1107b
Signed by: primrose
GPG key ID: 4E887A4CA9714ADA
2 changed files with 40 additions and 32 deletions

32
.config/nvim/lua/lsp.lua Normal file
View file

@ -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("<C-k>", vim.lsp.buf.signature_help)
nnoremap("gd", telescope.lsp_definitions)
nnoremap("gu", telescope.lsp_references)
nnoremap("<leader>ca", vim.lsp.buf.code_action)
nnoremap("<leader>cl", vim.lsp.codelens.run)
nnoremap("<leader>r", vim.lsp.buf.rename)
nnoremap("<leader>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

View file

@ -1,5 +1,4 @@
local map = vim.keymap.set local map = vim.keymap.set
local methods = vim.lsp.protocol.Methods
---------------------- ----------------------
-- Language servers -- -- Language servers --
@ -76,32 +75,6 @@ local servers = {
------------- -------------
-- Helpers -- -- 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", "<C-k>", 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", "<leader>ca", vim.lsp.buf.code_action, { buffer = bufnr })
map("n", "<leader>cl", vim.lsp.codelens.run, { buffer = bufnr })
map("n", "<leader>r", vim.lsp.buf.rename, { buffer = bufnr })
map("n", "<leader>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 -- Helix style border
local border = { local border = {
@ -142,6 +115,9 @@ capabilities = require("cmp_nvim_lsp").default_capabilities(capabilities)
---------- ----------
require("fidget").setup() require("fidget").setup()
local mylsp = require('lsp')
for name, config in pairs(servers) do for name, config in pairs(servers) do
require("lspconfig")[name].setup { require("lspconfig")[name].setup {
capabilities = capabilities, capabilities = capabilities,
@ -149,7 +125,7 @@ for name, config in pairs(servers) do
-- NOTE: https://github.com/neovim/neovim/issues/30675 -- NOTE: https://github.com/neovim/neovim/issues/30675
offset_encoding = config.offset_encoding or "utf-8", offset_encoding = config.offset_encoding or "utf-8",
on_attach = function(client, bufno) on_attach = function(client, bufno)
on_attach(client, bufno); mylsp.on_attach(client, bufno);
(config.on_attach or function(...) end)(client, bufno) (config.on_attach or function(...) end)(client, bufno)
end, end,
} }
@ -160,7 +136,7 @@ end
------------------------ ------------------------
-- Java -- Java
local config = { local config = {
on_attach = on_attach, on_attach = mylsp.on_attach,
capabilities = capabilities, capabilities = capabilities,
cmd = { cmd = {
-- https://github.com/NixOS/nixpkgs/issues/232822#issuecomment-1564243667 -- https://github.com/NixOS/nixpkgs/issues/232822#issuecomment-1564243667
@ -212,7 +188,7 @@ metals_config.on_attach = function(client, bufnr)
map("n", "<leader>dsi", require("dap").step_into) map("n", "<leader>dsi", require("dap").step_into)
map("n", "<leader>dl", require("dap").run_last) map("n", "<leader>dl", require("dap").run_last)
on_attach(client, bufnr) mylsp.on_attach(client, bufnr)
end end
local nvim_metals_group = vim.api.nvim_create_augroup("nvim-metals", { clear = true }) local nvim_metals_group = vim.api.nvim_create_augroup("nvim-metals", { clear = true })
vim.api.nvim_create_autocmd("FileType", { vim.api.nvim_create_autocmd("FileType", {
@ -240,7 +216,7 @@ vim.g.haskell_tools = {
map("n", "<leader>hhr", ht.repl.toggle, opts) map("n", "<leader>hhr", ht.repl.toggle, opts)
vim.opt_local.shiftwidth = 2 vim.opt_local.shiftwidth = 2
on_attach(client, bufnr) mylsp.on_attach(client, bufnr)
end, end,
default_settings = { default_settings = {
haskell = { haskell = {
@ -267,7 +243,7 @@ vim.g.haskell_tools = {
---------- ----------
vim.g.rustaceanvim = { vim.g.rustaceanvim = {
server = { server = {
on_attach = on_attach, on_attach = mylsp.on_attach,
settings = { settings = {
["rust-analyzer"] = { files = { excludeDirs = { ".direnv/" } } }, ["rust-analyzer"] = { files = { excludeDirs = { ".direnv/" } } },
}, },