|
| 1 | +" ============================================================================= |
| 2 | +" Helpers |
| 3 | + |
| 4 | + |
| 5 | +let g:c_family_filetype_list = |
| 6 | + \ ['c', 'h', 'cpp', 'cxx', 'cc', 'hpp', 'hxx', 'hh'] |
| 7 | + |
| 8 | + |
| 9 | +let s:supported_filetypes = g:c_family_filetype_list + |
| 10 | + \ ['python', 'javascript', 'go', 'ruby', 'java', 'c', 'cpp'] |
| 11 | + |
| 12 | + |
| 13 | +function! s:check_filetype(filetype) |
| 14 | + if index(s:supported_filetypes, a:filetype) == -1 |
| 15 | + return 0 |
| 16 | + endif |
| 17 | + return 1 |
| 18 | +endfunction |
| 19 | + |
| 20 | + |
| 21 | +function! s:set_db() |
| 22 | + let path = codequery#db#find_db_path(&filetype) |
| 23 | + if empty(path) |
| 24 | + echom 'CodeQuery DB Not Found' |
| 25 | + return 0 |
| 26 | + endif |
| 27 | + |
| 28 | + let g:db_path = path |
| 29 | + return 1 |
| 30 | +endfunction |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +" ============================================================================= |
| 35 | +" Entries |
| 36 | + |
| 37 | + |
| 38 | +function! codequery#run_codequery(args) |
| 39 | + if !s:check_filetype(&filetype) |
| 40 | + echom 'Not Supported Filetype: ' . &filetype |
| 41 | + return |
| 42 | + endif |
| 43 | + |
| 44 | + let g:last_query_word = '' |
| 45 | + let g:fuzzy = 0 |
| 46 | + let g:append_to_quickfix = 0 |
| 47 | + let g:querytype = 1 |
| 48 | + let g:db_path = '' |
| 49 | + if !s:set_db() |
| 50 | + return |
| 51 | + endif |
| 52 | + |
| 53 | + let args = split(a:args, ' ') |
| 54 | + let args_num = len(args) |
| 55 | + let cword = codequery#query#get_valid_cursor_word() |
| 56 | + |
| 57 | + if args_num == 0 |
| 58 | + call codequery#query#do_query(cword) |
| 59 | + |
| 60 | + elseif index(g:codequery_subcommands, args[0]) != -1 |
| 61 | + call codequery#query#set_options(args) |
| 62 | + let iword = codequery#query#get_valid_input_word(args) |
| 63 | + let word = codequery#query#get_final_query_word(iword, cword) |
| 64 | + if empty(word) |
| 65 | + echom 'Invalid Args: ' . a:args |
| 66 | + return |
| 67 | + endif |
| 68 | + |
| 69 | + call codequery#query#do_query(word) |
| 70 | + else |
| 71 | + echom 'Wrong Subcommand !' |
| 72 | + endif |
| 73 | +endfunction |
| 74 | + |
| 75 | + |
| 76 | +function! s:make_codequery_db(args) |
| 77 | + let args = split(a:args, ' ') |
| 78 | + if empty(args) |
| 79 | + let args = [&filetype] |
| 80 | + endif |
| 81 | + |
| 82 | + for ft in args |
| 83 | + if !s:check_filetype(ft) |
| 84 | + echom 'Not Supported Filetype: ' . ft |
| 85 | + continue |
| 86 | + endif |
| 87 | + |
| 88 | + let db_path = codequery#db#find_db_path(ft) |
| 89 | + if empty(db_path) |
| 90 | + if index(g:c_family_filetype_list, ft) != -1 |
| 91 | + let db_path = 'c_family.db' |
| 92 | + else |
| 93 | + let db_path = ft . '.db' |
| 94 | + endif |
| 95 | + endif |
| 96 | + |
| 97 | + if ft ==? 'python' |
| 98 | + let shell_cmd = codequery#db#construct_python_db_build_cmd(db_path) |
| 99 | + elseif ft ==? 'javascript' |
| 100 | + let shell_cmd = codequery#db#construct_javascript_db_build_cmd(db_path) |
| 101 | + elseif ft ==? 'ruby' |
| 102 | + let shell_cmd = codequery#db#construct_ruby_db_build_cmd(db_path) |
| 103 | + elseif ft ==? 'go' |
| 104 | + let shell_cmd = codequery#db#construct_go_db_build_cmd(db_path) |
| 105 | + elseif ft ==? 'java' |
| 106 | + let shell_cmd = codequery#db#construct_java_db_build_cmd(db_path) |
| 107 | + elseif index(g:c_family_filetype_list, ft) != -1 |
| 108 | + let shell_cmd = codequery#db#construct_c_db_build_cmd(db_path) |
| 109 | + else |
| 110 | + echom 'No Command For Building .' . ft . ' file' |
| 111 | + continue |
| 112 | + endif |
| 113 | + |
| 114 | + " TODO: Rewrite it when Vim8 is coming |
| 115 | + " ---------------------------------------------------------------- |
| 116 | + if exists(':Start') |
| 117 | + silent execute 'Start! -title=Make_CodeQuery_DB -wait=error ' . shell_cmd |
| 118 | + redraw! |
| 119 | + echom 'Making ' . db_path ' => Run :CodeQueryViewDB to Check Status' |
| 120 | + else |
| 121 | + silent execute '!' . shell_cmd |
| 122 | + redraw! |
| 123 | + endif |
| 124 | + " ---------------------------------------------------------------- |
| 125 | + endfor |
| 126 | +endfunction |
| 127 | + |
| 128 | + |
| 129 | +function! s:view_codequery_db(args) |
| 130 | + let args = split(a:args, ' ') |
| 131 | + if empty(args) |
| 132 | + let args = [&filetype] |
| 133 | + endif |
| 134 | + |
| 135 | + for ft in args |
| 136 | + if !s:check_filetype(ft) |
| 137 | + echom 'Not Supported Filetype: ' . ft |
| 138 | + continue |
| 139 | + endif |
| 140 | + |
| 141 | + let db_path = codequery#db#find_db_path(ft) |
| 142 | + if empty(db_path) |
| 143 | + if index(g:c_family_filetype_list, ft) != -1 |
| 144 | + execute '!echo "\n(c family) DB Not Found"' |
| 145 | + else |
| 146 | + execute '!echo "\n(' . ft . ') DB Not Found"' |
| 147 | + endif |
| 148 | + continue |
| 149 | + endif |
| 150 | + |
| 151 | + execute '!echo "\n(' . db_path . ') is update at: " && stat -f "\%Sm" ' . db_path |
| 152 | + endfor |
| 153 | +endfunction |
| 154 | + |
| 155 | + |
| 156 | +function! s:move_codequery_db_to_git_hidden_dir(args) |
| 157 | + let args = split(a:args, ' ') |
| 158 | + if empty(args) |
| 159 | + let args = [&filetype] |
| 160 | + endif |
| 161 | + |
| 162 | + for ft in args |
| 163 | + if index(g:c_family_filetype_list, ft) != -1 |
| 164 | + let db_name = 'c_family.db' |
| 165 | + else |
| 166 | + let db_name = ft . '.db' |
| 167 | + endif |
| 168 | + let git_root_dir = systemlist('git rev-parse --show-toplevel')[0] |
| 169 | + let db_path = codequery#db#find_db_path(ft) |
| 170 | + |
| 171 | + if !v:shell_error && !empty(db_path) |
| 172 | + let new_db_path = git_root_dir . '/.git/codequery/' . db_name |
| 173 | + call system('mkdir -p ' . git_root_dir . '/.git/codequery/') |
| 174 | + call system('mv ' . db_path . ' ' . new_db_path) |
| 175 | + echom 'Done (' . db_name . ')' |
| 176 | + else |
| 177 | + echom 'Git Dir Not Found or (' . db_name . ') Not Found' |
| 178 | + endif |
| 179 | + endfor |
| 180 | +endfunction |
| 181 | + |
| 182 | + |
| 183 | +function! s:show_menu(args) |
| 184 | + let args = split(a:args, ' ') |
| 185 | + let args_num = len(args) |
| 186 | + |
| 187 | + if args_num > 0 && index(s:menu_subcommands, args[0]) != -1 |
| 188 | + if args[0] ==# 'Unite' |
| 189 | + if args_num > 1 && args[1] ==# 'Magic' |
| 190 | + let magic_menu = 1 |
| 191 | + else |
| 192 | + let magic_menu = 0 |
| 193 | + endif |
| 194 | + call s:use_unite_menu(magic_menu) |
| 195 | + return |
| 196 | + endif |
| 197 | + endif |
| 198 | + |
| 199 | + echom 'Wrong Subcommands! Try: ' . join(s:menu_subcommands, ', ') |
| 200 | +endfunction |
| 201 | + |
| 202 | + |
| 203 | +function! s:run_codequery_again_with_different_subcmd(args) |
| 204 | + let args = split(a:args, ' ') |
| 205 | + let args_num = len(args) |
| 206 | + if !empty(s:last_query_word) && args_num > 0 |
| 207 | + cclose |
| 208 | + let again_cmd = 'CodeQuery ' . args[0] . ' ' . s:last_query_word . ' ' |
| 209 | + \ . (s:last_query_fuzzy ? '-f' : '') |
| 210 | + execute again_cmd |
| 211 | + else |
| 212 | + echom 'Wrong Subcommands!' |
| 213 | + endif |
| 214 | +endfunction |
| 215 | + |
| 216 | + |
| 217 | +" modify from someone's .vimrc |
| 218 | +function! s:filter_qf_results(query) |
| 219 | + let results = getqflist() |
| 220 | + for d in results |
| 221 | + if bufname(d['bufnr']) !~ a:query && d['text'] !~ a:query |
| 222 | + call remove(results, index(results, d)) |
| 223 | + endif |
| 224 | + endfor |
| 225 | + call setqflist(results) |
| 226 | + call s:prettify_qf_layout_and_map_keys(results) |
| 227 | +endfunction |
0 commit comments