diff --git a/autoload/codequery.vim b/autoload/codequery.vim deleted file mode 100644 index 973c6ff..0000000 --- a/autoload/codequery.vim +++ /dev/null @@ -1,281 +0,0 @@ -" ============================================================================= -" Helpers - - -let g:c_family_filetype_list = - \ ['c', 'h', 'cpp', 'cxx', 'cc', 'hpp', 'hxx', 'hh'] - - -let g:codequery_supported_filetype_list = g:c_family_filetype_list + - \ ['python', 'javascript', 'go', 'ruby', 'java', 'c', 'cpp'] - - -let s:menu_subcommands = [ 'Unite' ] - - -function! s:check_filetype(filetype) abort - if index(g:codequery_supported_filetype_list, a:filetype) == -1 - return 0 - endif - return 1 -endfunction - - -function! s:set_db() abort - let path = codequery#db#find_db_path(&filetype) - if empty(path) - echom 'CodeQuery DB Not Found' - return 0 - endif - - let g:codequery_db_path = path - return 1 -endfunction - - -function! s:save_cwd() abort - let g:codequery_cwd = getcwd() -endfunction - - -function! s:restore_cwd() - execute 'lcd ' . g:codequery_cwd - let g:codequery_cwd = '' -endfunction - - - -" ============================================================================= -" Entries - - -function! codequery#run_codequery(args) abort - call s:save_cwd() - if !s:check_filetype(&filetype) - echom 'Not Supported Filetype: ' . &filetype - if g:codequery_auto_switch_to_find_text_for_wrong_filetype - silent execute g:codequery_find_text_cmd - call codequery#query#prettify_qf_layout_and_map_keys(getqflist()) - endif - return - endif - - let g:codequery_last_query_word = '' - let g:codequery_fuzzy = 0 - let g:codequery_append_to_qf = 0 - let g:codequery_querytype = 1 - let g:codequery_db_path = '' - if !s:set_db() - call s:restore_cwd() - if g:codequery_trigger_build_db_when_db_not_found - execute 'CodeQueryMakeDB ' . &filetype - endif - return - endif - - let args = split(a:args, ' ') - let args_num = len(args) - let cword = codequery#query#get_valid_cursor_word() - - 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) - let word = codequery#query#get_final_query_word(iword, cword) - if empty(word) - echom 'Invalid Args: ' . a:args - call s:restore_cwd() - return - endif - - call codequery#query#do_query(word) - else - echom 'Wrong Subcommand !' - endif - call s:restore_cwd() -endfunction - - -function! codequery#make_codequery_db(args) abort - call s:save_cwd() - let args = split(a:args, ' ') - if empty(args) - let args = [&filetype] - endif - - for ft in args - if !s:check_filetype(ft) - echom 'Not Supported Filetype: ' . ft - 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' - else - let db_path = ft . '.db' - endif - endif - - if ft ==? 'python' - let shell_cmd = codequery#db#construct_python_db_build_cmd(db_path) - elseif ft ==? 'javascript' - let shell_cmd = codequery#db#construct_javascript_db_build_cmd(db_path) - elseif ft ==? 'ruby' - let shell_cmd = codequery#db#construct_ruby_db_build_cmd(db_path) - elseif ft ==? 'go' - let shell_cmd = codequery#db#construct_go_db_build_cmd(db_path) - elseif ft ==? 'java' - let shell_cmd = codequery#db#construct_java_db_build_cmd(db_path) - elseif index(g:c_family_filetype_list, ft) != -1 - let shell_cmd = codequery#db#construct_c_db_build_cmd(db_path) - else - echom 'No Command For Building .' . ft . ' file' - continue - endif - - if v:version >= 800 - echom 'Making DB ...' - let mydict = {'db_path': db_path, - \'callback': function("codequery#db#make_db_callback")} - let options = {'out_io': 'null', - \'exit_cb': mydict.callback} - let s:build_job = job_start(['/bin/sh', '-c', shell_cmd], options) - let timer = timer_start(500, - \{-> execute("call job_status(s:build_job)","")}, - \{'repeat': 60}) - elseif exists(':Start') - silent execute 'Start! -title=Make_CodeQuery_DB -wait=error ' . shell_cmd - redraw! - echom 'Making ... ' . db_path ' => Run :CodeQueryViewDB to Check Status' - else - silent execute '!' . shell_cmd - redraw! - endif - endfor - call s:restore_cwd() -endfunction - - -function! codequery#view_codequery_db(args) abort - call s:save_cwd() - let args = split(a:args, ' ') - if empty(args) - let args = [&filetype] - endif - - for ft in args - if !s:check_filetype(ft) - echom 'Not Supported Filetype: ' . ft - continue - endif - - let db_path = codequery#db#find_db_path(ft) - if empty(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 - endfor - call s:restore_cwd() -endfunction - - -function! codequery#move_codequery_db_to_git_hidden_dir(args) abort - call s:save_cwd() - let args = split(a:args, ' ') - if empty(args) - let args = [&filetype] - endif - - for ft in args - if index(g:c_family_filetype_list, ft) != -1 - let db_name = 'c_family.db' - else - let db_name = ft . '.db' - endif - let git_root_dir = systemlist('git rev-parse --show-toplevel')[0] - let db_path = codequery#db#find_db_path(ft) - - if !v:shell_error && !empty(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) - echom 'Done (' . db_name . ')' - else - echom 'Git Dir Not Found or (' . db_name . ') Not Found' - endif - endfor - call s:restore_cwd() -endfunction - - -function! codequery#show_menu(args) abort - let args = split(a:args, ' ') - let args_num = len(args) - - if args_num > 0 && index(s:menu_subcommands, args[0]) != -1 - if args[0] ==# 'Unite' - if args_num > 1 && args[1] ==# 'Magic' - let magic_menu = 1 - else - let magic_menu = 0 - endif - call codequery#menu#use_unite_menu(magic_menu) - return - endif - endif - - echom 'Wrong Subcommands! Try: ' . join(s:menu_subcommands, ', ') -endfunction - - -function! codequery#run_codequery_again_with_different_subcmd(args) abort - let args = split(a:args, ' ') - let args_num = len(args) - if !empty(g:codequery_last_query_word) && args_num > 0 - cclose - let again_cmd = 'CodeQuery ' . args[0] . ' ' . g:codequery_last_query_word . ' ' - \ . (g:last_query_fuzzy ? '-f' : '') - execute again_cmd - else - echom 'Wrong Subcommands!' - endif -endfunction - - -" modify from someone's .vimrc -function! codequery#filter_qf_results(args) abort - let args = split(a:args, ' ') - if len(args) > 1 - let query = args[1] - let reverse_filter = 1 - else - let query = args[0] - let reverse_filter = 0 - endif - echom query - - let results = getqflist() - for d in results - if reverse_filter - if bufname(d['bufnr']) =~ query || d['text'] =~ query - call remove(results, index(results, d)) - endif - else - if bufname(d['bufnr']) !~ query && d['text'] !~ query - call remove(results, index(results, d)) - endif - endif - endfor - call setqflist(results) - call codequery#query#prettify_qf_layout_and_map_keys(results) -endfunction diff --git a/autoload/codequery/db.vim b/autoload/codequery/db.vim deleted file mode 100644 index 3801e69..0000000 --- a/autoload/codequery/db.vim +++ /dev/null @@ -1,159 +0,0 @@ -" ============================================================================= -" Entries - - -" `lcd` brings side effect !! -function! codequery#db#find_db_path(filetype) abort - if index(g:c_family_filetype_list, a:filetype) != -1 - let db_name = 'c_family.db' - else - let db_name = a:filetype . '.db' - endif - - let lookup_path = findfile(expand('%:p:h') . '/' . db_name, '.') - - if !empty(lookup_path) - lcd %:p:h - return lookup_path - endif - - lcd %:p:h - let git_root_dir = systemlist('git rev-parse --show-toplevel')[0] - if !v:shell_error - let lookup_path = findfile(git_root_dir . '/' . db_name, '.') - if !empty(lookup_path) - execute 'lcd ' . git_root_dir - return lookup_path - else - let lookup_path = findfile(git_root_dir . '/.git/codequery/' . - \ db_name, '.') - if !empty(lookup_path) - execute 'lcd ' . git_root_dir - return lookup_path - endif - endif - endif -endfunction - - -function! codequery#db#make_db_callback(job, status) dict - echom 'Done! (' . self.db_path . ')' -endfunction - - -function! codequery#db#construct_python_db_build_cmd(db_path) abort - let find_cmd = 'find . -iname "*.py" > python_cscope.files' - let pycscope_cmd = 'pycscope -f "python_cscope.out" -i python_cscope.files' - let ctags_cmd = 'ctags --fields=+i -n -R -f "python_tags" -L python_cscope.files' - let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path . '" -c python_cscope.out' . - \ ' -t python_tags -p' - let shell_cmd = find_cmd . ' && ' . - \ pycscope_cmd . ' && ' . - \ ctags_cmd . ' && ' . - \ cqmakedb_cmd - - if exists('g:codequery_enable_auto_clean_languages') && - \ index(g:codequery_enable_auto_clean_languages, 'python') != -1 - let shell_cmd .= '&& rm python_cscope.files python_cscope.out python_tags' - endif - - return exists('g:codequery_build_python_db_cmd') ? g:codequery_build_python_db_cmd : shell_cmd -endfunction - - -function! codequery#db#construct_javascript_db_build_cmd(db_path) abort - let starscope_cmd = 'starscope --force-update -e ctags -e cscope **/*.js' - let rename_cmd = 'mv tags javascript_tags && mv cscope.out javascript_cscope.out' - let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path . - \ '" -c ./javascript_cscope.out -t ./javascript_tags -p' - - let shell_cmd = starscope_cmd . ' && ' . rename_cmd . ' && ' . cqmakedb_cmd - - if exists('g:codequery_enable_auto_clean_languages') && - \ index(g:codequery_enable_auto_clean_languages, 'javascript') != -1 - let shell_cmd .= ' && rm javascript_cscope.out javascript_tags .starscope.db' - endif - - return exists('g:codequery_build_javascript_db_cmd') ? g:codequery_build_javascript_db_cmd : shell_cmd -endfunction - - -function! codequery#db#construct_ruby_db_build_cmd(db_path) abort - let starscope_cmd = 'starscope --force-update -e ctags -e cscope **/*.rb' - let rename_cmd = 'mv tags ruby_tags && mv cscope.out ruby_cscope.out' - let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path . - \ '" -c ./ruby_cscope.out -t ./ruby_tags -p' - - let shell_cmd = starscope_cmd . ' && ' . rename_cmd . ' && ' . cqmakedb_cmd - - if exists('g:codequery_enable_auto_clean_languages') && - \ index(g:codequery_enable_auto_clean_languages, 'ruby') != -1 - let shell_cmd .= ' && rm ruby_cscope.out ruby_tags .starscope.db' - endif - - return exists('g:codequery_build_ruby_db_cmd') ? g:codequery_build_ruby_db_cmd : shell_cmd -endfunction - - -function! codequery#db#construct_go_db_build_cmd(db_path) abort - let starscope_cmd = 'starscope --force-update -e ctags -e cscope **/*.go' - let rename_cmd = 'mv tags go_tags && mv cscope.out go_cscope.out' - let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path . - \ '" -c ./go_cscope.out -t ./go_tags -p' - - let shell_cmd = starscope_cmd . ' && ' . rename_cmd . ' && ' . cqmakedb_cmd - - if exists('g:codequery_enable_auto_clean_languages') && - \ index(g:codequery_enable_auto_clean_languages, 'go') != -1 - let shell_cmd .= ' && rm go_cscope.out go_tags .starscope.db' - endif - - return exists('g:codequery_build_go_db_cmd') ? g:codequery_build_go_db_cmd : shell_cmd -endfunction - - -function! codequery#db#construct_java_db_build_cmd(db_path) abort - let find_cmd = 'find . -iname "*.java" > java_cscope.files' - let cscope_cmd = 'cscope -cbR -i java_cscope.files -f java_cscope.out' - let ctags_cmd = 'ctags --fields=+i -n -R -f "java_tags" -L java_cscope.files' - let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path . '" -c java_cscope.out' . - \ ' -t java_tags -p' - let shell_cmd = find_cmd . ' && ' . - \ cscope_cmd . ' && ' . - \ ctags_cmd . ' && ' . - \ cqmakedb_cmd - - if exists('g:codequery_enable_auto_clean_languages') && - \ index(g:codequery_enable_auto_clean_languages, 'java') != -1 - let shell_cmd .= '&& rm java_cscope.files java_cscope.out java_tags' - endif - - return exists('g:codequery_build_java_db_cmd') ? g:codequery_build_java_db_cmd : shell_cmd -endfunction - - -function! codequery#db#construct_c_db_build_cmd(db_path) abort - let find_cmd = 'find . -iname "*.c" > c_cscope.files && ' . - \ 'find . -iname "*.h" >> c_cscope.files && ' . - \ 'find . -iname "*.cpp" >> c_cscope.files && ' . - \ 'find . -iname "*.cxx" >> c_cscope.files && ' . - \ 'find . -iname "*.cc" >> c_cscope.files && ' . - \ 'find . -iname "*.hpp" >> c_cscope.files && ' . - \ 'find . -iname "*.hxx" >> c_cscope.files && ' . - \ 'find . -iname "*.hh" >> c_cscope.files' - let cscope_cmd = 'cscope -cbk -i c_cscope.files -f c_cscope.out' - let ctags_cmd = 'ctags --fields=+i -n -R -f "c_tags" -L c_cscope.files' - let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path . '" -c c_cscope.out' . - \ ' -t c_tags -p' - let shell_cmd = find_cmd . ' && ' . - \ cscope_cmd . ' && ' . - \ ctags_cmd . ' && ' . - \ cqmakedb_cmd - - if exists('g:codequery_enable_auto_clean_languages') && - \ index(g:codequery_enable_auto_clean_languages, 'c') != -1 - let shell_cmd .= '&& rm c_cscope.files c_cscope.out c_tags' - endif - - return exists('g:codequery_build_c_db_cmd') ? g:codequery_build_c_db_cmd : shell_cmd -endfunction diff --git a/autoload/codequery/menu.vim b/autoload/codequery/menu.vim deleted file mode 100644 index edfc1bc..0000000 --- a/autoload/codequery/menu.vim +++ /dev/null @@ -1,118 +0,0 @@ -" ============================================================================= -" Entries - - -scriptencoding utf-8 - - -function! codequery#menu#patch_unite_magic_menu_from_qf(fre_cmds, fun_cmds, cla_cmds) abort - call map(a:fre_cmds, '[substitute(v:val[0], "Find", "Switch To", "g"), v:val[1]]') - call map(a:fun_cmds, '[substitute(v:val[0], "Find", "Switch To", "g"), v:val[1]]') - call map(a:cla_cmds, '[substitute(v:val[0], "Find", "Switch To", "g"), v:val[1]]') - call map(a:fre_cmds, '[v:val[0], substitute(v:val[1], "CodeQuery", "CodeQueryAgain", "")]') - call map(a:fun_cmds, '[v:val[0], substitute(v:val[1], "CodeQuery", "CodeQueryAgain", "")]') - call map(a:cla_cmds, '[v:val[0], substitute(v:val[1], "CodeQuery", "CodeQueryAgain", "")]') - call insert(a:fre_cmds, ['▷ Filter', 'call feedkeys(":CodeQueryFilter ")'], 0) -endfunction - - - -function! codequery#menu#use_unite_menu(magic) abort - let cword = codequery#query#get_valid_cursor_word() - let menu_frequent_cmds = [['▷ Find Symbol', 'CodeQuery Symbol'], - \['▷ Find Text', 'CodeQuery Text']] - let menu_function_cmds = [['▷ Find Function Def. [F]', 'CodeQuery Definition'], - \['▷ Find Call [F]', 'CodeQuery Call'], - \['▷ Find Caller [F]', 'CodeQuery Caller'], - \['▷ Find Callee [F]', 'CodeQuery Callee']] - let menu_class_cmds = [['▷ Find Class Def. [C]', 'CodeQuery Class'], - \['▷ Find Class Member [C]', 'CodeQuery Member'], - \['▷ Find Parent [C]', 'CodeQuery Parent'], - \['▷ Find Child [C]', 'CodeQuery Child']] - let menu_other_cmds = [['▷ List Function', 'CodeQuery FunctionList'], - \['▷ List Imports', 'CodeQuery FileImporter']] - let menu_delimiter = [['* ------------------------- *', '']] - let menu_db_cmds = [['▷ Make DB', 'call feedkeys(":CodeQueryMakeDB ")'], - \['▷ View DB', 'call feedkeys(":CodeQueryViewDB ")'], - \['▷ Move DB', 'call feedkeys(":CodeQueryMoveDBToGitDir ")']] - let menu_show_qf = [['▷ Show QF ▲', 'CodeQueryShowQF'], - \['▷ Hide QF ▼', 'cclose']] - let menu_goto_magic = [['▷ Open Magic Menu ▸', 'CodeQueryMenu Unite Magic']] - let menu_goto_full = [['▷ Open Full Menu ▸', 'CodeQueryMenu Unite Full']] - - " DB not found => remove unnecessary items from menu - let db_path = codequery#db#find_db_path(&filetype) - if empty(db_path) && &filetype !=# 'qf' - let menu_frequent_cmds = [['▷ Find Text', g:codequery_find_text_cmd]] - let menu_function_cmds = [] - let menu_class_cmds = [] - let menu_other_cmds = [] - let menu_delimiter = [] - let menu_db_cmds = [['▷ Make DB', 'call feedkeys(":CodeQueryMakeDB ' . &filetype . '")']] - let menu_goto_magic = [] - let menu_goto_full = [] - if index(g:codequery_supported_filetype_list, &filetype) == -1 - let menu_show_qf += [['# Not Supported Filetype: [' . &filetype . ']', '']] - endif - endif - - if a:magic - if &filetype ==# 'qf' - call codequery#menu#patch_unite_magic_menu_from_qf(menu_frequent_cmds, - \ menu_function_cmds, - \ menu_class_cmds) - let menu_other_cmds = [] - let menu_goto_full = [] - if exists('g:codequery_last_query_word') - let cword = g:codequery_last_query_word - endif - endif - - let menu_description = 'CodeQuery Magic Menu' - if g:codequery_enable_not_so_magic_menu - let cmd_candidates = menu_frequent_cmds - \ + menu_function_cmds - \ + menu_class_cmds - \ + menu_other_cmds - \ + menu_show_qf - \ + menu_goto_full - elseif cword =~# '\C^[A-Z].*' - let cmd_candidates = menu_frequent_cmds - \ + menu_class_cmds - \ + menu_other_cmds - \ + menu_show_qf - \ + menu_goto_full - else - let cmd_candidates = menu_frequent_cmds - \ + menu_function_cmds - \ + menu_other_cmds - \ + menu_show_qf - \ + menu_goto_full - endif - else - if &filetype ==# 'qf' - echom 'Can Not Open Full Menu In QF (Use Magic Menu)' - return - endif - let menu_description = 'CodeQuery Full Menu' - let cmd_candidates = menu_frequent_cmds - \ + menu_function_cmds - \ + menu_class_cmds - \ + menu_other_cmds - \ + menu_delimiter - \ + menu_db_cmds - \ + menu_delimiter - \ + menu_show_qf - \ + menu_goto_magic - endif - - if !exists('g:unite_source_menu_menus') - let g:unite_source_menu_menus = {} - endif - let g:unite_source_menu_menus.codequery = { - \ 'description' : menu_description, - \} - let g:unite_source_menu_menus.codequery.command_candidates = cmd_candidates - execute 'Unite -silent -prompt-visible -prompt=::' . cword - \ . ':: menu:codequery' -endfunction diff --git a/autoload/codequery/query.vim b/autoload/codequery/query.vim deleted file mode 100644 index 7c8cf24..0000000 --- a/autoload/codequery/query.vim +++ /dev/null @@ -1,268 +0,0 @@ -" ============================================================================= -" Helpers - - -let s:subcmd_map = { 'Symbol' : 1, - \ 'Definition' : 2, - \ 'Caller' : 6, - \ 'Callee' : 7, - \ 'Call' : 8, - \ 'Class' : 3, - \ 'Member' : 9, - \ 'Parent' : 12, - \ 'Child' : 11, - \ 'FunctionList' : 13, - \ 'FileImporter' : 4, - \ 'Text' : 21 } - - -function! s:create_grep_options(word) abort - if g:codequery_fuzzy - let fuzzy_option = '-f' - let word = '"' . a:word . '"' - else - let fuzzy_option = '-e' - let word = a:word - endif - - 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 - - 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 - - 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 }''' - - elseif g:codequery_querytype == s:subcmd_map['Text'] - let grepformat = '' - let grepprg = g:codequery_find_text_cmd . ' ' . a:word - endif - - return [grepformat, grepprg] -endfunction - - -function! s:show_result(word) abort - let results = getqflist() - call codequery#query#prettify_qf_layout_and_map_keys(results) - if !empty(results) - echom 'Found ' . len(results) . ' result' . (len(results) > 1 ? 's' : '') - \. ' /' . a:word . '/' - else - echom 'Result Not Found /' . a:word . '/' - endif -endfunction - - - -" ============================================================================= -" Entries - - -function! codequery#query#is_valid_word(word) abort - return strlen(matchstr(a:word, '\v^[a-z|A-Z|0-9|_|*|?]+$')) > 0 -endfunction - - -function! codequery#query#get_valid_cursor_word() abort - return codequery#query#is_valid_word(expand('')) ? expand('') : '' -endfunction - - -function! codequery#query#get_valid_input_word(args) abort - let args = deepcopy(a:args) - if g:codequery_fuzzy - call remove(args, index(args, '-f')) - endif - if g:codequery_append_to_qf - call remove(args, index(args, '-a')) - endif - - if len(args) <= 1 - return '' - endif - - return codequery#query#is_valid_word(args[1]) ? args[1] : '' -endfunction - - -function! codequery#query#get_final_query_word(iword, cword) abort - if empty(a:iword) && g:codequery_querytype == s:subcmd_map['FunctionList'] - return expand('%') - elseif empty(a:iword) && g:codequery_querytype == s:subcmd_map['FileImporter'] - return expand('%:r') - elseif empty(a:iword) && !empty(a:cword) - return a:cword - elseif empty(a:iword) - return '' - else - return a:iword - endif -endfunction - - -" Ref: MarcWeber's vim-addon-qf-layout -function! codequery#query#prettify_qf_layout_and_map_keys(results) abort - if &filetype !=# 'qf' - if !empty(g:codequery_cwd) - execute 'lcd ' . g:codequery_cwd - endif - copen - execute 'lcd ' . g:codequery_cwd - endif - - " unlock qf to make changes - setlocal modifiable - setlocal nolist - setlocal nowrap - - " delete all the text in qf - silent %delete - - " insert new text with pretty layout - let max_fn_len = 0 - let max_lnum_len = 0 - for d in a:results - let d['filename'] = bufname(d['bufnr']) - let max_fn_len = max([max_fn_len, len(d['filename'])]) - let max_lnum_len = max([max_lnum_len, len(d['lnum'])]) - endfor - let reasonable_max_len = 60 - let max_fn_len = min([max_fn_len, reasonable_max_len]) - let qf_format = '"%-' . max_fn_len . 'S | %' . max_lnum_len . 'S | %s"' - let evaluating_str = 'printf(' . qf_format . - \ ', v:val["filename"], v:val["lnum"], v:val["text"])' - call append('0', map(a:results, evaluating_str)) - - " delete empty line - global/^$/delete - - " put the cursor back - normal! gg - - " map shortcuts - if !g:codequery_disable_qf_key_bindings - nnoremap s :CodeQueryAgain Symbol - nnoremap x :CodeQueryAgain Text - nnoremap c :CodeQueryAgain Call - nnoremap r :CodeQueryAgain Caller - nnoremap e :CodeQueryAgain Callee - nnoremap d :CodeQueryAgain Definition - nnoremap C :CodeQueryAgain Class - nnoremap M :CodeQueryAgain Member - nnoremap P :CodeQueryAgain Parent - nnoremap D :CodeQueryAgain Child - - nnoremap m :CodeQueryMenu Unite Magic - nnoremap q :cclose - nnoremap \ :CodeQueryFilter - - nnoremap p p - nnoremap u :colder \| CodeQueryShowQF - nnoremap :cnewer \| CodeQueryShowQF - endif - - " lock qf again - setlocal nomodifiable - setlocal nomodified -endfunction - - -function! codequery#query#do_query(word) abort - if empty(a:word) - echom 'Invalid Search Term: ' . a:word - return - endif - - let grep_options = s:create_grep_options(a:word) - let [grepformat, grepprg] = grep_options - - " Find Text - if empty(grepformat) - if g:codequery_find_text_from_current_file_dir == 1 - lcd %:p:h - endif - silent execute grepprg - call codequery#query#prettify_qf_layout_and_map_keys(getqflist()) - let g:codequery_last_query_word = a:word - let g:last_query_fuzzy = g:codequery_fuzzy - return - endif - - if v:version >= 800 - echom 'Searching ... [' . a:word . ']' - - let job_dict = {'is_append': g:codequery_append_to_qf ? 1 : 0, - \'target_file': tempname(), - \'backup_ef': &errorformat, - \'word' : a:word, - \'callback': function("codequery#query#do_query_callback")} - let options = {'out_io': 'file', - \'out_name': job_dict.target_file, - \'exit_cb': job_dict.callback} - - let &errorformat = '%f:%l%m' - let grepprg .= ' \| awk "{ sub(/.*\/\.\//,x) }1"' - let shell_cmd = substitute(grepprg, '\\|', '|', "g") - let shell_cmd = substitute(shell_cmd, "''", "'", "g") - - let s:query_job = job_start(['/bin/sh', '-c', shell_cmd], options) - let timer = timer_start(50, - \{-> execute("call job_status(s:query_job)", "")}, - \{'repeat': 200}) - else - let grepcmd = g:codequery_append_to_qf ? 'grepadd!' : 'grep!' - let l:grepprg_bak = &l:grepprg - let l:grepformat_bak = &grepformat - try - let &l:grepformat = grepformat - let &l:grepprg = grepprg . ' \| awk "{ sub(/.*\/\.\//,x) }1"' - silent execute grepcmd - redraw! - call s:show_result(a:word) - finally - let &l:grepprg = l:grepprg_bak - let &grepformat = l:grepformat_bak - let g:codequery_last_query_word = a:word - let g:last_query_fuzzy = g:codequery_fuzzy - endtry - endif -endfunction - - -function! codequery#query#do_query_callback(job, status) dict - try - if self.is_append - execute "caddfile " . self.target_file - else - execute "cgetfile " . self.target_file - endif - call s:show_result(self.word) - finally - let g:codequery_last_query_word = self.word - let g:last_query_fuzzy = g:codequery_fuzzy - let &errorformat = self.backup_ef - call delete(self.target_file) - endtry -endfunction - - -function! codequery#query#set_options(args) abort - let g:codequery_querytype = get(s:subcmd_map, a:args[0]) - - if index(a:args, '-f') != -1 - let g:codequery_fuzzy = 1 - endif - - if index(a:args, '-a') != -1 - let g:codequery_append_to_qf = 1 - endif -endfunction diff --git a/plugin/codequery.vim b/plugin/codequery.vim index adfb665..c25c027 100644 --- a/plugin/codequery.vim +++ b/plugin/codequery.vim @@ -1,73 +1,113 @@ -" Copyright (c) 2016 excusemejoe -" MIT License +if !exists("g:codequeryprg") + " let dbpath='D:\android\project\alps_f_base\frameworks\cqproject.db' + " let g:cquery='cqsearch -s '.$CQDBPATH.' -c -e -p ' + let g:cquery='cqsearch ' +endif +function! s:Cquery(cmd, args) + if exists("g:cqdb") + let g:cqdb = "" + endif + call FindCqueryDB() + if !exists("g:cqdb") || len(g:cqdb)<=0 + echo "No db file found." + return + endif -if exists('g:loaded_loaded_codequery') - finish -endif -let g:loaded_codequery = 1 + redraw + echo "Searching ..." + " If no pattern is provided, search for the word under the cursor + if empty(a:args) + let l:grepargs = expand("") + else + let l:grepargs = a:args + end -" ============================================================================= -" Options + "把参数转为cmd的GBK编码,否则搜索不了中文;另一种方案是把quickfix搜索结果转为GBK编码 + let l:grepargs = iconv(l:grepargs, "utf-8", "cp936") -" Init with default value -if !exists('g:codequery_find_text_cmd') - let g:codequery_find_text_cmd = 'Ack!' -endif -if !exists('g:codequery_find_text_from_current_file_dir') - let g:codequery_find_text_from_current_file_dir = 0 -endif -if !exists('g:codequery_auto_switch_to_find_text_for_wrong_filetype') - let g:codequery_auto_switch_to_find_text_for_wrong_filetype = 0 -endif -if !exists('g:codequery_trigger_build_db_when_db_not_found') - let g:codequery_trigger_build_db_when_db_not_found = 0 -endif -if !exists('g:codequery_disable_qf_key_bindings') - let g:codequery_disable_qf_key_bindings = 0 -endif -if !exists('g:codequery_enable_not_so_magic_menu') - let g:codequery_enable_not_so_magic_menu = 0 -endif + let grepprg_bak=&grepprg + let grepformat_bak=&grepformat + try + let &grepprg=g:cquery + let &grepformat="%f:%l\t%m" + echo g:cquery." ".l:grepargs + silent execute a:cmd . " -s " .g:cqdb.' -c -e -p '. l:grepargs + finally + let &grepprg=grepprg_bak + let &grepformat=grepformat_bak + endtry + + " call QfMakeConv() + + if a:cmd =~# '^l' + botright lopen + else + botright copen + endif + exec "nnoremap q :ccl" + exec "nnoremap t T" + exec "nnoremap T TgT" + exec "nnoremap o " + exec "nnoremap go " + exec "nnoremap v v" + exec "nnoremap gv v" + + " If highlighting is on, highlight the search keyword. + if exists("g:csearchhighlight") + let @/=a:args + set hlsearch + end + + redraw! +endfunction + +function! s:CqueryFromSearch(cmd, args) + let search = getreg('/') + " translate vim regular expression to perl regular expression. + let search = substitute(search,'\(\\<\|\\>\)','\\b','g') + call s:Cquery(a:cmd, '"' . search .'" '. a:args) +endfunction + +command! -bang -nargs=* -complete=file Cquery call s:Cquery('grep',) +command! -bang -nargs=* -complete=file CqueryAdd call s:Cquery('grepadd', ) +command! -bang -nargs=* -complete=file CqueryFromSearch call s:CqueryFromSearch('grep', ) +command! -bang -nargs=* -complete=file LCquery call s:Cquery('lgrep', ) +command! -bang -nargs=* -complete=file LCqueryAdd call s:Cquery('lgrepadd', ) + +nmap cqg :Cquery g -t =expand("") +nmap cqs :Cquery s -t =expand("") +nmap cqq :Cquery q -t =expand("") +nmap cqc :Cquery c -t =expand("") +nmap cqd :Cquery d -t =expand("") + + +function! QfMakeConv() + let qflist = getqflist() + for i in qflist + " let i.text = iconv(i.text, "cp936", "utf-8") + let i.text = iconv(i.text, "utf-8", "cp936") + endfor + call setqflist(qflist) +endfunction +function! FindCqueryDB() + let dir=expand("%:p:h") + " if exists("g:csindex") && len(dir)>=len(g:csindex) + " return + " endif -" No need to init -"let g:codequery_build_python_db_cmd = '' -"let g:codequery_build_javascript_db_cmd = '' -"let g:codequery_build_ruby_db_cmd = '' -"let g:codequery_build_go_db_cmd = '' -"let g:codequery_build_java_db_cmd = '' -"let g:codequery_build_c_db_cmd = '' -"let g:codequery_enable_auto_clean_languages = [] - - -" ============================================================================= -" Commands - - -command! -nargs=* -complete=customlist,s:complete_function CodeQuery - \ call codequery#run_codequery() -command! -nargs=* -complete=customlist,s:complete_function CodeQueryAgain - \ call codequery#run_codequery_again_with_different_subcmd() -command! -nargs=* CodeQueryFilter call codequery#filter_qf_results() -command! -nargs=* CodeQueryMakeDB call codequery#make_codequery_db() -command! -nargs=* CodeQueryViewDB call codequery#view_codequery_db() -command! -nargs=* CodeQueryMoveDBToGitDir - \ call codequery#move_codequery_db_to_git_hidden_dir() -command! -nargs=* CodeQueryMenu call codequery#show_menu() -command! -nargs=0 CodeQueryShowQF call - \ codequery#query#prettify_qf_layout_and_map_keys(getqflist()) - -let g:codequery_subcommands = [ 'Symbol', 'Text', - \ 'Definition', - \ 'Caller', 'Callee', 'Call', - \ 'Class', 'Member', 'Parent', 'Child', - \ 'FunctionList', - \ 'FileImporter', - \ ] - -function! s:complete_function(arg_lead, cmd_line, cursor_pos) - return g:codequery_subcommands + let prefixPath="/.KingConfig" + let csindexfilename="cqproject.db" + let dirLen=len(dir) + while (g:iswindows==1 && dirLen>3) || (g:iswindows!=1 && dirLen>1) + if isdirectory(dir.prefixPath) && filereadable(dir.prefixPath."/".csindexfilename) + let g:cqdb = dir.prefixPath."/".csindexfilename + return + endif + let dir=fnamemodify(dir, ':h') + let dirLen=len(dir) + endwhile endfunction