Skip to content

Commit

Permalink
refactor: move autocmd Filetype c,cpp ... to after/ftplugin/c.vim
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyinzuo committed Jul 15, 2024
1 parent 5ca0c97 commit 2f60361
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 80 deletions.
16 changes: 7 additions & 9 deletions .vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,10 @@ if has('autocmd') " vim-tiny does not have autocmd
set expandtab tabstop=4 shiftwidth=4 softtabstop=4
command -nargs=1 IndentSize :set tabstop=<args> shiftwidth=<args> softtabstop=<args>
command -nargs=1 IndentSizeLocal :setlocal tabstop=<args> shiftwidth=<args> softtabstop=<args>
augroup equalprg_filetype
autocmd!
" autocmd FileType c,cpp setlocal equalprg=indent
" autocmd FileType c,cpp setlocal equalprg=uncrustify\ -c\ .uncrustify.cfg\ --replace\ --no-backup
" pip3 install sqlformat
autocmd FileType sql setlocal equalprg=sqlformat\ -k\ upper\ -r\ --indent_columns\ -
" apt install shfmt
autocmd FileType sh,bash setlocal equalprg=shfmt
augroup end
" apt install shfmt
autocmd FileType sh,bash setlocal equalprg=shfmt
" autocmd FileType c,cpp setlocal equalprg=indent
" autocmd FileType c,cpp setlocal equalprg=uncrustify\ -c\ .uncrustify.cfg\ --replace\ --no-backup
autocmd FileType c,cpp if expand('%:p') =~ '^/usr/include/\(\(c++\)\|\(\w\+\.h$\)\)' | setlocal tabstop=8 shiftwidth=8 softtabstop=8 | else | setlocal tabstop=2 shiftwidth=2 softtabstop=2 | endif
autocmd FileType cuda,vim,tex,html,sh,zsh,lua,json setlocal tabstop=2 shiftwidth=2 softtabstop=2
if v:version >= 901
Expand Down Expand Up @@ -454,12 +449,15 @@ if has('autocmd') " vim-tiny does not have autocmd
endif
return start + 1
endif

if getline('.') =~# &include
let filetype = &filetype
if filetype == 'cpp' || filetype == 'cuda'
" r! ls -1 /usr/include/c++/13 | awk '{print " \x27"$0"\x27"}' | paste -sd,
let completions = ['algorithm', 'any', 'array', 'atomic', 'backward', 'barrier', 'bit', 'bits', 'bitset', 'cassert', 'ccomplex', 'cctype', 'cerrno', 'cfenv', 'cfloat', 'charconv', 'chrono', 'cinttypes', 'ciso646', 'climits', 'clocale', 'cmath', 'codecvt', 'compare', 'complex', 'complex.h', 'concepts', 'condition_variable', 'coroutine', 'csetjmp', 'csignal', 'cstdalign', 'cstdarg', 'cstdbool', 'cstddef', 'cstdint', 'cstdio', 'cstdlib', 'cstring', 'ctgmath', 'ctime', 'cuchar', 'cwchar', 'cwctype', 'cxxabi.h', 'debug', 'decimal', 'deque', 'exception', 'execution', 'expected', 'experimental', 'ext', 'fenv.h', 'filesystem', 'format', 'forward_list', 'fstream', 'functional', 'future', 'initializer_list', 'iomanip', 'ios', 'iosfwd', 'iostream', 'istream', 'iterator', 'latch', 'limits', 'list', 'locale', 'map', 'math.h', 'memory', 'memory_resource', 'mutex', 'new', 'numbers', 'numeric', 'optional', 'ostream', 'parallel', 'pstl', 'queue', 'random', 'ranges', 'ratio', 'regex', 'scoped_allocator', 'semaphore', 'set', 'shared_mutex', 'source_location', 'span', 'spanstream', 'sstream', 'stack', 'stacktrace', 'stdatomic.h', 'stdexcept', 'stdfloat', 'stdlib.h', 'stop_token', 'streambuf', 'string', 'string_view', 'syncstream', 'system_error', 'tgmath.h', 'thread', 'tr1', 'tr2', 'tuple', 'typeindex', 'typeinfo', 'type_traits', 'unordered_map', 'unordered_set', 'utility', 'valarray', 'variant', 'vector', 'version']
call filter(completions, 'v:val =~ "^" . a:base')
else
let completions = []
endif
" 使用getcompletion()获取文件类型的补全列表
echom a:base
Expand Down
27 changes: 27 additions & 0 deletions root/.vim/after/ftplugin/c.vim
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
setlocal commentstring=//\ %s
" setlocal equalprg=indent
" setlocal equalprg=uncrustify\ -c\ .uncrustify.cfg\ --replace\ --no-backup
if expand('%:p') =~ '^/usr/include/\(\(c++\)\|\(\w\+\.h$\)\)'
setlocal tabstop=8 shiftwidth=8 softtabstop=8
else
setlocal tabstop=2 shiftwidth=2 softtabstop=2
endif

if !has('nvim')
" 搜索的模式是一个正则表达式,用来匹配不是在if, for, while, switch, 和catch后面的左花括号{,因为在C++中函数的定义是以左花括号开始的。
" 需要注意的是,这个搜索模式可能并不完全精确,因为函数的定义还可能包含许多其他的复杂性,比如模板函数,函数指针,宏定义等等。对于一些简单的代码文件,这个方法通常可以工作得很好。
" 这些配置会更好地在代码中快速导航,但如果你需要更精确地识别函数,你可能需要考虑使用一些更高级的插件,比如ctags,cscope等等。

" jump to the previous function
" 向后(b表示backward)搜索一个匹配特定模式的地方,这个模式是一个函数的开始位置。
nnoremap <silent> <buffer> [f :call search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{', "bw")<CR>
" jump to the next function
" 向前(w表示forward)搜索一个匹配特定模式的地方,这个模式是一个函数的开始位置。
nnoremap <silent> <buffer> ]f :call search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{', "w")<CR>
nnoremap <silent> <buffer> [[ :call search('\<class\>\|\<struct\>\|\<enum\>\|\<typedef\>', "bW")<CR>
nnoremap <silent> <buffer> ]] :call search('\<class\>\|\<struct\>\|\<enum\>\|\<typedef\>', "wW")<CR>
setlocal foldmethod=expr foldexpr=mycpp#FoldExpr(v:lnum)
endif

setlocal completefunc=mycpp#CompleteFunc
3 changes: 1 addition & 2 deletions root/.vim/after/ftplugin/cpp.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
command -buffer -nargs=? YDef call cpp#YDef(<f-args>)

setlocal commentstring=//\ %s
runtime! after/ftplugin/c.vim
2 changes: 1 addition & 1 deletion root/.vim/after/ftplugin/cuda.vim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
setlocal commentstring=//\ %s
runtime! after/ftplugin/cpp.vim
3 changes: 3 additions & 0 deletions root/.vim/autoload/mycpp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function! mycpp#FoldExpr(lnum)

return b:cpp_fold_level
endfunction

" VIM学习笔记 自动补全详解(Auto-Completion Detail) - YYQ的文章 - 知乎
" https://zhuanlan.zhihu.com/p/106309525
function! mycpp#CompleteFunc(findstart, base)
if a:findstart
" 确定补全开始的位置
Expand Down
30 changes: 0 additions & 30 deletions root/.vim/autoload/noplug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,3 @@ function noplug#SystemToQf(args)
call setqflist([], 'r', {'title': a:args})
call s:ShowQuickfixListIfNotEmpty()
endfunction

function noplug#MyCppCompleteFunc(findstart, base)
if a:findstart
" 确定补全开始的位置
let line = getline('.')
let start = col('.') - 1
while start > 0 && line[start - 1] =~ '\S'
let start -= 1
endwhile
if line[start] != '<' && line[start] != '"'
return -3
endif
return start + 1
endif

if getline('.') =~# &include
let filetype = &filetype
if filetype == 'cpp' || filetype == 'cuda'
" r! ls -1 /usr/include/c++/13 | awk '{print " \x27"$0"\x27"}' | paste -sd,
let completions = ['algorithm', 'any', 'array', 'atomic', 'backward', 'barrier', 'bit', 'bits', 'bitset', 'cassert', 'ccomplex', 'cctype', 'cerrno', 'cfenv', 'cfloat', 'charconv', 'chrono', 'cinttypes', 'ciso646', 'climits', 'clocale', 'cmath', 'codecvt', 'compare', 'complex', 'complex.h', 'concepts', 'condition_variable', 'coroutine', 'csetjmp', 'csignal', 'cstdalign', 'cstdarg', 'cstdbool', 'cstddef', 'cstdint', 'cstdio', 'cstdlib', 'cstring', 'ctgmath', 'ctime', 'cuchar', 'cwchar', 'cwctype', 'cxxabi.h', 'debug', 'decimal', 'deque', 'exception', 'execution', 'expected', 'experimental', 'ext', 'fenv.h', 'filesystem', 'format', 'forward_list', 'fstream', 'functional', 'future', 'initializer_list', 'iomanip', 'ios', 'iosfwd', 'iostream', 'istream', 'iterator', 'latch', 'limits', 'list', 'locale', 'map', 'math.h', 'memory', 'memory_resource', 'mutex', 'new', 'numbers', 'numeric', 'optional', 'ostream', 'parallel', 'pstl', 'queue', 'random', 'ranges', 'ratio', 'regex', 'scoped_allocator', 'semaphore', 'set', 'shared_mutex', 'source_location', 'span', 'spanstream', 'sstream', 'stack', 'stacktrace', 'stdatomic.h', 'stdexcept', 'stdfloat', 'stdlib.h', 'stop_token', 'streambuf', 'string', 'string_view', 'syncstream', 'system_error', 'tgmath.h', 'thread', 'tr1', 'tr2', 'tuple', 'typeindex', 'typeinfo', 'type_traits', 'unordered_map', 'unordered_set', 'utility', 'valarray', 'variant', 'vector', 'version']
call filter(completions, 'v:val =~ "^" . a:base')
endif
" 使用getcompletion()获取文件类型的补全列表
echom a:base
let completions += getcompletion(a:base . '*.h*', 'file_in_path', 1)
let completions += getcompletion(a:base . '*/$', 'file_in_path', 1)
return completions
endif
return []
endfunction
16 changes: 10 additions & 6 deletions root/.vim/doc/git/git-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ $DOC2/cs/system/software_engineering/tools-open-source/git-github

*conventional-commits*

代码提交版本的备注重要吗? - 知乎 https://www.zhihu.com/question/659970119

https://cbea.ms/git-commit/

|semver|
Expand All @@ -28,12 +30,14 @@ www.conventionalcommits.org
*cog*
cli工具: https://github.com/cocogitto/cocogitto

bash补全(See: https://docs.cocogitto.io/) >
cog generate-completions bash > ~/.local/share/bash-completion/completions/cog
bash补全(See: https://docs.cocogitto.io/)
>bash
cog generate-completions bash > ~/.local/share/bash-completion/completions/cog
<

初始化配置文件 >
cog init
初始化配置文件
>bash
cog init
<

之后可以在cog.toml中添加`tag_prefix = "v"`,用于识别语义化版本 https://semver.org/lang/zh-CN/
Expand Down Expand Up @@ -699,6 +703,6 @@ GitHub会根据提供的邮箱地址自动链接到对应的GitHub账户。
安装: https://github.com/cli/cli/blob/trunk/docs/install_linux.md

pull并检出github某个pr
>
gh pr checkout https://github.com/milvus-io/milvus/pull/33173
>bash
gh pr checkout https://github.com/milvus-io/milvus/pull/33173
<
2 changes: 2 additions & 0 deletions root/.vim/ftplugin/sql.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
" pip3 install sqlformat
setlocal equalprg=sqlformat\ -k\ upper\ -r\ --indent_columns\ -
36 changes: 4 additions & 32 deletions root/.vim/vimrc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ if has('autocmd') " vim-tiny does not have autocmd
let g:vim_plug_dir = get(g:, 'vim_plug_dir', '~/plugged')

let g:coc_data_home = get(g:, 'coc_data_home', '~/coc')

" See Also: https://github.com/neoclide/coc-sources
" All Coc Extensions:
" coc-explorer will kill neovim's intro, quickly type <space> or other key
Expand Down Expand Up @@ -194,16 +193,8 @@ if has('autocmd') " vim-tiny does not have autocmd
set expandtab tabstop=4 shiftwidth=4 softtabstop=4
command -nargs=1 IndentSize :set tabstop=<args> shiftwidth=<args> softtabstop=<args>
command -nargs=1 IndentSizeLocal :setlocal tabstop=<args> shiftwidth=<args> softtabstop=<args>
augroup equalprg_filetype
autocmd!
" autocmd FileType c,cpp setlocal equalprg=indent
" autocmd FileType c,cpp setlocal equalprg=uncrustify\ -c\ .uncrustify.cfg\ --replace\ --no-backup
" pip3 install sqlformat
autocmd FileType sql setlocal equalprg=sqlformat\ -k\ upper\ -r\ --indent_columns\ -
" apt install shfmt
autocmd FileType sh,bash setlocal equalprg=shfmt
augroup end
autocmd FileType c,cpp if expand('%:p') =~ '^/usr/include/\(\(c++\)\|\(\w\+\.h$\)\)' | setlocal tabstop=8 shiftwidth=8 softtabstop=8 | else | setlocal tabstop=2 shiftwidth=2 softtabstop=2 | endif
" apt install shfmt
autocmd FileType sh,bash setlocal equalprg=shfmt
autocmd FileType cuda,vim,tex,html,sh,zsh,lua,json setlocal tabstop=2 shiftwidth=2 softtabstop=2

let g:project_vimrc = '.project.vim'
Expand Down Expand Up @@ -296,17 +287,6 @@ if has('autocmd') " vim-tiny does not have autocmd
augroup jump_to_symbol
autocmd!
" See: https://stackoverflow.com/questions/12128678/vim-go-to-beginning-end-of-next-method
" #gpt4-answer
" jump to the previous function
" 向后(b表示backward)搜索一个匹配特定模式的地方,这个模式是一个函数的开始位置。
autocmd FileType c,cpp nnoremap <silent> <buffer> [f :call search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{', "bw")<CR>
" jump to the next function
" 向前(w表示forward)搜索一个匹配特定模式的地方,这个模式是一个函数的开始位置。
autocmd FileType c,cpp nnoremap <silent> <buffer> ]f :call search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{', "w")<CR>
" 搜索的模式是一个正则表达式,用来匹配不是在if, for, while, switch, 和catch后面的左花括号{,因为在C++中函数的定义是以左花括号开始的。
" 需要注意的是,这个搜索模式可能并不完全精确,因为函数的定义还可能包含许多其他的复杂性,比如模板函数,函数指针,宏定义等等。对于一些简单的代码文件,这个方法通常可以工作得很好。
" 这些配置会更好地在代码中快速导航,但如果你需要更精确地识别函数,你可能需要考虑使用一些更高级的插件,比如ctags,cscope等等。

" Go 语言的函数定义是以 func 关键字开始的,所以我们可以使用这个关键字来搜索函数的开始位置。
" 在这里,我们用 \< 和 \> 来指定词的边界,这样我们就可以准确地匹配func,而不是 func 作为其他词的一部分。bW 和 wW是搜索命令的标志,b 表示向后搜索,w 表示向前搜索,W 表示只匹配整个单词。
autocmd FileType go nnoremap <silent> <buffer> [f :call search('\<func\>', "bW")<CR>
Expand All @@ -319,8 +299,6 @@ if has('autocmd') " vim-tiny does not have autocmd
autocmd FileType rust nnoremap <silent> <buffer> ]f :call search('\<fn\>', "wW")<CR>
autocmd FileType lua nnoremap <silent> <buffer> [f :call search('\<function\>', "bW")<CR>
autocmd FileType lua nnoremap <silent> <buffer> ]f :call search('\<function\>', "wW")<CR>
autocmd FileType c,cpp nnoremap <silent> <buffer> [[ :call search('\<class\>\|\<struct\>\|\<enum\>\|\<typedef\>', "bW")<CR>
autocmd FileType c,cpp nnoremap <silent> <buffer> ]] :call search('\<class\>\|\<struct\>\|\<enum\>\|\<typedef\>', "wW")<CR>
autocmd FileType go nnoremap <silent> <buffer> [[ :call search('\<type\>', "bW")<CR>
autocmd FileType go nnoremap <silent> <buffer> ]] :call search('\<type\>', "wW")<CR>
augroup END
Expand Down Expand Up @@ -386,11 +364,9 @@ if has('autocmd') " vim-tiny does not have autocmd
set foldexpr=nvim_treesitter#foldexpr()
else
set foldmethod=marker
autocmd FileType c,cpp,cuda setlocal foldmethod=expr foldexpr=mycpp#FoldExpr(v:lnum)
autocmd FileType vim,lua,go,sh,json setlocal foldmethod=indent
" zfip折叠当前段落 zf%折叠匹配括号内的内容 zfie折叠\begin{}\end{}之间的内容(vimtex)
autocmd FileType tex setlocal foldmethod=manual
autocmd FileType c,cpp,cuda setlocal completefunc=mycpp#CompleteFunc
" zpr mapping https://www.zhihu.com/question/30782510/answer/70078216,
" 可以用:vim /keyword/ % 替代
" 打开所有折叠: zR
Expand Down Expand Up @@ -423,12 +399,8 @@ if has('autocmd') " vim-tiny does not have autocmd
if exists("+omnifunc")
autocmd Filetype * if &omnifunc == "" | setlocal omnifunc=syntaxcomplete#Complete | endif
endif
if g:no_plug
" https://zhuanlan.zhihu.com/p/106309525
autocmd FileType c,cpp,cuda setlocal completefunc=noplug#MyCppCompleteFunc
if has("patch-7.4.1649")
packadd! matchit
endif
if g:no_plug && has("patch-7.4.1649")
packadd! matchit
endif

""""""""""""""""" StatusLine """"""""""""""""""
Expand Down

0 comments on commit 2f60361

Please sign in to comment.