diff options
Diffstat (limited to '.vim/lua/plugins/_lsp.lua')
| -rw-r--r-- | .vim/lua/plugins/_lsp.lua | 213 |
1 files changed, 155 insertions, 58 deletions
diff --git a/.vim/lua/plugins/_lsp.lua b/.vim/lua/plugins/_lsp.lua index 780f703..9a1a0bb 100644 --- a/.vim/lua/plugins/_lsp.lua +++ b/.vim/lua/plugins/_lsp.lua @@ -5,75 +5,172 @@ local util = require('lspconfig.util') local u = require('utils') local nnoremap = require('astronauta.keymap').nnoremap --- https://github.com/kosayoda/nvim-lightbulb -require('nvim-lightbulb').update_lightbulb { - sign = { - enabled = true, - priority = 10, - }, - float = { - enabled = true, - text = '', - win_opts = {}, - }, -} - +local M = {} local my_attach = function(client) - -- attach completion - require 'completion'.on_attach(client) - - -- lsp mappings - nnoremap { 'K', function() vim.lsp.buf.hover() end } - nnoremap { '<C-]>', function() vim.lsp.buf.definition() end } - nnoremap { 'grr', function() vim.lsp.buf.rename() end } - nnoremap { '<c-h>', function() vim.lsp.buf.signature_help() end } - nnoremap { ']d', function() vim.lsp.diagnostic.goto_next() end } - nnoremap { '[d', function() vim.lsp.diagnostic.goto_prev() end } - - -- nnoremap <silent> gD <cmd> lua vim.lsp.buf.declaration()<CR> - -- nnoremap <silent> gd <cmd> lua vim.lsp.buf.definition()<CR> - -- nnoremap <silent> gT <cmd> lua vim.lsp.buf.type_definition()<CR> - -- nnoremap <silent> gI <cmd> lua vim.lsp.buf.implementation()<CR> - -- nnoremap <silent> gr <cmd> lua vim.lsp.buf.references()<CR> - -- nnoremap <silent> g0 <cmd> lua vim.lsp.buf.document_symbol()<CR> - -- nnoremap <silent> gW <cmd> lua vim.lsp.buf.workspace_symbol()<CR> - -- nnoremap <silent> ;h <cmd> lua vim.lsp.buf.hover()<CR> - -- nnoremap <silent> ;s <cmd> lua vim.lsp.buf.signature_help()<CR> + -- attach completion + require 'completion'.on_attach(client) + + -- lsp mappings + nnoremap { 'K', function() vim.lsp.buf.hover() end } + nnoremap { '<C-]>', function() vim.lsp.buf.definition() end } + nnoremap { 'gd', function() vim.lsp.buf.definition() end } + nnoremap { 'grr', function() vim.lsp.buf.rename() end } + nnoremap { '<c-h>', function() vim.lsp.buf.signature_help() end } + nnoremap { ']d', function() vim.lsp.diagnostic.goto_next() end } + nnoremap { '[d', function() vim.lsp.diagnostic.goto_prev() end } + + nnoremap { '<leader>f', function() vim.lsp.buf.formatting() end } + + -- nnoremap { 'g0', function() vim.lsp.buf.document_symbol() end } + -- nnoremap { 'gr', function() vim.lsp.buf.references() end } + nnoremap { 'gW', function() vim.lsp.buf.workspace_symbol() end } + nnoremap { 'gD', function() vim.lsp.buf.declaration() end } + nnoremap { 'gT', function() vim.lsp.buf.type_definition() end } + nnoremap { 'gI', function() vim.lsp.buf.implementation() end } + + -- nnoremap <silent> ;s <cmd> lua vim.lsp.buf.signature_help()<CR> + nnoremap { '<leader>d', function() vim.lsp.diagnostic.show_line_diagnostics() end } + + u.augroup2 { + lsp_user = { + {'BufWritePre', '*', 'lua vim.lsp.buf.formatting_sync(nil, 1000)'}, + {'BufWritePre', '*.go', 'lua vim.lsp.buf.code_action({ source = { organizeImports = true } })'}, + {'CursorHold', '*', 'lua vim.lsp.diagnostic.show_line_diagnostics()'}, + } + } end +-- handler overwrites {{{ +-- configure builtin diagnositcs +vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( + vim.lsp.diagnostic.on_publish_diagnostics, { + virtual_text = false, + signs = true, + underline = true, + update_in_insert = true, + } +) + +-- 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 +-- }}} + +-- configure signs {{{ +vim.fn.sign_define("LspDiagnosticsSignError", {text="⚡"}) +vim.fn.sign_define("LspDiagnosticsSignWarning", {text="⯈"}) +vim.fn.sign_define("LspDiagnosticsSignInformation", {text="≈"}) +vim.fn.sign_define("LspDiagnosticsSignHint", {text="🞶"}) +-- }}} + -- lsp.ccls.setup{ --- on_attach = my_attach, +-- on_attach = my_attach, -- } lsp.gopls.setup{ - on_attach = my_attach, - root_dir = util.root_pattern('go.mod', '.git'), - -- NOTE: this is done to avoid the gopls warning about being outside gopath - -- message_level = vim.lsp.protocol.MessageType.Error, + on_attach = my_attach, + -- root_dir = util.root_pattern('go.mod', '.git'), + settings = { + gopls = { + -- analyses = { + -- composites = false + -- }, + codelenses = { + test = true, + tidy = true, + }, + gofumpt = true, + } + } } lsp.sumneko_lua.setup{ - on_attach = my_attach; - 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, - } - } - } - }, + on_attach = my_attach; + 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, - cmd = { 'env', 'CARGO_TARGET_DIR=/tmp', 'rust-analyzer' }, + on_attach = my_attach, + cmd = { 'env', 'CARGO_TARGET_DIR=/tmp', 'rust-analyzer' }, } + +lsp.efm.setup{ + on_attach = my_attach, + root_dir = util.root_pattern('.git', '.editorconfig'), + init_options = { documentFormatting = true }, + filetypes = { "sh" }, + settings = { + -- rootMarkers = {".git/"}, + languages = { + sh = { + { + lintCommand = 'shellcheck -f gcc -x', + lintSource = 'shellcheck', + lintFormats = { + '%f:%l:%c: %trror: %m', + '%f:%l:%c: %tarning: %m', + '%f:%l:%c: %tote: %m', + }, + formatCommand = 'shfmt -ci -s -bn', + formatStdin = true, + } + } + } + } +} + +lsp.zls.setup{ + on_attach = my_attach +} + +return M |