summary refs log tree commit diff
path: root/.config/nvim/lua/config/lsp.lua
blob: 282a3dce014b06688082c66bdf399cf8f5f8795d (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
if not vim.lsp.enable then return end

local lsp = vim.lsp

lsp.enable({
  'bashls',
  'ccls',
  'clangd',
  'gopls',
  'rust_analyzer',
  'superhtml',
  'tinymist',
  'zk',
  'zls',
})

local capabilities = nil
if pcall(require, 'blink.cmp') then
  capabilities = require('blink.cmp').get_lsp_capabilities()
end

lsp.config('*', {
  root_markers = {'.git'},
  capabilities = capabilities
})

vim.api.nvim_create_autocmd({'LspAttach'}, {
  group = vim.api.nvim_create_augroup('lspattach', {}),
  callback = function(args)
    local client = assert(vim.lsp.get_client_by_id(args.data.client_id))

    -- TODO: what does this do?
    vim.opt_local.omnifunc = 'v:lua.vim.lsp.omnifunc'

    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, { desc = 'goto_definition' })
    vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, { desc = 'goto declaration' })
    vim.keymap.set('n', 'gT', vim.lsp.buf.type_definition, { desc = 'goto type definition' })
    vim.keymap.set('n', 'gR', vim.lsp.buf.references, { desc = 'show references' })
    vim.keymap.set('n', 'ga', vim.lsp.buf.code_action, { desc = 'show code actions' })
    vim.keymap.set('n', 'grr', vim.lsp.buf.rename, { desc = 'rename' })

    if client:supports_method('textDocument/implementation') then
      vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, { desc = 'goto implementation' })
    end

    if client:supports_method('textDocument/completion') then
      vim.lsp.completion.enable(true, client.id, args.buf, {autotrigger = false})
    end

    if client:supports_method('textDocument/formatting') then
    vim.keymap.set('n', '<leader>f', vim.lsp.buf.format, { desc = 'format' })
    end

    if not client:supports_method('textDocument/willSaveWaitUntil')
        and client:supports_method('textDocument/formatting') then
      vim.api.nvim_create_autocmd({'BufWritePre'}, {
        group = vim.api.nvim_create_augroup('lspattach', {}),
        buffer = args.buf,
        callback = function()
          if vim.b.nofmt then return end
          vim.lsp.buf.format({ bufnr = args.buf, id = client.id, timeout_ms = 1000 })
        end,
      })
    end
  end,
})