summaryrefslogtreecommitdiff
path: root/pkgs/custom-neovim/config/lua/lsp.lua
blob: 9269713dbcd30e9352dd95af607378dd79a0deb4 (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
89
90
91
92
93
94
-- LSP Configs
-- Get more from https://github.com/neovim/nvim-lspconfig/tree/master/lsp

-- Lua LS
vim.lsp.config("lua_ls", {
  cmd = { 'lua-language-server' },
  filetypes = { 'lua' },
  root_markers = {
    '.luarc.json',
    '.luarc.jsonc',
    '.luacheckrc',
    '.stylua.toml',
    'stylua.toml',
    'selene.toml',
    'selene.yml',
    '.git',
  },
})
vim.lsp.enable("lua_ls")

-- Marksman LS
vim.lsp.config("marksman", {
  cmd = {"marksman", "server"},
  filetypes = {"markdown", "markdown.mdx"},
  root_markers = {".marksman.toml", ".git"},
})
vim.lsp.enable("marksman")

-- Nixd LS
vim.lsp.config("nixd", {
  cmd = { 'nixd' },
  filetypes = { 'nix' },
  root_markers = { 'flake.nix', '.git' },
})
vim.lsp.enable("nixd")

-- CC LS
vim.lsp.config("ccls", {
  cmd = { 'ccls' },
  filetypes = { 'c', 'cpp', 'objc', 'objcpp', 'cuda' },
  root_markers = { 'compile_commands.json', '.ccls', '.git' },
  offset_encoding = 'utf-32',
  -- ccls does not support sending a null root directory
  workspace_required = true,
})
vim.lsp.enable("ccls")

-- LSP Autocommands

-- Disable semantic tokens to stop weird highlighting.
vim.api.nvim_create_autocmd('LspAttach', {
  callback = function(ev)
    local client = vim.lsp.get_client_by_id(ev.data.client_id)
    if client then
      client.server_capabilities.semanticTokensProvider = nil
    end
  end
})

-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function()
    -- Enable completion triggered by <c-x><c-o>
    -- vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

    local mapkey = function(mode, key, desc, action, plugin)
      local keymapper = plugin and require("lz.n").keymap(plugin).set or vim.keymap.set
      keymapper(mode, key, action, { noremap = true, silent = true, desc = desc })
    end

    -- LSP Pickers
    mapkey('n', "<leader>a", "Perform code action", ":FzfLua lsp_code_actions<cr>", "fzf-lua")
    mapkey("n", "<leader>S", "Open workspace symbol picker", ":FzfLua lsp_workspace_symbols<cr>", "fzf-lua")
    mapkey("n", "<leader>s", "Open symbol picker", ":FzfLua lsp_document_symbols<cr>", "fzf-lua")
    mapkey("n", "<leader>I", "Open workspace diagnostic picker", ":FzfLua lsp_workspace_diagnostics<cr>", "fzf-lua")
    mapkey("n", "<leader>i", "Open diagnostic picker", ":FzfLua lsp_document_diagnostics<cr>", "fzf-lua")

    -- Goto Keys
    mapkey('n', 'gD', "Go to declaration", vim.lsp.buf.declaration)
    mapkey('n', 'gd', "Go to definition", vim.lsp.buf.definition)
    mapkey('n', 'go', "Go to type definition", vim.lsp.buf.type_definition)
    mapkey('n', 'gi', "Go to implementation", vim.lsp.buf.implementation)

    -- Other LSP Keys
    mapkey('n', 'gs', "Go to signature help", vim.lsp.buf.signature_help)
    mapkey('n', 'gR', "Go to buffer references", vim.lsp.buf.references)
    mapkey('n', '<leader>r', "Rename symbol", vim.lsp.buf.rename)
    mapkey('n', '<leader>h', "Format buffer", function()
      vim.lsp.buf.format { async = true }
    end)
  end,
})