Skip to content

LanguageClient neovim

Fangrui Song edited this page Sep 14, 2018 · 17 revisions
  1. See Getting started to build the ccls executable
  2. Install LanguageClient-neovim You may use the plugin manager vim-plug

LanguageClient-neovim

nvim +PlugInstall +UpdateRemotePlugins +qa

~/.config/nvim/settings.json (optional, by default it creates cache directory .ccls-cache in the project root)

{
        "initializationOptions": {
                "cacheDirectory": "/tmp/ccls"
        }
}

~/.config/nvim/init.vim

let g:LanguageClient_serverCommands = {
    \ 'cpp': ['ccls', '--log-file=/tmp/cc.log'],
    \ 'c': ['ccls', '--log-file=/tmp/cc.log'],
    \ }

let g:LanguageClient_loadSettings = 1 " Use an absolute configuration path if you want system-wide settings
let g:LanguageClient_settingsPath = '/home/YOUR_USERNAME/.config/nvim/settings.json'

Make sure you have .ccls or compile_commands.json in your project root. Open a file in the project, run :LanguageClientStart.

These features will work out-of-the-box.

nn <silent> <M-j> :call LanguageClient#textDocument_definition()<cr>
nn <silent> <C-,> :call LanguageClient#textDocument_references({'includeDeclaration': v:false})<cr>
nn <silent> K :call LanguageClient#textDocument_hover()<cr>

textDocument/documentHighlight

augroup LanguageClient_config
  au!
  au BufEnter * let b:Plugin_LanguageClient_started = 0
  au User LanguageClientStarted setl signcolumn=yes
  au User LanguageClientStarted let b:Plugin_LanguageClient_started = 1
  au User LanguageClientStopped setl signcolumn=auto
  au User LanguageClientStopped let b:Plugin_LanguageClient_stopped = 0
  au CursorMoved * if b:Plugin_LanguageClient_started | sil call LanguageClient#textDocument_documentHighlight() | endif
augroup END

$ccls/navigate

Semantic navigation. Roughly,

"D" => first child declaration "L" => previous declaration "R" => next declaration "U" => parent declaration

nn <silent> xh :call LanguageClient#findLocations({'method':'$ccls/navigate','direction':'L'})<cr>
nn <silent> xj :call LanguageClient#findLocations({'method':'$ccls/navigate','direction':'D'})<cr>
nn <silent> xk :call LanguageClient#findLocations({'method':'$ccls/navigate','direction':'U'})<cr>
nn <silent> xl :call LanguageClient#findLocations({'method':'$ccls/navigate','direction':'R'})<cr>

Custom cross references

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

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

" $ccls/member
nn <silent> xm :call LanguageClient#findLocations({'method':'$ccls/member'})<cr>

nn xx x

$ccls/inheritance derived:false: base classes/overridden methods/specialized from

$ccls/inheritanceHierarchy derived:false

$ccls/inheritance derived:true

$ccls/inheritanceHierarchy derived:true

$ccls/call: callers/callees of a function

$ccls/callers

$ccls/vars: instances of a type

$ccls/vars

$ccls/member: fields of a struct/class/union/...

$ccls/member

There are also hierarchical views of $ccls/call $ccls/inheritance $ccls/member (see the Emacs page) but they have not been implemented by a Vim plugin.

Clone this wiki locally