From af518a14441209bc9971d1ecd95920a98496b372 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Mon, 30 Aug 2021 18:18:39 +0200 Subject: vim: create lsp mappings based on capabilities --- .vim/lua/lsp.lua | 75 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 23 deletions(-) (limited to '.vim/lua') diff --git a/.vim/lua/lsp.lua b/.vim/lua/lsp.lua index 51c1a51..f5fe263 100644 --- a/.vim/lua/lsp.lua +++ b/.vim/lua/lsp.lua @@ -2,34 +2,63 @@ local lsp = require('lspconfig') local nnoremap = require('astronauta.keymap').nnoremap local spinner_lsp= require('spinner.lsp') +require('lspconfig/configs').zk = { + default_config = { + cmd = {'zk', 'lsp'}, + filetypes = {'markdown'}, + root_dir = function() + return vim.loop.cwd() + end, + settings = {}, + } +} + 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 } + if client.resolved_capabilities.goto_definition then + nnoremap { '' , vim.lsp.buf.definition } + nnoremap { 'gd' , vim.lsp.buf.definition } + nnoremap { 'gR' , vim.lsp.buf.references } + end + if client.resolved_capabilities.hover then + nnoremap { 'K' , vim.lsp.buf.hover } + end + if client.resolved_capabilities.rename then + nnoremap { 'grr' , vim.lsp.buf.rename } + end + if client.resolved_capabilities.signature_help then + nnoremap { '' , vim.lsp.buf.signature_help } + -- nnoremap ;s lua vim.lsp.buf.signature_help() + end + if client.resolved_capabilities.document_formatting then + nnoremap { 'f' , vim.lsp.buf.formatting_sync } + end + if client.resolved_capabilities.type_definition then + nnoremap { 'gT' , vim.lsp.buf.type_definition } + end + if client.resolved_capabilities.declaration then + nnoremap { 'gD' , vim.lsp.buf.declaration } + end + if client.resolved_capabilities.implementation then + nnoremap { 'gI' , vim.lsp.buf.implementation } + end + if client.resolved_capabilities.workspace_symbol then + nnoremap { 'gW' , vim.lsp.buf.workspace_symbol } + end + -- if client.resolved_capabilities.document_symbol then + -- nnoremap { 'g0', vim.lsp.buf.document_symbol } + -- end + 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 } augroup { 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()'}, + -- {'CursorHold' , '*' , 'lua vim.lsp.diagnostic.show_line_diagnostics()'}, } } end @@ -51,7 +80,7 @@ local setup = function() -- handler overwrites -- configure builtin diagnositics - vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with( -- {{{ + vim.lsp.handlers['textDocument/publishDiagnostics'] = vim.lsp.with( -- {{{ vim.lsp.diagnostic.on_publish_diagnostics, { virtual_text = false, signs = true, @@ -63,7 +92,7 @@ local setup = function() -- overwrite codeAction handler to not ask for confirmation if only a single -- action is given - vim.lsp.handlers["textDocument/codeAction"] = function(_, _, actions) -- {{{ + vim.lsp.handlers['textDocument/codeAction'] = function(_, _, actions) -- {{{ if actions == nil or vim.tbl_isempty(actions) then -- skip printing 'no code actions available' return @@ -73,11 +102,11 @@ local setup = function() if #actions == 1 then action_chosen = actions[1] else - local option_strings = {"Code Actions:"} + 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)) + table.insert(option_strings, string.format('%d. %s', i, title)) end local choice = vim.fn.inputlist(option_strings) @@ -87,11 +116,11 @@ local setup = function() action_chosen = actions[choice] end - if action_chosen.edit or type(action_chosen.command) == "table" then + 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 + if type(action_chosen.command) == 'table' then vim.lsp.buf.execute_command(action_chosen.command) end else -- cgit 1.4.1