summary refs log tree commit diff
path: root/.vim/lua
diff options
context:
space:
mode:
Diffstat (limited to '.vim/lua')
-rw-r--r--.vim/lua/lsp.lua128
1 files changed, 58 insertions, 70 deletions
diff --git a/.vim/lua/lsp.lua b/.vim/lua/lsp.lua
index 98e9e58..efba1b1 100644
--- a/.vim/lua/lsp.lua
+++ b/.vim/lua/lsp.lua
@@ -4,15 +4,33 @@ local lspstatus   = require('lsp-status')
 
 local my_attach = function(client, bufnr)
   if vim.opt.diff:get() then
-    vim.notify('not running LSP client in diff mode')
+    vim.notify('not running LSP client in diff mode', vim.log.levels.WARN)
     vim.lsp.stop_client()
     return
   end
 
+  local group = vim.api.nvim_create_augroup('LspOnAttach', {})
+  vim.api.nvim_create_autocmd('CursorHold', {
+    buffer = bufnr,
+    group = group,
+    callback = function()
+      if not _G.diagnostic_hidden then
+        vim.diagnostic.open_float(nil, {
+          focusable = false,
+          close_events = {'BufLeave', 'CursorMoved', 'InsertEnter', 'FocusLost'},
+          border = 'single',
+          source = 'always',
+          prefix = ' ',
+          scope = 'cursor',
+        })
+      end
+    end,
+  })
+
   -- spinner_lsp.on_attach(client, bufnr)
-  lspstatus.on_attach(client, bufnr)
+  lspstatus.on_attach(client)
 
-  if client.resolved_capabilities.goto_definition then
+  if client.supports_method('textDocument/definition') then
     vim.keymap.set('n', '<C-]>'     , vim.lsp.buf.definition)
     vim.keymap.set('n', 'gd'        , vim.lsp.buf.definition)
     vim.keymap.set('n', 'gR'        , vim.lsp.buf.references)
@@ -21,48 +39,57 @@ local my_attach = function(client, bufnr)
       ['gR'] = {'goto references'},
     }, { buffer = bufnr })
   end
-  if client.resolved_capabilities.hover then
+  if client.supports_method('textDocument/hover') then
     vim.keymap.set('n', 'K'         , vim.lsp.buf.hover)
   end
-  if client.resolved_capabilities.rename then
+  if client.supports_method('textDocument/rename') then
     vim.keymap.set('n', 'grr'       , vim.lsp.buf.rename)
     wk_register({
       ['grr'] = {'rename'},
     }, { buffer = bufnr })
   end
-  if client.resolved_capabilities.signature_help then
+  if client.supports_method('signatureHelp') then
     vim.keymap.set('n', '<c-h>'     , vim.lsp.buf.signature_help)
     -- vim.keymap.set('n', ';s '       , vim.lsp.buf.signature_help)
   end
-  if client.resolved_capabilities.document_formatting then
-    vim.keymap.set('n', '<leader>f' , vim.lsp.buf.formatting_sync)
+  if client.supports_method('textDocument/formatting') then
+    vim.api.nvim_buf_set_option(bufnr, 'formatexpr', 'v:lua.vim.lsp.formatexpr()')
+    vim.keymap.set('n', '<leader>f' , vim.lsp.buf.format)
+    wk_register({
+      ['<leader>f'] = {'format'},
+    }, { buffer = bufnr })
+    vim.api.nvim_create_autocmd('BufWritePre', {
+      buffer = bufnr,
+      group = group,
+      callback = function() require('lsp').format(vim.fn.expand('<afile>:p')) end,
+    })
   end
-  if client.resolved_capabilities.type_definition then
+  if client.supports_method('textDocument/typeDefinition') then
     vim.keymap.set('n', 'gT'        , vim.lsp.buf.type_definition)
     wk_register({
       ['gT'] = {'goto typedef'},
     }, { buffer = bufnr })
   end
-  if client.resolved_capabilities.declaration then
+  if client.supports_method('textDocument/declaration') then
     vim.keymap.set('n', 'gD'        , vim.lsp.buf.declaration)
     wk_register({
       ['gD'] = {'goto declaration'},
     }, { buffer = bufnr })
   end
-  if client.resolved_capabilities.implementation then
+  if client.supports_method('textDocument/implementation') then
     vim.keymap.set('n', 'gI'        , vim.lsp.buf.implementation)
     wk_register({
       ['gI'] = {'goto implementation'},
     }, { buffer = bufnr })
   end
-  if client.resolved_capabilities.workspace_symbol then
+  if client.supports_method('workspace/symbol') then
     vim.keymap.set('n', 'gW'        , vim.lsp.buf.workspace_symbol)
     wk_register({
       ['gW'] = {'goto symbol'},
     }, { buffer = bufnr })
   end
   -- if client.resolved_capabilities.document_symbol then
-  --   vim.keymap.set('n', 'g0', 'vim.lsp.buf.document_symbol')
+  --   vim.keymap.set('n', 'g0', vim.lsp.buf.document_symbol)
   -- end
   if client.supports_method('textDocument/codeAction') then
     vim.keymap.set('n', 'ga', vim.lsp.buf.code_action)
@@ -70,7 +97,7 @@ local my_attach = function(client, bufnr)
   end
 end
 
-local my_exit = function(code, signal, client_id)
+local my_exit = function(_, _, _)
   -- spinner_lsp.on_exit(code, signal, client_id)
 end
 
@@ -90,57 +117,8 @@ local capabilities = function()
   return caps
 end
 
--- overwrite codeAction handler to not ask for confirmation if only a single
--- action is given
-local my_code_action_handler = function(_, _, actions)
-  -- skip printing 'no code actions available'
-  if (not actions) or #actions == 0 then return end
-
-  local action_chosen
-  if #actions == 1 then
-    action_chosen = actions[1]
-  else
-    local option_strings = {}
-    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
-    vim.ui.select(option_strings,
-      {
-        prompt = 'Code Actions',
-      },
-      function(choice)
-        if choice < 1 or choice > #actions then
-          return
-        end
-        action_chosen = actions[choice]
-    end)
-  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
-
-local handlers = {
-  -- ['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, {border = border}),
-  -- ['textDocument/signatureHelp'] = vim.lsp.with(vim.lsp.handlers.signatureHelp, {border = border}),
-  ['textDocument/codeAction'] = my_code_action_handler,
-}
--- }}}
-
-
--- local lua_runtime_path = vim.split(package.path, ';')
--- table.insert(lua_runtime_path, 'lua/?.lua')
--- table.insert(lua_runtime_path, 'lua/?/init.lua')
+-- vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, {border = border}),
+-- vim.lsp.handlers['textDocument/signatureHelp'] = vim.lsp.with(vim.lsp.handlers.signatureHelp, {border = border}),
 
 local servers = {
   ccls = { disabled = true,
@@ -230,12 +208,18 @@ local setup = function()
   -- spinner_lsp.init_capabilities(caps)
   for server, opts in pairs(servers) do
     if not opts.disabled then
-      opts = vim.tbl_extend('keep', opts, {
+      opts = vim.tbl_extend('error', opts, {
         on_attach    = my_attach,
         on_exit      = my_exit,
         capabilities = caps,
-        handlers     = handlers,
       })
+      -- lsp-status custom handlers
+      local ok, lspstatus_ext = pcall(lspstatus.extensions[server])
+      if ok then
+        opts = vim.tbl_extend('error', opts, {
+          handlers = lspstatus_ext.setup(),
+        })
+      end
       lsp[server].setup(opts)
     end
   end
@@ -248,10 +232,14 @@ return {
   setup        = setup,
 
   format = function(afile)
-    local condition = not string.match(afile, "^/home/robert/devel/upstream")
-    if condition then
+    if vim.g.nofmt then return end
+    if not string.match(afile, '^/home/robert/devel/upstream') then
+      vim.lsp.buf.format()
+      if string.match(afile, '.go$') then
+        vim.lsp.buf.code_action({ only = {'source.organizeImports'} })
+      end
       -- vim.notify('%#MoreMsg#󰃢%#Italic# fmt')
-      vim.lsp.buf.formatting_seq_sync(nil, 1000)
+      vim.notify('%#MoreMsg#󰃢%#Italic# fmt')
     end
   end,
 }