From 3079e935c793a9aa1831fd165e28636eb7a87dae Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Thu, 29 Jul 2021 23:39:08 +0200 Subject: vim: cleanup config --- .vim/lua/lsp.lua | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 .vim/lua/lsp.lua (limited to '.vim/lua/lsp.lua') diff --git a/.vim/lua/lsp.lua b/.vim/lua/lsp.lua new file mode 100644 index 0000000..0e5f736 --- /dev/null +++ b/.vim/lua/lsp.lua @@ -0,0 +1,168 @@ +local lsp = require('lspconfig') +local nnoremap = require('astronauta.keymap').nnoremap +local spinner_lsp= require('spinner.lsp') + +local my_attach = function(client, bufnr) + spinner_lsp.on_attach(client, bufnr) + + -- lsp mappings + nnoremap { 'K' , vim.lsp.buf.hover } + nnoremap { '' , vim.lsp.buf.definition } + nnoremap { 'gd' , vim.lsp.buf.definition } + nnoremap { 'grr' , vim.lsp.buf.rename } + nnoremap { '' , vim.lsp.buf.signature_help } + nnoremap { ']d' , vim.lsp.diagnostic.goto_next } + nnoremap { '[d' , vim.lsp.diagnostic.goto_prev } + + nnoremap { 'f' , vim.lsp.buf.formatting } + -- nnoremap { 'g0', vim.lsp.buf.document_symbol } + nnoremap { 'gR' , vim.lsp.buf.references } + nnoremap { 'gW' , vim.lsp.buf.workspace_symbol } + nnoremap { 'gD' , vim.lsp.buf.declaration } + nnoremap { 'gT' , vim.lsp.buf.type_definition } + nnoremap { 'gI' , vim.lsp.buf.implementation } + + -- nnoremap ;s lua vim.lsp.buf.signature_help() + nnoremap { 'd' , vim.lsp.diagnostic.show_line_diagnostics } + + require('utils').augroup2 { + lsp_user = { + {'BufWritePre' , '*' , 'lua vim.lsp.buf.formatting_seq_sync(nil, 1000)'}, + {'BufWritePre' , '*.go' , 'lua vim.lsp.buf.code_action({ only = {"source.organizeImports"} })'}, + {'CursorHold' , '*' , 'lua vim.lsp.diagnostic.show_line_diagnostics()'}, + } + } +end + +local setup = function() + local caps = vim.lsp.protocol.make_client_capabilities() + -- enable lsp-based snippets + caps.textDocument.completion.completionItem.snippetSupport = true + caps.textDocument.completion.completionItem.resolveSupport = { + properties = { + 'documentation', + 'detail', + 'additionalTextEdits', + } + } + + spinner_lsp.setup() + spinner_lsp.init_capabilities(caps) + + -- handler overwrites + -- configure builtin diagnositics + vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( -- {{{ + vim.lsp.diagnostic.on_publish_diagnostics, { + virtual_text = false, + signs = true, + underline = true, + update_in_insert = false, + } + ) + -- }}} + + -- overwrite codeAction handler to not ask for confirmation if only a single + -- action is given + vim.lsp.handlers["textDocument/codeAction"] = function(_, _, actions) -- {{{ + if actions == nil or vim.tbl_isempty(actions) then + -- skip printing 'no code actions available' + return + end + + local action_cosen + if #actions == 1 then + action_chosen = actions[1] + else + local option_strings = {"Code Actions:"} + for i, action in ipairs(actions) do + local title = action.title:gsub('\r\n', '\\r\\n') + title = title:gsub('\n', '\\n') + table.insert(option_strings, string.format("%d. %s", i, title)) + end + + local choice = vim.fn.inputlist(option_strings) + if choice < 1 or choice > #actions then + return + end + action_chosen = actions[choice] + end + + if action_chosen.edit or type(action_chosen.command) == "table" then + if action_chosen.edit then + vim.lsp.util.apply_workspace_edit(action_chosen.edit) + end + if type(action_chosen.command) == "table" then + vim.lsp.buf.execute_command(action_chosen.command) + end + else + vim.lsp.buf.execute_command(action_chosen) + end + end + -- }}} + + vim.fn.sign_define("LspDiagnosticsSignError", {text="⚡", numhl="Error"}) + vim.fn.sign_define("LspDiagnosticsSignWarning", {text="⯈", numhl="Warning"}) + vim.fn.sign_define("LspDiagnosticsSignInformation", {text="ℹ"}) + vim.fn.sign_define("LspDiagnosticsSignHint", {text="🞶"}) + + -- lsp.ccls.setup { + -- on_attach = my_attach, + -- capabilities = caps, + -- } + + lsp.gopls.setup { + on_attach = my_attach, + capabilities = caps, + settings = { + gopls = { + -- analyses = { + -- composites = false + -- }, + codelenses = { + test = true, + tidy = true, + }, + gofumpt = true, + } + } + } + + lsp.sumneko_lua.setup { + on_attach = my_attach, + capabilities = caps, + cmd = { 'lua-language-server' }; + settings = { + Lua = { + runtime = { + version = 'LuaJIT', + }, + diagnostics = { + globals = {'vim'}, + }, + workspace = { + library = { + [vim.fn.expand('$VIMRUNTIME/lua')] = true, + [vim.fn.expand('$VIMRUNTIME/lua/vim/lsp')] = true, + } + } + } + } + } + + lsp.rust_analyzer.setup { + on_attach = my_attach, + capabilities = caps, + cmd = { 'env', 'CARGO_TARGET_DIR=/tmp', 'rust-analyzer' }, + } + + lsp.zls.setup { + on_attach = my_attach, + capabilities = caps, + } + +end + +return { + my_attach = my_attach, + setup = setup +} -- cgit 1.4.1