nvim/lua/lsp.lua 2.9 K raw
1
vim.lsp.enable({
2
  "astro",
3
  "gopls",
4
  "lua_ls",
5
  "tsserver",
6
  "rust-analyzer",
7
  "solc",
8
  "html",
9
  "json"
10
})
11
12
vim.api.nvim_create_autocmd("LspAttach", {
13
  group = vim.api.nvim_create_augroup('lsp-attach', { clear = true }),
14
  callback = function(event)
15
    local map = function(keys, func, desc)
16
      vim.keymap.set("n", keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
17
    end
18
19
    map("gl", vim.diagnostic.open_float, "Open Diagnostic Float")
20
    map("K", vim.lsp.buf.hover, "Hover Documentation")
21
    map("gs", vim.lsp.buf.signature_help, "Signature Documentation")
22
    map("gD", vim.lsp.buf.declaration, "Goto Declaration")
23
    map("<leader>la", vim.lsp.buf.code_action, "Code Action")
24
    map("<leader>lr", vim.lsp.buf.rename, "Rename all references")
25
    map("<leader>lf", vim.lsp.buf.format, "Format")
26
    map("<leader>v", "<cmd>vsplit | lua vim.lsp.buf.definition()<cr>", "Goto Definition in Vertical Split")
27
28
    local function client_supports_method(client, method, bufnr)
29
      if vim.fn.has 'nvim-0.11' == 1 then
30
        return client:supports_method(method, bufnr)
31
      else
32
        return client.supports_method(method, { bufnr = bufnr })
33
      end
34
    end
35
36
    local client = vim.lsp.get_client_by_id(event.data.client_id)
37
    if client and client_supports_method(client, vim.lsp.protocol.Methods.textDocument_documentHighlight, event.buf) then
38
      local highlight_augroup = vim.api.nvim_create_augroup('lsp-highlight', { clear = false })
39
40
      -- When cursor stops moving: Highlights all instances of the symbol under the cursor
41
      vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
42
        buffer = event.buf,
43
        group = highlight_augroup,
44
        callback = vim.lsp.buf.document_highlight,
45
      })
46
      -- When cursor moves: Clears the highlighting
47
      vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
48
        buffer = event.buf,
49
        group = highlight_augroup,
50
        callback = vim.lsp.buf.clear_references,
51
      })
52
53
      -- When LSP detaches: Clears the highlighting
54
      vim.api.nvim_create_autocmd('LspDetach', {
55
        group = vim.api.nvim_create_augroup('lsp-detach', { clear = true }),
56
        callback = function(event2)
57
          vim.lsp.buf.clear_references()
58
          vim.api.nvim_clear_autocmds { group = 'lsp-highlight', buffer = event2.buf }
59
        end,
60
      })
61
    end
62
  end,
63
})
64
65
vim.diagnostic.config({
66
  virtual_lines = false,
67
  -- virtual_text = true,
68
  underline = true,
69
  update_in_insert = false,
70
  severity_sort = true,
71
  float = {
72
    border = "rounded",
73
    source = true,
74
  },
75
  signs = {
76
    text = {
77
      [vim.diagnostic.severity.ERROR] = "󰅚 ",
78
      [vim.diagnostic.severity.WARN] = "󰀪 ",
79
      [vim.diagnostic.severity.INFO] = "󰋽 ",
80
      [vim.diagnostic.severity.HINT] = "󰌶 ",
81
    },
82
    numhl = {
83
      [vim.diagnostic.severity.ERROR] = "ErrorMsg",
84
      [vim.diagnostic.severity.WARN] = "WarningMsg",
85
    },
86
  },
87
})