Skip to content

Commit

Permalink
vimrc: add c/cpp/cuda completefunc for .vim/vimrc
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyinzuo committed Feb 4, 2024
1 parent 62381d7 commit 6eb52d0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
30 changes: 30 additions & 0 deletions root/.vim/autoload/noplug.vim
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,33 @@ function noplug#SystemToQf(args)
cexpr system(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
31 changes: 11 additions & 20 deletions root/.vim/vimrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
set number "显示行号
if has('autocmd') " vim-tiny does not have autocmd
set nocp "no Vi-compatible
set nocp " no Vi-compatible
"""""""""""""""""""""" CONFIG """""""""""""""""""""""""""""""""

if filereadable(expand('~/.vim/config.vim'))
Expand Down Expand Up @@ -349,22 +349,19 @@ if has('autocmd') " vim-tiny does not have autocmd
helptags ~/.vim/doc
endif

" Load plugins, 可能改变g:no_plug的值
if s:has_vimrcd
source ~/.vim/vimrc.d/plugin.vim
endif

if !exists("g:plugs") || !has_key(g:plugs, 'coc.nvim')
if g:no_plug
" https://zhuanlan.zhihu.com/p/106309525
if has("autocmd") && exists("+omnifunc")
if exists("+omnifunc")
autocmd Filetype * if &omnifunc == "" | setlocal omnifunc=syntaxcomplete#Complete | endif
endif
autocmd FileType c,cpp,cuda setlocal completefunc=noplug#MyCppCompleteFunc
endif

""""""""""""""""""""" StatusLine """"""""""""""""""

" 设置状态行-----------------------------------
" 设置状态行显示常用信息
""""""""""""""""" StatusLine """"""""""""""""""
" %F 完整文件路径名
" %m 当前缓冲被修改标记
" %m 当前缓冲只读标记
Expand All @@ -380,7 +377,7 @@ if has('autocmd') " vim-tiny does not have autocmd
" %{...} 评估表达式的值,并用值代替
" %{"[fenc=".(&fenc==""?&enc:&fenc).((exists("+bomb") && &bomb)?"+":"")."]"} 显示文件编码
" %{&ff} 显示文件类型
" 链接:https://zhuanlan.zhihu.com/p/532430825
" See: https://zhuanlan.zhihu.com/p/532430825
set laststatus=2
if !has('nvim') || g:nvim_compatibility_with_vim == 1
set statusline=%1*%F%m%r%h%w\ \|\
Expand Down Expand Up @@ -408,21 +405,19 @@ if has('autocmd') " vim-tiny does not have autocmd
augroup END

""""""""""""""""""" Marks """"""""""""""""""""""

" 为小写字母 a 到 z 创建映射
for mark in range(char2nr('a'), char2nr('z'))
let sign_name = 'mark' . nr2char(mark)
execute 'nnoremap m' . nr2char(mark) . ' m' . nr2char(mark) . ':call marks#SignMark("' . sign_name . '")<CR>'
endfor

" 为大写字母 A 到 Z 创建映射
for mark in range(char2nr('A'), char2nr('Z'))
let sign_name = 'mark' . nr2char(mark)
execute 'nnoremap m' . nr2char(mark) . ' m' . nr2char(mark) . ':call marks#SignMark("' . sign_name . '")<CR>'
endfor
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
""""""""""""""""""" tag system """"""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""

""""""""""""""""""" Tag System """""""""""""""""
" 当前使用.globalrc配置文件
" let $GTAGSCONF = '/root/gtags.conf'

Expand All @@ -431,7 +426,6 @@ if has('autocmd') " vim-tiny does not have autocmd

" 递归向上层寻找tags文件
" set tags=tags;/

if has("cscope")
" use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
set cscopetag
Expand All @@ -450,7 +444,6 @@ if has('autocmd') " vim-tiny does not have autocmd
" when it was compiled. If it wasn't, time to recompile vim...

""""""""""""" My cscope/vim key mappings
"
" The following maps all invoke one of the following cscope search types:
"
" 's' symbol: find all references to the token under cursor
Expand All @@ -477,19 +470,18 @@ if has('autocmd') " vim-tiny does not have autocmd
" 分裂窗口显示搜索结果
" nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR>
" nmap <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>

if g:tag_system == 'cscope'
" Reference: https://cscope.sourceforge.net/cscope_maps.vim
""""""""""""" Standard cscope/vim boilerplate

" reset cscope:
" :cs reset

" add any cscope database in current directory
if filereadable("cscope.out")
" add any cscope database in current directory
cs add cscope.out
" else add the database pointed to by environment variable
elseif $CSCOPE_DB != ""
" else add the database pointed to by environment variable
cs add $CSCOPE_DB
endif

Expand All @@ -501,7 +493,6 @@ if has('autocmd') " vim-tiny does not have autocmd
" 自动加载gtags db
autocmd VimEnter * call tagsystem#GtagsCscopeAdd()
augroup END

" 不使用global官方gtags-cscope vimscript
" https://www.gnu.org/software/global/globaldoc_toc.html#Gtags_002dcscope
" source ~/.vim/vimrc.d/gtags-cscope.vim
Expand Down

0 comments on commit 6eb52d0

Please sign in to comment.