summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/config/lsp.lua
blob: 07d9d4cbe24c7959f11a295d2a95a65adae37a1f (plain)
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
vim.diagnostic.config({
  virtual_text = true,
  current_line = true,
})

vim.lsp.config('*', {
  capabilities = {
    textDocument = {
      semanticTokens = {
        multilineTokenSupport = true,
      }
    }
  },
  root_markers = { '.git' },
})

vim.lsp.enable({
  "rust_analyzer",
  "ruff", -- python
  "ty",   -- python
  -- "pyright", -- replace with astrals ty
  -- "delance",
  -- "cyright",
  "bashls",
  "clangd",
  "lua_ls",

  "gopls",
  "cssls",
  "ts_ls",
  "jsonls",
})

local capabilities = require('blink.cmp').get_lsp_capabilities()

local disable_semantic_tokens = {
  lua = true,
  python = true, -- TODO: fix ty highlight colors later
}

vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('lsp', {}),
  callback = function(args)
    local bufnr = args.buf
    local client = assert(vim.lsp.get_client_by_id(args.data.client_id))
    if client:supports_method('textDocument/implementation') then
      -- Create a keymap for vim.lsp.buf.implementation ...
    end
    -- Enable auto-completion. Note: Use CTRL-Y to select an item. |complete_CTRL-Y|
    if client:supports_method('textDocument/completion') then
      -- Optional: trigger autocompletion on EVERY keypress. May be slow!
      -- local chars = {}; for i = 32, 126 do table.insert(chars, string.char(i)) end
      -- client.server_capabilities.completionProvider.triggerCharacters = chars
      vim.lsp.completion.enable(true, client.id, args.buf, { autotrigger = true })
    end
    -- Auto-format ("lint") on save.
    -- Usually not needed if server supports "textDocument/willSaveWaitUntil".
    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('lsp', { clear = false }),
        buffer = args.buf,
        callback = function()
          vim.lsp.buf.format({ bufnr = args.buf, id = client.id, timeout_ms = 1000 })
        end,
      })
    end
    -- LSP keybindings
    vim.opt_local.omnifunc = "v:lua.vim.lsp.omnifunc"
    vim.keymap.set("n", "gd", vim.lsp.buf.definition, { buffer = 0, desc = "Goto definition" })
    vim.keymap.set("n", "gr", vim.lsp.buf.references, { buffer = 0, desc = "List references" })
    vim.keymap.set("n", "gD", vim.lsp.buf.declaration, { buffer = 0, desc = "Goto declaration" })
    vim.keymap.set("n", "gT", vim.lsp.buf.type_definition, { buffer = 0, desc = "Goto type definition" })
    -- vim.keymap.set("n", "K", vim.lsp.buf.hover, { buffer = 0, desc = "Hover documentation" }) -- hover.nvim

    vim.keymap.set("n", "<space>cr", vim.lsp.buf.rename, { buffer = 0, desc = "Rename all references" })
    vim.keymap.set("n", "<space>ca", vim.lsp.buf.code_action, { buffer = 0, desc = "Code action" })

    local filetype = vim.bo[bufnr].filetype
    if disable_semantic_tokens[filetype] then
      client.server_capabilities.semanticTokensProvider = nil
    end

    if client.name == "vscode-css-language-server" then
      capabilities.textDocument.completion.completionItem.snippetSupport = true
    end
  end,
})