From dc895fe1ee8c5b4b205d453d1148f8acd9a87f6d Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Fri, 29 May 2020 10:54:46 +0530 Subject: [PATCH] nvim: Refactor the nvim-lsp settings This also helps in the key bindings now being in effect only for which language servers has been enabled. Signed-off-by: Sanchayan Maity --- nvim/.config/nvim/after/plugin/nvim_lsp.vim | 9 ------- nvim/.config/nvim/lua/lsp.lua | 27 +++++++++++++++++++++ nvim/.config/nvim/plugin_settings.vim | 3 +-- 3 files changed, 28 insertions(+), 11 deletions(-) delete mode 100644 nvim/.config/nvim/after/plugin/nvim_lsp.vim create mode 100644 nvim/.config/nvim/lua/lsp.lua diff --git a/nvim/.config/nvim/after/plugin/nvim_lsp.vim b/nvim/.config/nvim/after/plugin/nvim_lsp.vim deleted file mode 100644 index dd3e297..0000000 --- a/nvim/.config/nvim/after/plugin/nvim_lsp.vim +++ /dev/null @@ -1,9 +0,0 @@ -nnoremap gd lua vim.lsp.buf.definition() -nnoremap gk lua vim.lsp.buf.hover() -nnoremap gD lua vim.lsp.buf.declaration() -nnoremap gi lua vim.lsp.buf.implementation() -nnoremap gs lua vim.lsp.buf.signature_help() -nnoremap gt lua vim.lsp.buf.type_definition() -nnoremap gr lua vim.lsp.buf.references() -nnoremap g0 lua vim.lsp.buf.document_symbol() -nnoremap gW lua vim.lsp.buf.workspace_symbol() diff --git a/nvim/.config/nvim/lua/lsp.lua b/nvim/.config/nvim/lua/lsp.lua new file mode 100644 index 0000000..3508062 --- /dev/null +++ b/nvim/.config/nvim/lua/lsp.lua @@ -0,0 +1,27 @@ +local nvim_lsp = require('nvim_lsp') + +local on_attach = function(_, bufnr) + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') + require'diagnostic'.on_attach() + require'completion'.on_attach() + + local opts = { noremap=true, silent=true } + vim.api.nvim_buf_set_keymap(bufnr, 'n', '', 'lua vim.lsp.buf.definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', 'lua vim.lsp.buf.hover()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', 'lua vim.lsp.buf.references()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', 'lua vim.lsp.util.show_line_diagnostics()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', 'lua vim.lsp.buf.implementation()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gs', 'lua vim.lsp.buf.signature_help()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gt', 'lua vim.lsp.buf.type_definition()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'g0', 'lua vim.lsp.buf.document_symbol()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gW', 'lua vim.lsp.buf.workspace_symbol()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'de', 'lua vim.lsp.buf.declaration()', opts) + vim.api.nvim_buf_set_keymap(bufnr, 'n', 'rn', 'lua vim.lsp.buf.rename()', opts) +end + +local servers = { 'rust_analyzer' } +for _, lsp in ipairs(servers) do + nvim_lsp[lsp].setup { + on_attach = on_attach, +} +end diff --git a/nvim/.config/nvim/plugin_settings.vim b/nvim/.config/nvim/plugin_settings.vim index a542ee7..3cef649 100644 --- a/nvim/.config/nvim/plugin_settings.vim +++ b/nvim/.config/nvim/plugin_settings.vim @@ -107,5 +107,4 @@ let g:completion_chain_complete_list = { \], \} -lua require'nvim_lsp'.rust_analyzer.setup{} -lua require'nvim_lsp'.rust_analyzer.setup{on_attach=require'diagnostic'.on_attach} +lua require 'lsp'