put lsp keymaps in on_attach function

This commit is contained in:
Andrzej Stepien 2024-03-17 23:04:58 +01:00
parent 8820c4d055
commit 948adc856b
1 changed files with 49 additions and 0 deletions

View File

@ -33,6 +33,54 @@ require("mason-lspconfig").setup({
},
})
local on_attach = function()
vim.api.nvim_create_autocmd("LspAttach", {
desc = "LSP actions",
callback = function()
local bufmap = function(mode, lhs, rhs)
local opts = { buffer = true }
vim.keymap.set(mode, lhs, rhs, opts)
end
-- Displays hover information about the symbol under the cursor
bufmap("n", "K", "<cmd>lua vim.lsp.buf.hover()<cr>")
-- Jump to the definition
bufmap("n", "gd", "<cmd>lua vim.lsp.buf.definition()<cr>")
-- Jump to declaration
bufmap("n", "gD", "<cmd>lua vim.lsp.buf.declaration()<cr>")
-- Lists all the implementations for the symbol under the cursor
bufmap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<cr>")
-- Jumps to the definition of the type symbol
bufmap("n", "go", "<cmd>lua vim.lsp.buf.type_definition()<cr>")
-- Lists all the references
bufmap("n", "gr", "<cmd>lua vim.lsp.buf.references()<cr>")
-- Displays a function's signature information
bufmap("n", "gs", "<cmd>lua vim.lsp.buf.signature_help()<cr>")
-- Renames all references to the symbol under the cursor
bufmap("n", "<F2>", "<cmd>lua vim.lsp.buf.rename()<cr>")
-- Selects a code action available at the current cursor position
bufmap("n", "<F4>", "<cmd>lua vim.lsp.buf.code_action()<cr>")
-- Show diagnostics in a floating window
bufmap("n", "gl", "<cmd>lua vim.diagnostic.open_float()<cr>")
-- Move to the previous diagnostic
bufmap("n", "nd", "<cmd>lua vim.diagnostic.goto_prev()<cr>")
-- Move to the next diagnostic
bufmap("n", "nd", "<cmd>lua vim.diagnostic.goto_next()<cr>")
end,
})
end
local lspconfig = require("lspconfig")
require("mason-lspconfig").setup_handlers({
@ -55,6 +103,7 @@ require("mason-lspconfig").setup_handlers({
["lua_ls"] = function()
lspconfig.lua_ls.setup({
capabilities = lsp_capabilities,
on_attach = on_attach,
settings = require("config.lsp.servers.lua").settings,
})
end,