fix: set LSP mappings only for buffers that have a configured language server

This commit is contained in:
Arman.H 2022-04-29 20:57:51 +04:30 committed by siduck
parent 1f3a4787ce
commit a8466ccf4c
2 changed files with 25 additions and 19 deletions

View File

@ -96,73 +96,79 @@ M.comment = function()
map("v", "<leader>/", "<esc><cmd> :lua require('Comment.api').toggle_linewise_op(vim.fn.visualmode())<CR>") map("v", "<leader>/", "<esc><cmd> :lua require('Comment.api').toggle_linewise_op(vim.fn.visualmode())<CR>")
end end
M.lspconfig = function() M.lspconfig = function(bufnr)
-- See `<cmd> :help vim.lsp.*` for documentation on any of the below functions -- See `<cmd> :help vim.lsp.*` for documentation on any of the below functions
map("n", "gD", function()
local buf_map = function(...)
local key, lhs, rhs = ...
map(key, lhs, rhs, { buffer = bufnr })
end
buf_map("n", "gD", function()
vim.lsp.buf.declaration() vim.lsp.buf.declaration()
end) end)
map("n", "gd", function() buf_map("n", "gd", function()
vim.lsp.buf.definition() vim.lsp.buf.definition()
end) end)
map("n", "K", function() buf_map("n", "K", function()
vim.lsp.buf.hover() vim.lsp.buf.hover()
end) end)
map("n", "gi", function() buf_map("n", "gi", function()
vim.lsp.buf.implementation() vim.lsp.buf.implementation()
end) end)
map("n", "<C-k>", function() buf_map("n", "<C-k>", function()
vim.lsp.buf.signature_help() vim.lsp.buf.signature_help()
end) end)
map("n", "<space>D", function() buf_map("n", "<space>D", function()
vim.lsp.buf.type_definition() vim.lsp.buf.type_definition()
end) end)
map("n", "<space>ra", function() buf_map("n", "<space>ra", function()
vim.lsp.buf.rename() vim.lsp.buf.rename()
end) end)
map("n", "<space>ca", function() buf_map("n", "<space>ca", function()
vim.lsp.buf.code_action() vim.lsp.buf.code_action()
end) end)
map("n", "gr", function() buf_map("n", "gr", function()
vim.lsp.buf.references() vim.lsp.buf.references()
end) end)
map("n", "<space>f", function() buf_map("n", "<space>f", function()
vim.diagnostic.open_float() vim.diagnostic.open_float()
end) end)
map("n", "[d", function() buf_map("n", "[d", function()
vim.diagnostic.goto_prev() vim.diagnostic.goto_prev()
end) end)
map("n", "d]", function() buf_map("n", "d]", function()
vim.diagnostic.goto_next() vim.diagnostic.goto_next()
end) end)
map("n", "<space>q", function() buf_map("n", "<space>q", function()
vim.diagnostic.setloclist() vim.diagnostic.setloclist()
end) end)
map("n", "<space>fm", function() buf_map("n", "<space>fm", function()
vim.lsp.buf.formatting() vim.lsp.buf.formatting()
end) end)
map("n", "<space>wa", function() buf_map("n", "<space>wa", function()
vim.lsp.buf.add_workspace_folder() vim.lsp.buf.add_workspace_folder()
end) end)
map("n", "<space>wr", function() buf_map("n", "<space>wr", function()
vim.lsp.buf.remove_workspace_folder() vim.lsp.buf.remove_workspace_folder()
end) end)
map("n", "<space>wl", function() buf_map("n", "<space>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders())) print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end) end)
end end

View File

@ -10,7 +10,7 @@ function M.on_attach(client, bufnr)
client.resolved_capabilities.document_formatting = false client.resolved_capabilities.document_formatting = false
client.resolved_capabilities.document_range_formatting = false client.resolved_capabilities.document_range_formatting = false
require("core.mappings").lspconfig() require("core.mappings").lspconfig(bufnr)
end end
local capabilities = vim.lsp.protocol.make_client_capabilities() local capabilities = vim.lsp.protocol.make_client_capabilities()