From 4e9764920a99de25366dae420f1f08e22a936273 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 8 Mar 2022 20:20:22 +0100 Subject: [PATCH] home: vim: move 'on_attach' to 'ambroisie.lsp' --- home/vim/lua/ambroisie/lsp.lua | 83 ++++++++++++++++++++++++++ home/vim/lua/ambroisie/utils.lua | 78 ------------------------ home/vim/plugin/settings/lspconfig.vim | 9 +-- home/vim/plugin/settings/null-ls.vim | 3 +- 4 files changed, 90 insertions(+), 83 deletions(-) create mode 100644 home/vim/lua/ambroisie/lsp.lua diff --git a/home/vim/lua/ambroisie/lsp.lua b/home/vim/lua/ambroisie/lsp.lua new file mode 100644 index 0000000..27e5e44 --- /dev/null +++ b/home/vim/lua/ambroisie/lsp.lua @@ -0,0 +1,83 @@ +local M = {} + +-- shared LSP configuration callback +-- @param client native client configuration +-- @param bufnr int? buffer number of the attched client +M.on_attach = function(client, bufnr) + -- Diagnostics + vim.diagnostic.config({ + -- Disable virtual test next to affected regions + virtual_text = false, + -- Show diagnostics signs + signs = true, + -- Underline offending regions + underline = true, + -- Do not bother me in the middle of insertion + update_in_insert = false, + -- Show highest severity first + severity_sort = true, + }) + + vim.cmd([[ + augroup DiagnosticsHover + autocmd! * + " Show diagnostics on "hover" + autocmd CursorHold,CursorHoldI lua vim.diagnostic.open_float(nil, {focus=false, scope="cursor"}) + augroup END + ]]) + + -- Format on save + if client.resolved_capabilities.document_formatting then + vim.cmd([[ + augroup LspFormatting + autocmd! * + autocmd BufWritePre lua vim.lsp.buf.formatting_sync() + augroup END + ]]) + end + + -- Mappings + local wk = require("which-key") + + local function list_workspace_folders() + local utils = require("ambroisie.utils") + utils.dump(vim.lsp.buf.list_workspace_folders()) + end + + local function show_line_diagnostics() + vim.diagnostic.open_float(nil, { scope="line" }) + end + + local function show_buffer_diagnostics() + vim.diagnostic.open_float(nil, { scope="buffer" }) + end + + local keys = { + K = { vim.lsp.buf.hover, "Show symbol information" }, + ["gd"] = { vim.lsp.buf.definition, "Go to definition" }, + ["gD"] = { vim.lsp.buf.declaration, "Go to declaration" }, + ["gi"] = { vim.lsp.buf.implementation, "Go to implementation" }, + ["gr"] = { vim.lsp.buf.references, "List all references" }, + + ["c"] = { + name = "Code", + a = { vim.lsp.buf.code_action, "Code actions" }, + d = { show_line_diagnostics, "Show line diagnostics" }, + D = { show_buffer_diagnostics, "Show buffer diagnostics" }, + r = { vim.lsp.buf.rename, "Rename symbol" }, + s = { vim.lsp.buf.signature_help, "Show signature" }, + t = { vim.lsp.buf.type_definition, "Go to type definition" }, + w = { + name = "Workspace", + a = { vim.lsp.buf.add_workspace_folder, "Add folder to workspace" }, + l = { list_workspace_folders, "List folders in workspace" }, + r = { vim.lsp.buf.remove_workspace_folder, "Remove folder from workspace" }, + }, + }, + } + + wk.register(keys, { buffer = bufnr }) +end + + +return M diff --git a/home/vim/lua/ambroisie/utils.lua b/home/vim/lua/ambroisie/utils.lua index 72f009c..88f3d27 100644 --- a/home/vim/lua/ambroisie/utils.lua +++ b/home/vim/lua/ambroisie/utils.lua @@ -34,82 +34,4 @@ M.list_lsp_clients = function(bufnr) return names end --- shared LSP configuration callback --- @param client native client configuration --- @param bufnr int? buffer number of the attched client -M.on_attach = function(client, bufnr) - -- Diagnostics - vim.diagnostic.config({ - -- Disable virtual test next to affected regions - virtual_text = false, - -- Show diagnostics signs - signs = true, - -- Underline offending regions - underline = true, - -- Do not bother me in the middle of insertion - update_in_insert = false, - -- Show highest severity first - severity_sort = true, - }) - - vim.cmd([[ - augroup DiagnosticsHover - autocmd! * - " Show diagnostics on "hover" - autocmd CursorHold,CursorHoldI lua vim.diagnostic.open_float(nil, {focus=false, scope="cursor"}) - augroup END - ]]) - - -- Format on save - if client.resolved_capabilities.document_formatting then - vim.cmd([[ - augroup LspFormatting - autocmd! * - autocmd BufWritePre lua vim.lsp.buf.formatting_sync() - augroup END - ]]) - end - - -- Mappings - local wk = require("which-key") - - local function list_workspace_folders() - M.dump(vim.lsp.buf.list_workspace_folders()) - end - - local function show_line_diagnostics() - vim.diagnostic.open_float(nil, { scope="line" }) - end - - local function show_buffer_diagnostics() - vim.diagnostic.open_float(nil, { scope="buffer" }) - end - - local keys = { - K = { vim.lsp.buf.hover, "Show symbol information" }, - ["gd"] = { vim.lsp.buf.definition, "Go to definition" }, - ["gD"] = { vim.lsp.buf.declaration, "Go to declaration" }, - ["gi"] = { vim.lsp.buf.implementation, "Go to implementation" }, - ["gr"] = { vim.lsp.buf.references, "List all references" }, - - ["c"] = { - name = "Code", - a = { vim.lsp.buf.code_action, "Code actions" }, - d = { show_line_diagnostics, "Show line diagnostics" }, - D = { show_buffer_diagnostics, "Show buffer diagnostics" }, - r = { vim.lsp.buf.rename, "Rename symbol" }, - s = { vim.lsp.buf.signature_help, "Show signature" }, - t = { vim.lsp.buf.type_definition, "Go to type definition" }, - w = { - name = "Workspace", - a = { vim.lsp.buf.add_workspace_folder, "Add folder to workspace" }, - l = { list_workspace_folders, "List folders in workspace" }, - r = { vim.lsp.buf.remove_workspace_folder, "Remove folder from workspace" }, - }, - }, - } - - wk.register(keys, { buffer = bufnr }) -end - return M diff --git a/home/vim/plugin/settings/lspconfig.vim b/home/vim/plugin/settings/lspconfig.vim index fe04135..dc706cc 100644 --- a/home/vim/plugin/settings/lspconfig.vim +++ b/home/vim/plugin/settings/lspconfig.vim @@ -1,5 +1,6 @@ lua << EOF local lspconfig = require("lspconfig") +local lsp = require("ambroisie.lsp") local utils = require("ambroisie.utils") -- Inform servers we are able to do completion, snippets, etc... @@ -10,7 +11,7 @@ capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities) if utils.is_executable("clangd") then lspconfig.clangd.setup({ capabilities = capabilities, - on_attach = utils.on_attach, + on_attach = lsp.on_attach, }) end @@ -18,7 +19,7 @@ end if utils.is_executable("rnix-lsp") then lspconfig.rnix.setup({ capabilities = capabilities, - on_attach = utils.on_attach, + on_attach = lsp.on_attach, }) end @@ -26,7 +27,7 @@ end if utils.is_executable("pyright") then lspconfig.pyright.setup({ capabilities = capabilities, - on_attach = utils.on_attach, + on_attach = lsp.on_attach, }) end @@ -34,7 +35,7 @@ end if utils.is_executable("rust-analyzer") then lspconfig.rust_analyzer.setup({ capabilities = capabilities, - on_attach = utils.on_attach, + on_attach = lsp.on_attach, }) end EOF diff --git a/home/vim/plugin/settings/null-ls.vim b/home/vim/plugin/settings/null-ls.vim index 6fc0e25..bec8124 100644 --- a/home/vim/plugin/settings/null-ls.vim +++ b/home/vim/plugin/settings/null-ls.vim @@ -1,9 +1,10 @@ lua << EOF local null_ls = require("null-ls") +local lsp = require("ambroisie.lsp") local utils = require("ambroisie.utils") null_ls.setup({ - on_attach = utils.on_attach, + on_attach = lsp.on_attach, }) -- C, C++