]> git.rmz.io Git - dotfiles.git/blob - vim/ftplugin/cpp.vim
vim: introduce characterize and replace easy align mapping
[dotfiles.git] / vim / ftplugin / cpp.vim
1 setlocal textwidth=100
2
3 " indent 4 spaces
4 setlocal shiftwidth=4
5 setlocal tabstop=4
6 setlocal softtabstop=4
7 setlocal expandtab
8
9 "TODO these are ignored because we set indentexpr in `indent/cpp.vim`
10 " Ideally, indentexpr should read the settings from .clang-format if it exists
11 setlocal nosmartindent
12 setlocal autoindent
13 setlocal cinkeys-=0# " don't reindent PP directives
14 setlocal cinoptions+=:0 " don't indent case labels
15
16 " Load the doxygen syntax
17 let b:load_doxygen_syntax = 1
18
19 augroup fswitch_cpp
20 au!
21 au BufEnter *.h let b:fswitchdst = 'cpp,cc,c'
22 au BufEnter *.h let b:fswitchlocs = '.,reg:|include.*|src|'
23 au BufEnter *.hpp let b:fswitchdst = 'cpp,c'
24 au BufEnter *.hpp let b:fswitchlocs = '.,reg:|include.*|src|'
25 au BufEnter *.cpp let b:fswitchdst = 'hpp,h'
26 au BufEnter *.cpp let b:fswitchlocs = '.,reg:|src|include/**|'
27 augroup END
28
29 let g:switch_custom_definitions =
30 \ [
31 \ ['EXPECT_FALSE', 'EXPECT_TRUE'],
32 \ ['EXPECT_EQ', 'EXPECT_NE'],
33 \ ['EXPECT_LT', 'EXPECT_GT'],
34 \ ['ASSERT_FALSE', 'ASSERT_TRUE'],
35 \ ['ASSERT_EQ', 'ASSERT_NE'],
36 \ ['ASSERT_LT', 'ASSERT_GT']
37 \ ]
38
39 let g:switch_const_definitions =
40 \ [ {
41 \ '\%(const \)\@!\([[:alnum:]_:<>]\{-}\) \%(&\)\@!': 'const \1 ',
42 \ 'const \([[:alnum:]_:<>]\{-}\) &\@!': 'const \1 &' ,
43 \ 'const \([[:alnum:]_:<>]\{-}\) &': '\1 '
44 \ } ]
45
46 nnoremap <buffer> <leader>c :call switch#Switch({'definitions': g:switch_const_definitions})<cr>
47
48 " Disable ale as we use ycm
49 let b:ale_enabled = 0
50
51 nnoremap <buffer> gd :YcmCompleter GoTo<CR>
52 nnoremap <buffer> <C-W>d :vs<CR>:YcmCompleter GoTo<CR>
53 nnoremap <buffer> gD :YcmCompleter GoToReferences<CR>
54
55 nnoremap <buffer> <leader>fi :YcmCompleter FixIt<CR>
56
57 " format with =
58 call operator#user#define_ex_command('ycmcompleter-format', 'YcmCompleter Format')
59 map <buffer> = <Plug>(operator-ycmcompleter-format)
60 vnoremap <buffer> = :YcmCompleter Format<CR>
61 " restore = mapping to g=
62 nnoremap <buffer>g= =
63 vnoremap <buffer>g= =
64
65 " hover popup
66 let g:ycm_auto_hover = ''
67 let b:ycm_hover = { 'command': 'GetDoc', 'syntax': &filetype }
68 nmap <buffer> gh <plug>(YCMHover)
69 nmap <buffer> gH :YcmCompleter GetDoc<CR>
70
71 setlocal errorformat=
72 " TODO I don't remember these errorformat or what they match, maybe CMake? {{{
73 setlocal errorformat+=\ %##%n\ %m\ %f:%l:%c
74 setlocal errorformat+=\ %##%n\ %m\ %f
75 setlocal errorformat+=%n
76 "}}}
77 setlocal errorformat+=%f:%l:%c:\ %trror:\ %m
78 setlocal errorformat+=%f:%l:%c:\ %tarning:\ %m
79 setlocal errorformat+=%D%*\\a[%*\\d]:\ Entering\ directory\ [`']%f'
80 setlocal errorformat+=%X%*\\a[%*\\d]:\ Leaving\ directory\ [`']%f'
81 setlocal errorformat+=%D%*\\a:\ Entering\ directory\ [`']%f'
82 setlocal errorformat+=%X%*\\a:\ Leaving\ directory\ [`']%f'
83 setlocal errorformat+=%*[^[]\[%tRROR\]%m\ \[%f:%l\]
84 setlocal errorformat+=%*[^[]\[%tARNING\]%m\ \[%f:%l\]
85
86 packadd termdebug
87 let g:termdebug_wide = 1
88 function! s:Debug(...)
89 tabedit %
90 let t:debug_tab=1
91 au BufDelete !gdb ++once if exists('t:debug_tab') | tabclose | endif
92
93 "TODO restore mappings
94 "TODO make mappings local to source buffer
95 nnoremap <buffer> <C-T> :call TermDebugSendCommand('advance -source '.expand('%').' -line '.line("."))<CR>
96 nnoremap <buffer> <C-S> :Step<CR>
97 nnoremap <buffer> <C-N> :Over<CR>
98 nnoremap <buffer> <C-F> :Finish<CR>
99 endfunction
100 command! -nargs=* -complete=file -bang Debug call <SID>Debug() | Termdebug<bang> <args>
101 command! -nargs=+ -complete=file -bang DebugCommand call <SID>Debug() | TermdebugCommand<bang> <args>
102
103 setlocal foldmethod=syntax
104 " set foldlevel according to number of matches of 'namespace' and 'class' not
105 " containing ';'
106 function! InitialFoldLevel()
107 let v:errmsg = ""
108 keepjumps keeppatterns silent! 1,/}/s/^\(namespace\|class\) \+[^;]*$//n
109 if v:errmsg == ""
110 let &l:foldlevel=str2nr(split(v:statusmsg)[0])
111 endif
112 endf
113 call InitialFoldLevel()