Skip to content

Cpp member auto completion

smhc edited this page Sep 3, 2018 · 4 revisions

Vim-mucomplete Configuration

The below configuration suppresses the auto completion popup for all cases except after a '.', '->' or '::' while editing C++ type files. The 'omni' completion type is attempted first, as it is considered the most useful when writing C++ code.

This config attempts to give auto completion only when needed to avoid excessive popups when writing C++ code. Note the completion popup can still be invoked manually using the 'tab'key.

" Remove 'tag' type from c-n completion. Call it explicitly via mucomplete instead.
set complete=.,w,b,u,k
set completeopt=menuone,noselect
" Shut off completion messages
set shortmess+=c


" Require tab to complete, no auto popup
let g:mucomplete#force_manual = 1
" Enable auto completion popup - although it is suppressed by default for all types due to the flag above.
let g:mucomplete#enable_auto_at_startup = 1


" Allow auto popup without tab key for member completion in cpp, also if manually invoked via tab key:
let s:cpp_cond = { t -> (t =~# '\m\(\k\|)\|]\)\%\(->\|::\|\.\)$') || (g:mucomplete_with_key && t =~# '\m\k\k$') }
let g:mucomplete#can_complete.cpp = { 'omni': s:cpp_cond }
let g:mucomplete#chains = {}
let g:mucomplete#chains.default = ['omni', 'c-n', 'path', 'tags']

Omni completion

A plugin providing omni completion is also required. This can be provided by a number of different plugins:

LanguageClient-neovim

An example using clangd and LanguageClient-neovim is provided below:

let g:LanguageClient_serverCommands = {
    \ 'cpp': ['/usr/local/bin/clangd']
    \ }

call plug#begin('~/.vim/plugged')
  Plug 'lifepillar/vim-mucomplete'
  Plug 'autozimu/LanguageClient-neovim', {
    \ 'branch': 'next',
    \ 'do': 'bash install.sh',
    \ }
call plug#end()

OmniCppComplete

An alternative config using a tags file and 'OmniCppComplete'. This is less accurate, but easier to install:

set tags=./tags

call plug#begin('~/.vim/plugged')
  Plug 'lifepillar/vim-mucomplete'
  Plug 'vim-scripts/OmniCppComplete'
call plug#end()