Skip to content

Commit ca43eb4

Browse files
committed
support c family
1 parent 3df52cd commit ca43eb4

File tree

1 file changed

+61
-10
lines changed

1 file changed

+61
-10
lines changed

plugin/codequery.vim

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ let s:subcmd_map = { 'Symbol' : 1,
6767
\ 'DefinitionGroup' : 20 }
6868

6969

70+
let s:c_family_filetype_list =
71+
\ ['c', 'h', 'cpp', 'cxx', 'cc', 'hpp', 'hxx', 'hh']
72+
73+
74+
let s:supported_filetypes = s:c_family_filetype_list +
75+
\ ['python', 'javascript', 'go', 'ruby', 'java', 'c', 'cpp']
76+
77+
7078
function! s:check_filetype(filetype)
71-
let supported_filetypes =
72-
\ ['python', 'javascript', 'go', 'ruby', 'java', 'c', 'cpp']
73-
if index(supported_filetypes, a:filetype) == -1
79+
if index(s:supported_filetypes, a:filetype) == -1
7480
return 0
7581
endif
7682
return 1
@@ -79,7 +85,12 @@ endfunction
7985

8086
" `lcd` brings side effect !!
8187
function! s:find_db_path(filetype)
82-
let db_name = a:filetype . '.db'
88+
if index(s:c_family_filetype_list, a:filetype) != -1
89+
let db_name = 'c_family.db'
90+
else
91+
let db_name = a:filetype . '.db'
92+
endif
93+
8394
let lookup_path = findfile(expand('%:p:h') . '/' . db_name, '.')
8495

8596
if !empty(lookup_path)
@@ -209,6 +220,32 @@ function! s:construct_java_db_build_cmd(db_path)
209220
endfunction
210221

211222

223+
function! s:construct_c_db_build_cmd(db_path)
224+
let find_cmd = 'find . -iname "*.c" > c_cscope.files && ' .
225+
\ 'find . -iname "*.h" >> c_cscope.files && ' .
226+
\ 'find . -iname "*.cpp" >> c_cscope.files && ' .
227+
\ 'find . -iname "*.cxx" >> c_cscope.files && ' .
228+
\ 'find . -iname "*.cc" >> c_cscope.files && ' .
229+
\ 'find . -iname "*.hpp" >> c_cscope.files && ' .
230+
\ 'find . -iname "*.hxx" >> c_cscope.files && ' .
231+
\ 'find . -iname "*.hh" >> c_cscope.files'
232+
let cscope_cmd = 'cscope -cbk -i c_cscope.files -f c_cscope.out'
233+
let ctags_cmd = 'ctags --fields=+i -n -R -f "c_tags" -L c_cscope.files'
234+
let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path . '" -c c_cscope.out' .
235+
\ ' -t c_tags -p'
236+
let shell_cmd = find_cmd . ' && ' .
237+
\ cscope_cmd . ' && ' .
238+
\ ctags_cmd . ' && ' .
239+
\ cqmakedb_cmd
240+
241+
if exists('g:codequery_enable_auto_clean_languages') &&
242+
\ index(g:codequery_enable_auto_clean_languages, 'c') != -1
243+
let shell_cmd .= '&& rm c_cscope.files c_cscope.out c_tags'
244+
endif
245+
246+
return exists('g:codequery_build_c_db_cmd') ? g:codequery_build_c_db_cmd : shell_cmd
247+
endfunction
248+
212249
function! s:is_valid_word(word)
213250
return strlen(matchstr(a:word, '\v^[a-z|A-Z|0-9|_|*|?]+$')) > 0
214251
endfunction
@@ -544,7 +581,11 @@ function! s:make_codequery_db(args)
544581

545582
let db_path = s:find_db_path(ft)
546583
if empty(db_path)
547-
let db_path = ft . '.db'
584+
if index(s:c_family_filetype_list, ft) != -1
585+
let db_path = 'c_family.db'
586+
else
587+
let db_path = ft . '.db'
588+
endif
548589
endif
549590

550591
if ft ==? 'python'
@@ -557,8 +598,10 @@ function! s:make_codequery_db(args)
557598
let shell_cmd = s:construct_go_db_build_cmd(db_path)
558599
elseif ft ==? 'java'
559600
let shell_cmd = s:construct_java_db_build_cmd(db_path)
601+
elseif index(s:c_family_filetype_list, ft) != -1
602+
let shell_cmd = s:construct_c_db_build_cmd(db_path)
560603
else
561-
echom 'No Command For Building ' . ft . ' DB'
604+
echom 'No Command For Building .' . ft . ' file'
562605
continue
563606
endif
564607

@@ -591,7 +634,11 @@ function! s:view_codequery_db(args)
591634

592635
let db_path = s:find_db_path(ft)
593636
if empty(db_path)
594-
execute '!echo "\n(' . ft . ') DB Not Found"'
637+
if index(s:c_family_filetype_list, ft) != -1
638+
execute '!echo "\n(c family) DB Not Found"'
639+
else
640+
execute '!echo "\n(' . ft . ') DB Not Found"'
641+
endif
595642
continue
596643
endif
597644

@@ -607,17 +654,21 @@ function! s:move_codequery_db_to_git_hidden_dir(args)
607654
endif
608655

609656
for ft in args
610-
let db_name = ft . '.db'
657+
if index(s:c_family_filetype_list, ft) != -1
658+
let db_name = 'c_family.db'
659+
else
660+
let db_name = ft . '.db'
661+
endif
611662
let git_root_dir = systemlist('git rev-parse --show-toplevel')[0]
612663
let db_path = s:find_db_path(ft)
613664

614665
if !v:shell_error && !empty(db_path)
615666
let new_db_path = git_root_dir . '/.git/codequery/' . db_name
616667
call system('mkdir -p ' . git_root_dir . '/.git/codequery/')
617668
call system('mv ' . db_path . ' ' . new_db_path)
618-
echom 'Done (' . ft . ')'
669+
echom 'Done (' . db_name . ')'
619670
else
620-
echom 'Git Dir Not Found or (' . ft . ') DB Not Found'
671+
echom 'Git Dir Not Found or (' . db_name . ') Not Found'
621672
endif
622673
endfor
623674
endfunction

0 commit comments

Comments
 (0)