summary refs log tree commit diff
path: root/.vim/lua/internal/lsp.lua
blob: b0aa7dcec6877319c99bb668d5d696d2919db73e (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
-- TODO vim.api.nvim_create_autocmd('LspAttach', {
-- 	callback = function(args)
-- 		local client = vim.lsp.get_client_by_id(args.data.client_id)
-- 		local bufnr = args.buf
-- 	end,
-- })

local my_attach = function(client, bufnr)
  if vim.opt.diff:get() then
    vim.notify('not running LSP client in diff mode', vim.log.levels.WARN)
    vim.lsp.stop_client(client)
    return
  end

  local group = vim.api.nvim_create_augroup('LspOnAttach', {})
  vim.api.nvim_create_autocmd('CursorHold', {
    buffer = bufnr,
    group = group,
    callback = function()
      if _G.diagnostic_hidden then
        return
      end
      -- evalutate if there's already a floating preview buffer
      local existing = vim.F.npcall(vim.api.nvim_buf_get_var, bufnr, 'lsp_floating_preview')
      if not existing or not vim.api.nvim_win_is_valid(existing) then
        vim.diagnostic.open_float()
      end
    end,
  })

  require('lsp-status').on_attach(client)
  if client.supports_method('textDocument/documentSymbol') then
    require('nvim-navic').attach(client, bufnr)
    -- require('aerial').on_attach(client, bufnr)
    -- vim.keymap.set('n', 'g0', vim.lsp.buf.document_symbol, {buffer = bufnr})
  end
  if client.supports_method('workspace/symbol') then
    vim.keymap.set(
      'n',
      'gW',
      vim.lsp.buf.workspace_symbol,
      { buffer = bufnr, desc = 'goto symbol' }
    )
    vim.keymap.set(
      'n',
      ';s',
      require('telescope.builtin').lsp_workspace_symbols,
      { desc = 'lsp workspace symbols' }
    )
  end
  if client.supports_method('textDocument/definition') then
    vim.keymap.set(
      'n',
      '<C-]>',
      vim.lsp.buf.definition,
      { buffer = bufnr, desc = 'goto definition' }
    )
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { buffer = bufnr, desc = 'goto definition' })
    vim.keymap.set('n', 'gR', vim.lsp.buf.references, { buffer = bufnr, desc = 'goto references' })
    vim.keymap.set(
      'n',
      ';R',
      require('telescope.builtin').lsp_references,
      { desc = 'lsp references' }
    )
  end
  if client.supports_method('textDocument/hover') then
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, { buffer = bufnr, desc = 'hover' })
  end
  if client.supports_method('textDocument/rename') then
    vim.keymap.set('n', 'grr', vim.lsp.buf.rename, { buffer = bufnr, desc = 'rename' })
  end
  if client.supports_method('signatureHelp') then
    vim.keymap.set(
      'n',
      '<c-h>',
      vim.lsp.buf.signature_help,
      { buffer = bufnr, desc = 'signature help' }
    )
  end
  if client.supports_method('textDocument/formatting') then
    -- if client.server_capabilities.documentFormattingProvider then
    vim.api.nvim_set_option_value('formatexpr', 'v:lua.vim.lsp.formatexpr()', { buf = bufnr })
    vim.keymap.set('n', '<leader>f', vim.lsp.buf.format, { buffer = bufnr, desc = 'format' })
    vim.api.nvim_create_autocmd('BufWritePre', {
      buffer = bufnr,
      callback = function()
        vim.cmd.mkview({ bang = true })
        require('internal.lsp').format(vim.fn.expand('<afile>:p'))
      end,
      group = vim.api.nvim_create_augroup('LspAutoFormat', {}),
    })
  end
  if client.supports_method('textDocument/typeDefinition') then
    vim.keymap.set(
      'n',
      'gT',
      vim.lsp.buf.type_definition,
      { buffer = bufnr, desc = 'goto typedef' }
    )
  end
  if client.supports_method('textDocument/declaration') then
    vim.keymap.set(
      'n',
      'gD',
      vim.lsp.buf.declaration,
      { buffer = bufnr, desc = 'goto declaration' }
    )
  end
  if client.supports_method('textDocument/implementation') then
    vim.keymap.set(
      'n',
      'gI',
      vim.lsp.buf.implementation,
      { buffer = bufnr, desc = 'goto implementation' }
    )
  end
  if client.supports_method('textDocument/codeAction') then
    -- vim.keymap.set('n', 'ga', vim.lsp.buf.code_action, { buffer = bufnr, desc = 'code action' })
    vim.keymap.set('n', 'ga', vim.cmd.CodeActionMenu, { buffer = bufnr, desc = 'code action' })
  end
  if client.supports_method('textDocument/documentHighlight') then
    vim.keymap.set(
      'n',
      '<leader>8',
      vim.lsp.buf.document_highlight,
      { buffer = bufnr, desc = 'document highlight' }
    )
    -- vim.keymap.set('n', '', vim.lsp.buf.clear_references, {buffer = bufnr, desc = 'clear references'})
    vim.api.nvim_create_autocmd('CursorMoved', {
      callback = vim.lsp.buf.clear_references,
      buffer = bufnr,
    })
  end
end

local my_exit = function(_, _, _) end

local capabilities = function()
  local caps = vim.lsp.protocol.make_client_capabilities()

  -- enable lsp-based snippets
  caps = vim.tbl_extend('keep', caps or {}, require('cmp_nvim_lsp').default_capabilities())

  -- add window/workDoneProgress capability
  caps = vim.tbl_extend('keep', caps or {}, require('lsp-status').capabilities)
  return caps
end

local servers = {
  clangd = {
    cmd = {
      'clangd',
      '--clang-tidy',
      '--header-insertion=iwyu',
      '--import-insertions',
      '--header-insertion-decorators',
    }
  },
  elixirls = {
    cmd = { 'elixir-ls' },
    settings = {
      elixirLS = {
        dialyzerEnabled = false,
      },
    },
  },
  gopls = {
    settings = {
      gopls = {
        analyses = {
          --  composites = false,
          fieldalignment = true,
          nilness = true,
          shadow = false,
          unusedparams = true,
          unusedwrite = true,
        },
        gofumpt = true,
        -- hoverKind = 'Structured',
      },
    },
  },
  lua_ls = {
    -- cmd = { 'lua-language-server' },
    cmd = { 'luals' }, -- custom firejail-wrapped
    settings = {
      Lua = {
        runtime = {
          version = 'LuaJIT',
          path = vim.split(package.path, ';'),
        },
        diagnostics = {
          globals = {
            '_G',
            'assert',
            'error',
            'os',
            'package',
            'pairs',
            'ipairs',
            'pcall',
            'require',
            'string',
            'table',
            'type',
            'unpack',
            'vim',
            'xpcall',
          },
          neededFileStatus = {
            ['code-style-check'] = 'Any',
          },
        },
        format = {
          enable = true,
          defaultConfig = {
            indent_style = 'space',
            indent_size = '2',
          },
        },
        workspace = {
          -- library = vim.api.nvim_get_runtime_file("", true),
          library = {
            [vim.env.VIMRUNTIME .. '/lua'] = true,
            [vim.env.VIMRUNTIME .. '/lua/vim/lsp'] = true,
            [vim.fn.stdpath('config') .. '/lua'] = true,
          },
        },
        telemetry = {
          enable = false,
        },
      },
    },
  },
  rust_analyzer = {
    -- cmd = { 'env', 'CARGO_TARGET_DIR=/tmp', 'RUST_SRC_PATH=/usr/src/rustlib/library', 'rust-analyzer' },
    settings = {
      ['rust-analyzer'] = {
        checkOnSave = {
          command = { 'cargo', 'clippy' },
        },
      },
    },
  },
  tsserver = {},
  pylsp = {},
  zls = {},
}

local setup = function()
  -- configure floating win handlers
  vim.lsp.handlers['textDocument/hover'] = vim.lsp.with(vim.lsp.handlers.hover, {
    focus = false,
    zindex = 100,
    border = _G.floating_win_border,
  })
  vim.lsp.handlers['textDocument/signature_help'] =
      vim.lsp.with(vim.lsp.handlers.signature_help, { border = _G.floating_win_border })

  local caps = capabilities()
  for server, opts in pairs(servers) do
    if not opts.disabled then
      opts = vim.tbl_extend('error', opts, {
        on_attach = my_attach,
        on_exit = my_exit,
        capabilities = caps,
      })
      -- lsp-status custom handlers
      local ok, lspstatus_ext = pcall(require('lsp-status').extensions[server])
      if ok then
        opts = vim.tbl_extend('error', opts, {
          handlers = lspstatus_ext.setup(),
        })
      end
      require('lspconfig')[server].setup(opts)
    end
  end
end

local format = function(afile)
  if vim.g.nofmt then
    return
  end

  local errhandler = function(err)
    vim.notify('internal.lsp.format: ' .. err, vim.log.levels.ERROR)
    return err
  end

  -- save folds
  pcall(vim.cmd.mkview, { bang = true })

  -- run lsp format
  xpcall(vim.lsp.buf.format, errhandler)

  -- run organize imports for go
  if string.match(afile, '.go$') then
    xpcall(
      vim.lsp.buf.code_action,
      errhandler,
      { context = { only = { 'source.organizeImports' } }, apply = true }
    )
  end

  -- restore folds
  pcall(vim.cmd.silent, { 'loadview', bank = true })

  -- vim.notify('%#MoreMsg#󰃢%#Italic# fmt', vim.log.levels.INFO)
  vim.notify('internal.lsp.format: 󰃢', vim.log.levels.INFO)
end

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