4 nvim lspconfig
Stephen Senran Zhang edited this page 2025-09-01 14:04:48 +08:00

Install

Install nvim-lspconfig according to the instructions in its README.

https://github.com/neovim/nvim-lspconfig/blob/master/lua/lspconfig/server_configurations/ccls.lua has a built-in rule for ccls. You can customize initialization options with:

local lspconfig = require('lspconfig')
lspconfig.ccls.setup {
  init_options = {
    cache = {
      directory = ".ccls-cache";
    };
  }
}

ccls extensions

To use ccls extensions, e.g., $ccls/inheritance, $ccls/member, etc., we need some hacks.

  1. Copy get_location function from nvim/runtime/lua/vim/lsp/buf.lua to ~/.config/nvim/lua/get_location.lua, and modify it to support custom parameters. An example can be found at here, and you can directly use it.
  2. In init.lua, add kepmaps on attach, e.g.,
local get_location = require'get_location'
require'lspconfig'.ccls.setup {
  -- ...
  on_attach = function(client, bufnr)
    local opts = { buffer = bufnr, remap = false }
    local lopts = { loclist = true }
    -- ...
    vim.keymap.set('n', 'gxb', function() get_location('$ccls/inheritance', {}, lopts) end, opts)
    vim.keymap.set('n', 'gxB', function() get_location('$ccls/inheritance', {levels=3}, lopts) end, opts)
    vim.keymap.set('n', 'gxd', function() get_location('$ccls/inheritance', {derived=true}, lopts) end, opts)
    vim.keymap.set('n', 'gxD', function() get_location('$ccls/inheritance', {derived=true, levels=3}, lopts) end, opts)
    vim.keymap.set('n', 'gxc', function() get_location('$ccls/call', {}, lopts) end, opts)
    vim.keymap.set('n', 'gxC', function() get_location('$ccls/call', {callee=true}, lopts) end, opts)
    vim.keymap.set('n', 'gxs', function() get_location('$ccls/member', {kind=2}, lopts) end, opts)
    vim.keymap.set('n', 'gxf', function() get_location('$ccls/member', {kind=3}, lopts) end, opts)
    vim.keymap.set('n', 'gxm', function() get_location('$ccls/member', {}, lopts) end, opts)
    vim.keymap.set('n', '<C-j>', function() get_location('$ccls/navigate', {direction='D'}, lopts) end, opts)
    vim.keymap.set('n', '<C-k>', function() get_location('$ccls/navigate', {direction='U'}, lopts) end, opts)
    vim.keymap.set('n', '<C-h>', function() get_location('$ccls/navigate', {direction='L'}, lopts) end, opts)
    vim.keymap.set('n', '<C-l>', function() get_location('$ccls/navigate', {direction='R'}, lopts) end, opts)
  end,
  -- ...