Skip to content

Instantly share code, notes, and snippets.

@nkaretnikov
Last active August 23, 2021 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkaretnikov/cbb619d2f940103656bbbb367f882441 to your computer and use it in GitHub Desktop.
Save nkaretnikov/cbb619d2f940103656bbbb367f882441 to your computer and use it in GitHub Desktop.
vim ccls

notes

Lists the plugins (I use vim-lsp): https://github.com/MaskRay/ccls/wiki/Editor-Configuration

Example shortcuts and a list of commands: https://github.com/prabirshrestha/vim-lsp

Install plug first: https://github.com/junegunn/vim-plug

The following should work out of the box once you open a *.cc file. I did :LspInstallServer first too. Not sure if it's relevant.

After that, you should be able to look up references (:LspReferences) and get better C++ highlighting.

This probably needs a compile_commands.json file in the project directory too. https://github.com/MaskRay/ccls/wiki/Project-Setup#ninja

.vimrc

" Specify a directory for plugins
" - For Neovim: stdpath('data') . '/plugged'
" - Avoid using standard Vim directory names like 'plugin'
call plug#begin('~/.vim/plugged')

" Make sure you use single quotes
Plug 'prabirshrestha/vim-lsp'
Plug 'mattn/vim-lsp-settings'
Plug 'jackguo380/vim-lsp-cxx-highlight'

" Initialize plugin system
call plug#end()

" Configuration of vim-lsp to use cquery and ccls with vim-lsp

" https://github.com/jackguo380/vim-lsp-cxx-highlight/blob/master/sample-configs/vim-lsp-register.vim
" also see https://github.com/prabirshrestha/vim-lsp/wiki/Servers-cquery
"
" cquery always requires these options
" highlight.enabled = true
" emitInactiveRegions = true
if executable('cquery')
   au User lsp_setup call lsp#register_server({
      \ 'name': 'cquery',
      \ 'cmd': {server_info->['cquery']},
      \ 'root_uri': {server_info->lsp#utils#path_to_uri(lsp#utils#find_nearest_parent_file_directory(lsp#utils#get_buffer_path(), 'compile_commands.json'))},
      \ 'initialization_options': {
      \   'cacheDirectory': '/path/to/cquery/cache',
      \   'highlight': { 'enabled' : v:true },
      \   'emitInactiveRegions': v:true
      \ },
      \ 'whitelist': ['c', 'cpp', 'objc', 'objcpp', 'cc'],
      \ })
endif

" also see https://github.com/prabirshrestha/vim-lsp/wiki/Servers-ccls
"
" highlight.lsRanges = true
" is only necessary if vim doesn't have +byte_offset
if executable('ccls')
   au User lsp_setup call lsp#register_server({
      \ 'name': 'ccls',
      \ 'cmd': {server_info->['ccls']},
      \ 'root_uri': {server_info->lsp#utils#path_to_uri(lsp#utils#find_nearest_parent_file_directory(lsp#utils#get_buffer_path(), 'compile_commands.json'))},
      \ 'initialization_options': {
      \   'highlight': { 'lsRanges' : v:true },
      \ },
      \ 'whitelist': ['c', 'cpp', 'objc', 'objcpp', 'cc'],
      \ })
endif

" set foldmethod=expr
"   \ foldexpr=lsp#ui#vim#folding#foldexpr()
"   \ foldtext=lsp#ui#vim#folding#foldtext()

" The above doesn't allow folding based on syntax.
" The following also supports `if`, `while`, etc.
"
" zc, zo to close/open a fold.
set foldmethod=syntax
set foldlevelstart=20  " open folds automatically

function! s:on_lsp_buffer_enabled() abort
    setlocal omnifunc=lsp#complete
    setlocal signcolumn=yes
    if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif

    " Definitions/declarations.
    nmap <buffer> gd <plug>(lsp-definition)
    nmap <buffer> gD <plug>(lsp-declaration)

    " Quick lookup.
    " XXX: Fix syntax highlight:
    " https://github.com/prabirshrestha/vim-lsp/issues/865
    nmap <buffer> gp <plug>(lsp-peek-definition)
    nmap <buffer> gP <plug>(lsp-peek-declaration)
    nmap <buffer> gh <plug>(lsp-hover)

    " Symbol search.
    " Live updated when typing in the search buffer.
    " c-j/c-k to scroll the quickpick window.
    nmap <buffer> gs <plug>(lsp-workspace-symbol-search)
    nmap <buffer> gS <plug>(lsp-document-symbol-search)

    " References.
    nmap <buffer> gr <plug>(lsp-references)
    nmap <buffer> ]r <plug>(lsp-next-reference)
    nmap <buffer> [r <plug>(lsp-previous-reference)

    " Rename.
    nmap <buffer> gN <plug>(lsp-rename)

    " Scroll in pop-ups with c-f/c-d.
    nnoremap <buffer> <expr><c-f> lsp#scroll(+4)
    nnoremap <buffer> <expr><c-d> lsp#scroll(-4)

    " let g:lsp_format_sync_timeout = 1000
    " autocmd! BufWritePre *.rs,*.go call execute('LspDocumentFormatSync')

    " refer to doc to add more commands
endfunction

augroup lsp_install
    au!
    " call s:on_lsp_buffer_enabled only for languages that has the server registered.
    autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment