refactor(nvim): split lsp into setup and mappings

This commit is contained in:
Oliver Davies 2021-10-13 21:00:22 +01:00
parent ab788203db
commit dedcbddc86
4 changed files with 67 additions and 70 deletions

View file

@ -1,70 +0,0 @@
local has_lsp, lspconfig = pcall(require, "lspconfig")
local servers = {
ansiblels = true,
bashls = true,
cssls = true,
html = true,
intelephense = {
filetypes = { "install", "inc", "module", "php", "test", "theme" }
},
tsserver = {
filetypes = { "js", "jsx", "typescript", "vue" }
},
yamlls = true,
}
local setup_server = function(server, config)
if not config then
return
end
if type(config) ~= "table" then
config = {}
end
lspconfig[server].setup(config)
end
for server, config in pairs(servers) do
setup_server(server, config)
end
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
vim.lsp.diagnostic.on_publish_diagnostics, {
underline = true,
virtual_text = false,
signs = true,
update_in_insert = true,
}
)
local key_mapper = function(mode, key, result)
vim.api.nvim_set_keymap(
mode,
key,
result,
{noremap = true, silent = true}
)
end
key_mapper('n', '<c-k>', ':lua vim.lsp.buf.signature_help()<CR>')
key_mapper('n', '<leader>af', ':lua vim.lsp.buf.code_action()<CR>')
key_mapper('n', '<leader>dn', ':lua vim.lsp.diagnostic.goto_next()<CR>')
key_mapper('n', '<leader>dp', ':lua vim.lsp.diagnostic.goto_prev()<CR>')
key_mapper('n', '<leader>ds', ':lua vim.lsp.diagnostic.show_line_diagnostics()<CR>')
key_mapper('n', '<leader>fb', ':lua require"telescope.builtin".buffers()<CR>')
key_mapper('n', '<leader>fh', ':lua require"telescope.builtin".help_tags()<CR>')
key_mapper('n', '<leader>fs', ':lua require"telescope.builtin".live_grep()<CR>')
key_mapper('n', '<leader>rn', ':lua vim.lsp.buf.rename()<CR>')
key_mapper('n', 'K', ':lua vim.lsp.buf.hover()<CR>')
key_mapper('n', 'gD', ':lua vim.lsp.buf.declaration()<CR>')
key_mapper('n', 'gW', ':lua vim.lsp.buf.workspace_symbol()<CR>')
key_mapper('n', 'gd', ':lua vim.lsp.buf.definition()<CR>')
key_mapper('n', 'gi', ':lua vim.lsp.buf.implementation()<CR>')
key_mapper('n', 'gr', ':lua vim.lsp.buf.references()<CR>')
key_mapper('n', 'gt', ':lua vim.lsp.buf.type_definition()<CR>')
key_mapper('n', 'gw', ':lua vim.lsp.buf.document_symbol()<CR>')

View file

@ -0,0 +1,2 @@
require "opdavies.plugins.lsp.setup"
require "opdavies.plugins.lsp.mappings"

View file

@ -0,0 +1,22 @@
local map_options = {
noremap = true,
silent = true,
}
vim.api.nvim_set_keymap('n', '<c-k>', ':lua vim.lsp.buf.signature_help()<CR>', map_options)
vim.api.nvim_set_keymap('n', '<leader>af', ':lua vim.lsp.buf.code_action()<CR>', map_options)
vim.api.nvim_set_keymap('n', '<leader>dn', ':lua vim.lsp.diagnostic.goto_next()<CR>', map_options)
vim.api.nvim_set_keymap('n', '<leader>dp', ':lua vim.lsp.diagnostic.goto_prev()<CR>', map_options)
vim.api.nvim_set_keymap('n', '<leader>ds', ':lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', map_options)
vim.api.nvim_set_keymap('n', '<leader>fb', ':lua require"telescope.builtin".buffers()<CR>', map_options)
vim.api.nvim_set_keymap('n', '<leader>fh', ':lua require"telescope.builtin".help_tags()<CR>', map_options)
vim.api.nvim_set_keymap('n', '<leader>fs', ':lua require"telescope.builtin".live_grep()<CR>', map_options)
vim.api.nvim_set_keymap('n', '<leader>rn', ':lua vim.lsp.buf.rename()<CR>', map_options)
vim.api.nvim_set_keymap('n', 'K', ':lua vim.lsp.buf.hover()<CR>', map_options)
vim.api.nvim_set_keymap('n', 'gD', ':lua vim.lsp.buf.declaration()<CR>', map_options)
vim.api.nvim_set_keymap('n', 'gW', ':lua vim.lsp.buf.workspace_symbol()<CR>', map_options)
vim.api.nvim_set_keymap('n', 'gd', ':lua vim.lsp.buf.definition()<CR>', map_options)
vim.api.nvim_set_keymap('n', 'gi', ':lua vim.lsp.buf.implementation()<CR>', map_options)
vim.api.nvim_set_keymap('n', 'gr', ':lua vim.lsp.buf.references()<CR>', map_options)
vim.api.nvim_set_keymap('n', 'gt', ':lua vim.lsp.buf.type_definition()<CR>', map_options)
vim.api.nvim_set_keymap('n', 'gw', ':lua vim.lsp.buf.document_symbol()<CR>', map_options)

View file

@ -0,0 +1,43 @@
local has_lsp, lspconfig = pcall(require, "lspconfig")
local servers = {
ansiblels = true,
bashls = true,
cssls = true,
html = true,
intelephense = {
filetypes = { "install", "inc", "module", "php", "test", "theme" }
},
tsserver = {
filetypes = { "js", "jsx", "typescript", "vue" }
},
yamlls = true,
}
local setup_server = function(server, config)
if not config then
return
end
if type(config) ~= "table" then
config = {}
end
lspconfig[server].setup(config)
end
for server, config in pairs(servers) do
setup_server(server, config)
end
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
vim.lsp.diagnostic.on_publish_diagnostics, {
underline = true,
virtual_text = false,
signs = true,
update_in_insert = true,
}
)