nvim/lua/lsp/attach.lua

32 lines
1.4 KiB
Lua
Raw Normal View History

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
2023-05-16 09:22:33 +01:00
local on_attach = function(client, bufnr)
-- Mappings.
-- See `:help vim.lsp.*` for documentation on any of the below functions
local bufopts = { noremap = true, silent = true, buffer = bufnr }
vim.keymap.set("n", "gd", vim.lsp.buf.definition, bufopts)
vim.keymap.set("n", "gr", vim.lsp.buf.references, bufopts)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts)
vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts)
vim.keymap.set("n", "<leader>D", vim.lsp.buf.type_definition, bufopts)
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, bufopts)
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, bufopts)
vim.keymap.set(
"n",
"<leader>i",
function() vim.lsp.buf.format { bufnr = bufnr, timeout_ms = 2000, async = true } end,
bufopts
)
-- specific client tweaks
if client.name ~= "null-ls" then client.server_capabilities.documentFormattingProvider = false end
if client.name == "ruff_lsp" then client.server_capabilities.hoverProvider = false end
-- Attach navic and navbuddy if applicable
2023-05-16 09:22:33 +01:00
if client.server_capabilities.documentSymbolProvider then
require("nvim-navic").attach(client, bufnr)
require("nvim-navbuddy").attach(client, bufnr)
end
end
return on_attach