Skip to content
Alecto Irene Perez edited this page Aug 24, 2021 · 13 revisions

Install coc.nvim

Open config file with command :CocConfig

{
  "languageserver": {
    "ccls": {
      "command": "ccls",
      "filetypes": ["c", "cpp", "cuda", "objc", "objcpp"],
      "rootPatterns": [".ccls-root", "compile_commands.json"],
      "initializationOptions": {
        "cache": {
          "directory": ".ccls-cache"
        },
        "client": {
          "snippetSupport": true
        }
      }
    }
  }
}

Note: coc.nvim also supports configuration from .vimrc instead.

more about initializationOptions, see src/config.hh in ccls source code

First example:

cd /tmp; mkdir c; cd c
git init  # let coc.nvim know this is a project
printf '#include <stddef.h>\nint main() {}\n' > a.cc
nvim a.cc

For a larger project, you'll need .ccls or compile_commands.json in your project root.

These features will work out-of-the-box. Find more on coc.nvim's README.

nmap <silent> <M-j> <Plug>(coc-definition)
nmap <silent> <C-,> <Plug>(coc-references)
nn <silent> K :call CocActionAsync('doHover')<cr>

textDocument/documentHighlight

set updatetime=300
au CursorHold * sil call CocActionAsync('highlight')
au CursorHoldI * sil call CocActionAsync('showSignatureHelp')

Completion

See client.snippetSupport and completion.placeholder for customization of snippet expansion.

Cross reference extensions

See LSP Extensions for the description of these extensions.

" bases
nn <silent> xb :call CocLocations('ccls','$ccls/inheritance')<cr>
" bases of up to 3 levels
nn <silent> xB :call CocLocations('ccls','$ccls/inheritance',{'levels':3})<cr>
" derived
nn <silent> xd :call CocLocations('ccls','$ccls/inheritance',{'derived':v:true})<cr>
" derived of up to 3 levels
nn <silent> xD :call CocLocations('ccls','$ccls/inheritance',{'derived':v:true,'levels':3})<cr>

" caller
nn <silent> xc :call CocLocations('ccls','$ccls/call')<cr>
" callee
nn <silent> xC :call CocLocations('ccls','$ccls/call',{'callee':v:true})<cr>

" $ccls/member
" member variables / variables in a namespace
nn <silent> xm :call CocLocations('ccls','$ccls/member')<cr>
" member functions / functions in a namespace
nn <silent> xf :call CocLocations('ccls','$ccls/member',{'kind':3})<cr>
" nested classes / types in a namespace
nn <silent> xs :call CocLocations('ccls','$ccls/member',{'kind':2})<cr>

nmap <silent> xt <Plug>(coc-type-definition)<cr>
nn <silent> xv :call CocLocations('ccls','$ccls/vars')<cr>
nn <silent> xV :call CocLocations('ccls','$ccls/vars',{'kind':1})<cr>

nn xx x

$ccls/navigate

See $ccls/navigate

nn <silent><buffer> <C-l> :call CocLocations('ccls','$ccls/navigate',{'direction':'D'})<cr>
nn <silent><buffer> <C-k> :call CocLocations('ccls','$ccls/navigate',{'direction':'L'})<cr>
nn <silent><buffer> <C-j> :call CocLocations('ccls','$ccls/navigate',{'direction':'R'})<cr>
nn <silent><buffer> <C-h> :call CocLocations('ccls','$ccls/navigate',{'direction':'U'})<cr>

Customization with coc.nvim

Options listed in Customization can be passed to CCLS via initializationOptions in the Coc configuration file. For example, if to direct ccls to look for compile_commands.json in the project build directory, you could add an entry for compilationDatabaseDirectory, and update rootPatterns accordingly.

{
  "languageserver": {
    "ccls": {
      "command": "ccls",
      "filetypes": ["c", "cpp", "cuda", "objc", "objcpp"],
      "rootPatterns": [".ccls-root", "build/compile_commands.json"],
      "initializationOptions": {
        "cache": {
          "directory": ".ccls-cache"
        },
        "client": {
          "snippetSupport": true
        },
        "compilationDatabaseDirectory": "build"
      }
    }
  }
}

Options like clang.extraArgs may be similarly be added using the same syntax as client.snippetSupport in the example above. For example, we can support C++20 by default by passing -std=c++20 as an extra argument to clang:

{
  "languageserver": {
    "ccls": {
      "command": "ccls",
      "filetypes": ["c", "cpp", "cuda", "objc", "objcpp"],
      "rootPatterns": [".ccls-root", "build/compile_commands.json"],
      "initializationOptions": {
        "cache": {
          "directory": ".ccls-cache"
        },
        "client": {
          "snippetSupport": true
        },
        "compilationDatabaseDirectory": "build",
        "clang": { 
          "extraArgs": [ "-std=c++20" ] 
        }
      }
    }
  }
}