summary refs log tree commit diff
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2021-08-30 18:18:39 +0200
committerRobert Günzler <r@gnzler.io>2021-08-31 12:41:19 +0200
commitaf518a14441209bc9971d1ecd95920a98496b372 (patch)
tree5ec46a77a81d697bd7ceb733a00a11957f827627
parentf0cb9f1f65c103a86447f9bbed64bd98a0b36cd8 (diff)
vim: create lsp mappings based on capabilities
-rw-r--r--.vim/lua/lsp.lua75
1 files changed, 52 insertions, 23 deletions
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 { '<C-]>'     , vim.lsp.buf.definition }
-  nnoremap { 'gd'        , vim.lsp.buf.definition }
-  nnoremap { 'grr'       , vim.lsp.buf.rename }
-  nnoremap { '<c-h>'     , vim.lsp.buf.signature_help }
+  if client.resolved_capabilities.goto_definition then
+    nnoremap { '<C-]>'     , 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 { '<c-h>'     , vim.lsp.buf.signature_help }
+    -- nnoremap <silent> ;s <cmd> lua vim.lsp.buf.signature_help()<CR>
+  end
+  if client.resolved_capabilities.document_formatting then
+    nnoremap { '<leader>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 { '<leader>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 <silent> ;s <cmd> lua vim.lsp.buf.signature_help()<CR>
   nnoremap { '<leader>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