summary refs log tree commit diff
path: root/.vim/lua/lsp.lua
blob: d8a4818cb53b59c7832e2886d08c6bfa093f51e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
local lsp         = require('lspconfig')
-- local spinner_lsp = require('spinner.lsp')
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.lsp.stop_client()
    return
  end

  -- spinner_lsp.on_attach(client, bufnr)
  lspstatus.on_attach(client, bufnr)

  if client.resolved_capabilities.goto_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)
  end
  if client.resolved_capabilities.hover then
    vim.keymap.set('n', 'K'         , vim.lsp.buf.hover)
  end
  if client.resolved_capabilities.rename then
    vim.keymap.set('n', 'grr'       , vim.lsp.buf.rename)
  end
  if client.resolved_capabilities.signature_help 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)
  end
  if client.resolved_capabilities.type_definition then
    vim.keymap.set('n', 'gT'        , vim.lsp.buf.type_definition)
  end
  if client.resolved_capabilities.declaration then
    vim.keymap.set('n', 'gD'        , vim.lsp.buf.declaration)
  end
  if client.resolved_capabilities.implementation then
    vim.keymap.set('n', 'gI'        , vim.lsp.buf.implementation)
  end
  if client.resolved_capabilities.workspace_symbol then
    vim.keymap.set('n', 'gW'        , vim.lsp.buf.workspace_symbol)
  end
  -- if client.resolved_capabilities.document_symbol then
  --   vim.keymap.set('n', 'g0', 'vim.lsp.buf.document_symbol')
  -- end

  augroup {
    lsp_user = {
      {'BufWritePre' , '*'    ,  'lua require("lsp").format(vim.fn.expand("<afile>:p"))'},
      {'BufWritePre' , '*.go' ,  'lua vim.lsp.buf.code_action({ only = {"source.organizeImports"} })'},
    }
  }
end

local my_exit = function(code, signal, client_id)
  -- spinner_lsp.on_exit(code, signal, client_id)
end

local capabilities = 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',
    }
  }
  -- add window/workDoneProgress capability
  caps = vim.tbl_extend('keep', caps or {}, lspstatus.capabilities)
  return caps
end

local setup = function()
  local caps = capabilities()
  -- spinner_lsp.setup()
  -- spinner_lsp.init_capabilities(caps)

  -- handler overwrites
  -- 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 #actions == 0 then
  --     -- skip printing 'no code actions available'
  --     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: blabla',
  --       },
  --       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
  -- }}}

  lsp.ccls.setup {
    on_attach = my_attach,
    on_exit = my_exit,
    capabilities = caps,
    root_dir = lsp.util.root_pattern('.ccls','meson.build', 'Makefile','.git'),
    init_options = {
      compilationDatabaseDirectory = "build",
      completion = {
        filterAndSort = false,
      }
    }
  }

  lsp.gopls.setup {
    on_attach = my_attach,
    on_exit = my_exit,
    capabilities = caps,
    settings = {
      gopls = {
        analyses = {
          --  composites = false,
          fieldalignment = true,
          nilness = true,
          shadow = false,
          unusedparams = true,
          unusedwrite = true,
        },
        gofumpt = true,
        -- hoverKind = 'Structured',
      }
    }
  }

  lsp.sumneko_lua.setup {
    on_attach = my_attach,
    on_exit = my_exit,
    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,
    on_exit = my_exit,
    capabilities = caps,
    cmd = { 'env', 'CARGO_TARGET_DIR=/tmp', 'RUST_SRC_PATH=/usr/src/rustlib/library', 'rust-analyzer' },
  }

  lsp.zls.setup {
    on_attach = my_attach,
    on_exit = my_exit,
    capabilities = caps,
  }

end

return {
  my_attach = my_attach,
  my_exit = my_exit,
  capabilities = capabilities,
  setup = setup,

  format = function(afile)
    local doit = true
    doit = doit and not string.match(afile, "^/home/robert/devel/upstream")
    if doit then
      print('Formatting using LSP:', afile)
      vim.lsp.buf.formatting_seq_sync(nil, 1000)
    end
  end,
}