-setlocal foldmethod=syntax
-
setlocal textwidth=100
" indent 4 spaces
setlocal softtabstop=4
setlocal expandtab
+"TODO these are ignored because we set indentexpr in `indent/cpp.vim`
+" Ideally, indentexpr should read the settings from .clang-format if it exists
setlocal nosmartindent
setlocal autoindent
setlocal cinkeys-=0# " don't reindent PP directives
setlocal cinoptions+=:0 " don't indent case labels
-augroup fswitch
- au BufEnter *.h let b:fswitchdst = 'cpp,c'
- au BufEnter *.h let b:fswitchlocs = 'reg:/include/source/'
+" Load the doxygen syntax
+let b:load_doxygen_syntax = 1
+
+augroup fswitch_cpp
+ au!
+ au BufEnter *.h let b:fswitchdst = 'cpp,cc,c'
+ au BufEnter *.h let b:fswitchlocs = '.,reg:|include.*|src|'
+ au BufEnter *.hpp let b:fswitchdst = 'cpp,c'
+ au BufEnter *.hpp let b:fswitchlocs = '.,reg:|include.*|src|'
+ au BufEnter *.cpp let b:fswitchdst = 'hpp,h'
+ au BufEnter *.cpp let b:fswitchlocs = '.,reg:|src|include/**|'
augroup END
+
+let g:switch_custom_definitions =
+ \ [
+ \ ['EXPECT_FALSE', 'EXPECT_TRUE'],
+ \ ['EXPECT_EQ', 'EXPECT_NE'],
+ \ ['EXPECT_LT', 'EXPECT_GT'],
+ \ ['ASSERT_FALSE', 'ASSERT_TRUE'],
+ \ ['ASSERT_EQ', 'ASSERT_NE'],
+ \ ['ASSERT_LT', 'ASSERT_GT']
+ \ ]
+
+let g:switch_const_definitions =
+ \ [ {
+ \ '\%(const \)\@!\([[:alnum:]_:<>]\{-}\) \%(&\)\@!': 'const \1 ',
+ \ 'const \([[:alnum:]_:<>]\{-}\) &\@!': 'const \1 &' ,
+ \ 'const \([[:alnum:]_:<>]\{-}\) &': '\1 '
+ \ } ]
+
+nnoremap <buffer> <leader>c :call switch#Switch({'definitions': g:switch_const_definitions})<cr>
+
+" Disable ale as we use ycm
+let b:ale_enabled = 0
+
+nnoremap <buffer> gd :YcmCompleter GoTo<CR>
+nnoremap <buffer> <C-W>d :vs<CR>:YcmCompleter GoTo<CR>
+nnoremap <buffer> gD :YcmCompleter GoToReferences<CR>
+
+nnoremap <buffer> <leader>fi :YcmCompleter FixIt<CR>
+nnoremap <buffer> <leader>fr :YcmCompleter RefactorRename<space>
+
+" format with =
+call operator#user#define_ex_command('ycmcompleter-format', 'YcmCompleter Format')
+map <buffer> = <Plug>(operator-ycmcompleter-format)
+vnoremap <buffer> = :YcmCompleter Format<CR>
+" restore = mapping to g=
+nnoremap <buffer>g= =
+vnoremap <buffer>g= =
+
+" hover popup
+let g:ycm_auto_hover = ''
+let b:ycm_hover = { 'command': 'GetDoc', 'syntax': &filetype }
+nmap <buffer> gh <plug>(YCMHover)
+nmap <buffer> gH :YcmCompleter GetDoc<CR>
+
+setlocal errorformat=
+" TODO I don't remember these errorformat or what they match, maybe CMake? {{{
+setlocal errorformat+=\ %##%n\ %m\ %f:%l:%c
+setlocal errorformat+=\ %##%n\ %m\ %f
+setlocal errorformat+=%n
+"}}}
+setlocal errorformat+=%f:%l:%c:\ %trror:\ %m
+setlocal errorformat+=%f:%l:%c:\ %tarning:\ %m
+setlocal errorformat+=%D%*\\a[%*\\d]:\ Entering\ directory\ [`']%f'
+setlocal errorformat+=%X%*\\a[%*\\d]:\ Leaving\ directory\ [`']%f'
+setlocal errorformat+=%D%*\\a:\ Entering\ directory\ [`']%f'
+setlocal errorformat+=%X%*\\a:\ Leaving\ directory\ [`']%f'
+setlocal errorformat+=%*[^[]\[%tRROR\]%m\ \[%f:%l\]
+setlocal errorformat+=%*[^[]\[%tARNING\]%m\ \[%f:%l\]
+
+packadd termdebug
+let g:termdebug_wide = 1
+function! s:Debug(...)
+ tabedit %
+ let t:debug_tab=1
+ au BufDelete !gdb ++once if exists('t:debug_tab') | tabclose | endif
+
+ "TODO restore mappings
+ "TODO make mappings local to source buffer
+ nnoremap <buffer> <C-T> :call TermDebugSendCommand('advance -source '.expand('%').' -line '.line("."))<CR>
+ nnoremap <buffer> <C-S> :Step<CR>
+ nnoremap <buffer> <C-N> :Over<CR>
+ nnoremap <buffer> <C-F> :Finish<CR>
+endfunction
+command! -nargs=* -complete=file -bang Debug call <SID>Debug() | Termdebug<bang> <args>
+command! -nargs=+ -complete=file -bang DebugCommand call <SID>Debug() | TermdebugCommand<bang> <args>
+
+setlocal foldmethod=syntax
+" set foldlevel according to number of matches of 'namespace' and 'class' not
+" containing ';'
+function! InitialFoldLevel()
+ let v:errmsg = ""
+ keepjumps keeppatterns silent! 1,/}/s/^\(namespace\|class\) \+[^;]*$//n
+ if v:errmsg == ""
+ let &l:foldlevel=str2nr(split(v:statusmsg)[0])
+ endif
+endf
+call InitialFoldLevel()