Skip to content

扩展额外的 db 存储路径 #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions autoload/codequery.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ endfunction

function! s:set_db() abort
let path = codequery#db#find_db_path(&filetype)
if empty(path)
if !strlen(path)
echom 'CodeQuery DB Not Found'
return 0
endif
Expand Down Expand Up @@ -79,7 +79,6 @@ function! codequery#run_codequery(args) abort

if args_num == 0
call codequery#query#do_query(cword)

elseif index(g:codequery_subcommands, args[0]) != -1
call codequery#query#set_options(args)
let iword = codequery#query#get_valid_input_word(args)
Expand All @@ -89,7 +88,6 @@ function! codequery#run_codequery(args) abort
call s:restore_cwd()
return
endif

call codequery#query#do_query(word)
else
echom 'Wrong Subcommand !'
Expand All @@ -111,41 +109,58 @@ function! codequery#make_codequery_db(args) abort
continue
endif

let db_path = codequery#db#find_db_path(ft)
if empty(db_path)
if index(g:c_family_filetype_list, ft) != -1
let db_path = 'c_family.db'
let dbs = flatten(add([], split(codequery#db#find_db_path(ft))))
for i in range(len(dbs))
echo i + 1 .. '. ' .. dbs[i]
endfor

let db = ft . '.db'
while len(dbs)
echo 'Tips: (Terminate operation)Quit: Q/q | (Use new file)New: W/w'
let sndb = input('Select File(number): ')
echo ' '
if sndb == 'q' || sndb == 'Q'
return
endif
if sndb == 'w' || sndb == 'W'
if index(g:c_family_filetype_list, ft) != -1
let db = 'c_family.db'
endif
break
elseif sndb > 0 && sndb < len(dbs) + 1
let db = get(dbs, sndb - 1, '')
break
else
let db_path = ft . '.db'
echo 'Invalid Input !'
endif
endif
endwhile

if ft ==? 'python'
let shell_cmd = codequery#db#construct_python_db_build_cmd(db_path)
let shell_cmd = codequery#db#construct_python_db_build_cmd(db)
elseif ft ==? 'javascript'
let shell_cmd = codequery#db#construct_javascript_db_build_cmd(db_path)
let shell_cmd = codequery#db#construct_javascript_db_build_cmd(db)
elseif ft ==? 'ruby'
let shell_cmd = codequery#db#construct_ruby_db_build_cmd(db_path)
let shell_cmd = codequery#db#construct_ruby_db_build_cmd(db)
elseif ft ==? 'go'
let shell_cmd = codequery#db#construct_go_db_build_cmd(db_path)
let shell_cmd = codequery#db#construct_go_db_build_cmd(db)
elseif ft ==? 'java'
let shell_cmd = codequery#db#construct_java_db_build_cmd(db_path)
let shell_cmd = codequery#db#construct_java_db_build_cmd(db)
elseif index(g:c_family_filetype_list, ft) != -1
let shell_cmd = codequery#db#construct_c_db_build_cmd(db_path)
let shell_cmd = codequery#db#construct_c_db_build_cmd(db)
else
echom 'No Command For Building .' . ft . ' file'
continue
endif

if has('nvim')
echom 'Making DB ...'
let mydict = {'db_path': db_path,
let mydict = {'db_path': db,
\'callback': function("codequery#db#make_db_nvim_callback")}
let callbacks = {'on_exit': mydict.callback}
let s:build_job = jobstart(['/bin/sh', '-c', shell_cmd], callbacks)
elseif v:version >= 800
echom 'Making DB ...'
let mydict = {'db_path': db_path,
let mydict = {'db_path': db,
\'callback': function("codequery#db#make_db_callback")}
let options = {'out_io': 'null',
\'exit_cb': mydict.callback}
Expand All @@ -156,7 +171,7 @@ function! codequery#make_codequery_db(args) abort
elseif exists(':Start')
silent execute 'Start! -title=Make_CodeQuery_DB -wait=error ' . shell_cmd
redraw!
echom 'Making ... ' . db_path ' => Run :CodeQueryViewDB to Check Status'
echom 'Making ... ' . db . ' => Run :CodeQueryViewDB to Check Status'
else
silent execute '!' . shell_cmd
redraw!
Expand All @@ -179,17 +194,19 @@ function! codequery#view_codequery_db(args) abort
continue
endif

let db_path = codequery#db#find_db_path(ft)
if empty(db_path)
let db_path = split(codequery#db#find_db_path(ft))
if !len(db_path)
if index(g:c_family_filetype_list, ft) != -1
execute '!echo "\n(c family) DB Not Found"'
else
execute '!echo "\n(' . ft . ') DB Not Found"'
endif
continue
endif

execute '!echo "\n(' . db_path . ') is update at: " && stat -f "\%Sm" ' . db_path
let dbs = flatten(add([], db_path))
for db in dbs
execute '!echo "' . db . ' " $(stat -f "\%Sm" ' . db . ')'
endfor
endfor
call s:restore_cwd()
endfunction
Expand All @@ -210,8 +227,11 @@ function! codequery#move_codequery_db_to_git_hidden_dir(args) abort
endif
let git_root_dir = systemlist('git rev-parse --show-toplevel')[0]
let db_path = codequery#db#find_db_path(ft)
if len(split(db_path)) > 1
db_path = ''
endif

if !v:shell_error && !empty(db_path)
if !v:shell_error && strlen(db_path)
let new_db_path = git_root_dir . '/.git/codequery/' . db_name
call system('mkdir -p ' . git_root_dir . '/.git/codequery/')
call system('mv ' . db_path . ' ' . new_db_path)
Expand Down
27 changes: 27 additions & 0 deletions autoload/codequery/db.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@
" Entries


let g:module_api_path_point = $codequery_base_db_path ? $codequery_base_db_path : expand('$HOME') . '/' . '.doctype'
function! codequery#db#module_found(filetype)
return codequery#db#path_module_found(g:module_api_path_point, '.*' . a:filetype . '\.db')
endfunction

function! codequery#db#path_module_found(basepath, match_rule_matchstr)
let dbs = []
if isdirectory(a:basepath)
let objs = split(globpath(a:basepath, '**'), '\n')
for obj in objs
if strlen(matchstr(obj, '.*' . a:match_rule_matchstr))
call add(dbs, obj)
endif
endfor
endif
return dbs
endfunction


" `lcd` brings side effect !!
function! codequery#db#find_db_path(filetype) abort
if index(g:c_family_filetype_list, a:filetype) != -1
Expand Down Expand Up @@ -30,9 +49,17 @@ function! codequery#db#find_db_path(filetype) abort
if !empty(lookup_path)
execute 'lcd ' . git_root_dir
return lookup_path
else
let cache_paths = codequery#db#module_found(a:filetype)
let lookup_path = join(cache_paths)
if strlen(lookup_path)
execute 'lcd ' . git_root_dir
return lookup_path
endif
endif
endif
endif
return ''
endfunction


Expand Down
25 changes: 19 additions & 6 deletions autoload/codequery/query.vim
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,36 @@ function! s:create_grep_options(word) abort
let pipeline_script_option = ' \| cut -f 2,3'

let grepformat = '%f:%l%m'
let grepprg = 'cqsearch -s ' . g:codequery_db_path . ' -p ' . g:codequery_querytype . ' -t '
\ . word . ' -u ' . fuzzy_option . pipeline_script_option

let grepformat = '%f:%l%m'

let grepprg = ''
let dbs = split(g:codequery_db_path)
for i in range(len(dbs))
let grepprg .= 'cqsearch -s ' . dbs[i] . ' -p ' . g:codequery_querytype . ' -t '
\ . word . ' -u ' . fuzzy_option . (i + 1 == len(dbs) ? '' : ' && ')
endfor

let last_sub_grepprg = pipeline_script_option

if g:codequery_querytype == s:subcmd_map['FileImporter']
let grepprg = 'cqsearch -s ' . g:codequery_db_path . ' -p ' . g:codequery_querytype . ' -t '
\ . word . ' -u ' . fuzzy_option

let last_sub_grepprg = ''

elseif g:codequery_querytype == s:subcmd_map['Callee'] ||
\ g:codequery_querytype == s:subcmd_map['Caller'] ||
\ g:codequery_querytype == s:subcmd_map['Member']
let grepprg = 'cqsearch -s ' . g:codequery_db_path . ' -p ' . g:codequery_querytype . ' -t '
\ . word . ' -u ' . fuzzy_option . ' \| awk ''{ print $2 " " $1 }'''

let last_sub_grepprg = ' \| awk ''{ print $2 " " $1 }'''

elseif g:codequery_querytype == s:subcmd_map['Text']
let grepformat = ''
let grepprg = g:codequery_find_text_cmd . ' ' . a:word
let last_sub_grepprg = ''
endif

let grepprg .= last_sub_grepprg

return [grepformat, grepprg]
endfunction

Expand Down Expand Up @@ -187,6 +199,7 @@ function! codequery#query#do_query(word) abort

" Find Text
if empty(grepformat)
echom "@@"
if g:codequery_find_text_from_current_file_dir == 1
lcd %:p:h
endif
Expand Down