Skip to content

Commit 6c9cea0

Browse files
committed
[lazy load] refactor 100% : all cmds works and use abort for functions
1 parent 0dff889 commit 6c9cea0

File tree

5 files changed

+97
-88
lines changed

5 files changed

+97
-88
lines changed

autoload/codequery.vim

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ let s:supported_filetypes = g:c_family_filetype_list +
1010
\ ['python', 'javascript', 'go', 'ruby', 'java', 'c', 'cpp']
1111

1212

13-
function! s:check_filetype(filetype)
13+
let s:menu_subcommands = [ 'Unite' ]
14+
15+
16+
function! s:check_filetype(filetype) abort
1417
if index(s:supported_filetypes, a:filetype) == -1
1518
return 0
1619
endif
1720
return 1
1821
endfunction
1922

2023

21-
function! s:set_db()
24+
function! s:set_db() abort
2225
let path = codequery#db#find_db_path(&filetype)
2326
if empty(path)
2427
echom 'CodeQuery DB Not Found'
@@ -35,7 +38,7 @@ endfunction
3538
" Entries
3639

3740

38-
function! codequery#run_codequery(args)
41+
function! codequery#run_codequery(args) abort
3942
if !s:check_filetype(&filetype)
4043
echom 'Not Supported Filetype: ' . &filetype
4144
return
@@ -73,7 +76,7 @@ function! codequery#run_codequery(args)
7376
endfunction
7477

7578

76-
function! s:make_codequery_db(args)
79+
function! codequery#make_codequery_db(args) abort
7780
let args = split(a:args, ' ')
7881
if empty(args)
7982
let args = [&filetype]
@@ -126,7 +129,7 @@ function! s:make_codequery_db(args)
126129
endfunction
127130

128131

129-
function! s:view_codequery_db(args)
132+
function! codequery#view_codequery_db(args) abort
130133
let args = split(a:args, ' ')
131134
if empty(args)
132135
let args = [&filetype]
@@ -153,7 +156,7 @@ function! s:view_codequery_db(args)
153156
endfunction
154157

155158

156-
function! s:move_codequery_db_to_git_hidden_dir(args)
159+
function! codequery#move_codequery_db_to_git_hidden_dir(args) abort
157160
let args = split(a:args, ' ')
158161
if empty(args)
159162
let args = [&filetype]
@@ -180,7 +183,7 @@ function! s:move_codequery_db_to_git_hidden_dir(args)
180183
endfunction
181184

182185

183-
function! s:show_menu(args)
186+
function! codequery#show_menu(args) abort
184187
let args = split(a:args, ' ')
185188
let args_num = len(args)
186189

@@ -191,7 +194,7 @@ function! s:show_menu(args)
191194
else
192195
let magic_menu = 0
193196
endif
194-
call s:use_unite_menu(magic_menu)
197+
call codequery#menu#use_unite_menu(magic_menu)
195198
return
196199
endif
197200
endif
@@ -200,13 +203,13 @@ function! s:show_menu(args)
200203
endfunction
201204

202205

203-
function! s:run_codequery_again_with_different_subcmd(args)
206+
function! codequery#run_codequery_again_with_different_subcmd(args) abort
204207
let args = split(a:args, ' ')
205208
let args_num = len(args)
206-
if !empty(s:last_query_word) && args_num > 0
209+
if !empty(g:last_query_word) && args_num > 0
207210
cclose
208-
let again_cmd = 'CodeQuery ' . args[0] . ' ' . s:last_query_word . ' '
209-
\ . (s:last_query_fuzzy ? '-f' : '')
211+
let again_cmd = 'CodeQuery ' . args[0] . ' ' . g:last_query_word . ' '
212+
\ . (g:last_query_fuzzy ? '-f' : '')
210213
execute again_cmd
211214
else
212215
echom 'Wrong Subcommands!'
@@ -215,13 +218,13 @@ endfunction
215218

216219

217220
" modify from someone's .vimrc
218-
function! s:filter_qf_results(query)
221+
function! codequery#filter_qf_results(query) abort
219222
let results = getqflist()
220223
for d in results
221224
if bufname(d['bufnr']) !~ a:query && d['text'] !~ a:query
222225
call remove(results, index(results, d))
223226
endif
224227
endfor
225228
call setqflist(results)
226-
call s:prettify_qf_layout_and_map_keys(results)
229+
call codequery#query#prettify_qf_layout_and_map_keys(results)
227230
endfunction

autoload/codequery/db.vim

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
" =============================================================================
2+
" Entries
3+
14

25
" `lcd` brings side effect !!
3-
function! codequery#db#find_db_path(filetype)
6+
function! codequery#db#find_db_path(filetype) abort
47
if index(g:c_family_filetype_list, a:filetype) != -1
58
let db_name = 'c_family.db'
69
else
@@ -33,7 +36,7 @@ function! codequery#db#find_db_path(filetype)
3336
endfunction
3437

3538

36-
function! s:construct_python_db_build_cmd(db_path)
39+
function! codequery#db#construct_python_db_build_cmd(db_path) abort
3740
let find_cmd = 'find . -iname "*.py" > python_cscope.files'
3841
let pycscope_cmd = 'pycscope -f "python_cscope.out" -i python_cscope.files'
3942
let ctags_cmd = 'ctags --fields=+i -n -R -f "python_tags" -L python_cscope.files'
@@ -53,7 +56,7 @@ function! s:construct_python_db_build_cmd(db_path)
5356
endfunction
5457

5558

56-
function! s:construct_javascript_db_build_cmd(db_path)
59+
function! codequery#db#construct_javascript_db_build_cmd(db_path) abort
5760
let starscope_cmd = 'starscope --force-update -e ctags -e cscope **/*.js'
5861
let rename_cmd = 'mv tags javascript_tags && mv cscope.out javascript_cscope.out'
5962
let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path .
@@ -70,7 +73,7 @@ function! s:construct_javascript_db_build_cmd(db_path)
7073
endfunction
7174

7275

73-
function! s:construct_ruby_db_build_cmd(db_path)
76+
function! codequery#db#construct_ruby_db_build_cmd(db_path) abort
7477
let starscope_cmd = 'starscope --force-update -e ctags -e cscope **/*.rb'
7578
let rename_cmd = 'mv tags ruby_tags && mv cscope.out ruby_cscope.out'
7679
let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path .
@@ -87,7 +90,7 @@ function! s:construct_ruby_db_build_cmd(db_path)
8790
endfunction
8891

8992

90-
function! s:construct_go_db_build_cmd(db_path)
93+
function! codequery#db#construct_go_db_build_cmd(db_path) abort
9194
let starscope_cmd = 'starscope --force-update -e ctags -e cscope **/*.go'
9295
let rename_cmd = 'mv tags go_tags && mv cscope.out go_cscope.out'
9396
let cqmakedb_cmd = 'cqmakedb -s "' . a:db_path .
@@ -104,7 +107,7 @@ function! s:construct_go_db_build_cmd(db_path)
104107
endfunction
105108

106109

107-
function! s:construct_java_db_build_cmd(db_path)
110+
function! codequery#db#construct_java_db_build_cmd(db_path) abort
108111
let find_cmd = 'find . -iname "*.java" > java_cscope.files'
109112
let cscope_cmd = 'cscope -cbR -i java_cscope.files -f java_cscope.out'
110113
let ctags_cmd = 'ctags --fields=+i -n -R -f "java_tags" -L java_cscope.files'
@@ -124,7 +127,7 @@ function! s:construct_java_db_build_cmd(db_path)
124127
endfunction
125128

126129

127-
function! s:construct_c_db_build_cmd(db_path)
130+
function! codequery#db#construct_c_db_build_cmd(db_path) abort
128131
let find_cmd = 'find . -iname "*.c" > c_cscope.files && ' .
129132
\ 'find . -iname "*.h" >> c_cscope.files && ' .
130133
\ 'find . -iname "*.cpp" >> c_cscope.files && ' .
@@ -149,4 +152,3 @@ function! s:construct_c_db_build_cmd(db_path)
149152

150153
return exists('g:codequery_build_c_db_cmd') ? g:codequery_build_c_db_cmd : shell_cmd
151154
endfunction
152-

autoload/codequery/menu.vim

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
" =============================================================================
2+
" Entries
13

2-
let s:menu_subcommands = [ 'Unite' ]
34

5+
scriptencoding utf-8
46

5-
function! codequery#patch_unite_magic_menu_from_qf(fre_cmds, fun_cmds, cla_cmds)
7+
8+
function! codequery#menu#patch_unite_magic_menu_from_qf(fre_cmds, fun_cmds, cla_cmds) abort
69
call map(a:fre_cmds, '[substitute(v:val[0], "Find", "Switch To", "g"), v:val[1]]')
710
call map(a:fun_cmds, '[substitute(v:val[0], "Find", "Switch To", "g"), v:val[1]]')
811
call map(a:cla_cmds, '[substitute(v:val[0], "Find", "Switch To", "g"), v:val[1]]')
@@ -14,8 +17,8 @@ endfunction
1417

1518

1619

17-
function! codequery:use_unite_menu(magic)
18-
let cword = s:get_valid_cursor_word()
20+
function! codequery#menu#use_unite_menu(magic) abort
21+
let cword = codequery#query#get_valid_cursor_word()
1922
let menu_frequent_cmds = [['▷ Find Symbol', 'CodeQuery Symbol'],
2023
\['▷ Find Text', 'CodeQuery Text']]
2124
let menu_function_cmds = [['▷ Find Function Def. [F]', 'CodeQuery Definition'],
@@ -39,13 +42,13 @@ function! codequery:use_unite_menu(magic)
3942

4043
if a:magic
4144
if &filetype ==# 'qf'
42-
call codequery#patch_unite_magic_menu_from_qf(menu_frequent_cmds,
45+
call codequery#menu#patch_unite_magic_menu_from_qf(menu_frequent_cmds,
4346
\ menu_function_cmds,
4447
\ menu_class_cmds)
4548
let menu_other_cmds = []
4649
let menu_goto_full = []
47-
if exists('s:last_query_word')
48-
let cword = s:last_query_word
50+
if exists('g:last_query_word')
51+
let cword = g:last_query_word
4952
endif
5053
endif
5154

@@ -90,6 +93,3 @@ function! codequery:use_unite_menu(magic)
9093
execute 'Unite -silent -prompt-visible -prompt=::' . cword
9194
\ . ':: menu:codequery'
9295
endfunction
93-
94-
95-

autoload/codequery/query.vim

Lines changed: 59 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
" =============================================================================
2+
" Helpers
3+
14

25
let s:subcmd_map = { 'Symbol' : 1,
36
\ 'Definition' : 2,
@@ -14,17 +17,64 @@ let s:subcmd_map = { 'Symbol' : 1,
1417
\ 'DefinitionGroup' : 20 }
1518

1619

17-
function! codequery#query#is_valid_word(word)
20+
function! s:create_grep_options(word) abort
21+
if g:fuzzy
22+
let fuzzy_option = '-f'
23+
let word = '"' . a:word . '"'
24+
else
25+
let fuzzy_option = '-e'
26+
let word = a:word
27+
endif
28+
29+
let pipeline_script_option = ' \| cut -f 2,3'
30+
31+
let grepformat = '%f:%l%m'
32+
let grepprg = 'cqsearch -s ' . g:db_path . ' -p ' . g:querytype . ' -t '
33+
\ . word . ' -u ' . fuzzy_option . pipeline_script_option
34+
35+
if g:querytype == s:subcmd_map['FileImporter']
36+
let grepprg = 'cqsearch -s ' . g:db_path . ' -p ' . g:querytype . ' -t '
37+
\ . word . ' -u ' . fuzzy_option
38+
39+
elseif g:querytype == s:subcmd_map['Callee'] ||
40+
\ g:querytype == s:subcmd_map['Caller'] ||
41+
\ g:querytype == s:subcmd_map['Member']
42+
let grepprg = 'cqsearch -s ' . g:db_path . ' -p ' . g:querytype . ' -t '
43+
\ . word . ' -u ' . fuzzy_option . ' \| awk ''{ print $2 " " $1 }'''
44+
45+
elseif g:querytype == s:subcmd_map['Text']
46+
silent execute g:codequery_find_text_cmd . ' ' . a:word
47+
call codequery#query#prettify_qf_layout_and_map_keys(getqflist())
48+
49+
let g:last_query_word = a:word
50+
let g:last_query_fuzzy = g:fuzzy
51+
return
52+
53+
elseif g:querytype == s:subcmd_map['DefinitionGroup']
54+
echom 'Not Implement !'
55+
return
56+
endif
57+
58+
return [grepformat, grepprg]
59+
endfunction
60+
61+
62+
63+
" =============================================================================
64+
" Entries
65+
66+
67+
function! codequery#query#is_valid_word(word) abort
1868
return strlen(matchstr(a:word, '\v^[a-z|A-Z|0-9|_|*|?]+$')) > 0
1969
endfunction
2070

2171

22-
function! codequery#query#get_valid_cursor_word()
72+
function! codequery#query#get_valid_cursor_word() abort
2373
return codequery#query#is_valid_word(expand('<cword>')) ? expand('<cword>') : ''
2474
endfunction
2575

2676

27-
function! codequery#query#get_valid_input_word(args)
77+
function! codequery#query#get_valid_input_word(args) abort
2878
let args = deepcopy(a:args)
2979
if g:fuzzy
3080
call remove(args, index(args, '-f'))
@@ -41,7 +91,7 @@ function! codequery#query#get_valid_input_word(args)
4191
endfunction
4292

4393

44-
function! codequery#query#get_final_query_word(iword, cword)
94+
function! codequery#query#get_final_query_word(iword, cword) abort
4595
if empty(a:iword) && g:querytype == s:subcmd_map['FunctionList']
4696
return expand('%')
4797
elseif empty(a:iword) && g:querytype == s:subcmd_map['FileImporter']
@@ -50,12 +100,14 @@ function! codequery#query#get_final_query_word(iword, cword)
50100
return a:cword
51101
elseif empty(a:iword)
52102
return ''
103+
else
104+
return a:iword
53105
endif
54106
endfunction
55107

56108

57109
" Ref: MarcWeber's vim-addon-qf-layout
58-
function! codequery#query#prettify_qf_layout_and_map_keys(results)
110+
function! codequery#query#prettify_qf_layout_and_map_keys(results) abort
59111
if &filetype !=# 'qf'
60112
copen
61113
endif
@@ -115,49 +167,7 @@ function! codequery#query#prettify_qf_layout_and_map_keys(results)
115167
endfunction
116168

117169

118-
function! s:create_grep_options(word)
119-
if g:fuzzy
120-
let fuzzy_option = '-f'
121-
let word = '"' . a:word . '"'
122-
else
123-
let fuzzy_option = '-e'
124-
let word = a:word
125-
endif
126-
127-
let pipeline_script_option = ' \| cut -f 2,3'
128-
129-
let grepformat = '%f:%l%m'
130-
let grepprg = 'cqsearch -s ' . g:db_path . ' -p ' . g:querytype . ' -t '
131-
\ . word . ' -u ' . fuzzy_option . pipeline_script_option
132-
133-
if g:querytype == s:subcmd_map['FileImporter']
134-
let grepprg = 'cqsearch -s ' . s:db_path . ' -p ' . s:querytype . ' -t '
135-
\ . word . ' -u ' . fuzzy_option
136-
137-
elseif g:querytype == s:subcmd_map['Callee'] ||
138-
\ g:querytype == s:subcmd_map['Caller'] ||
139-
\ g:querytype == s:subcmd_map['Member']
140-
let grepprg = 'cqsearch -s ' . g:db_path . ' -p ' . g:querytype . ' -t '
141-
\ . word . ' -u ' . fuzzy_option . ' \| awk ''{ print $2 " " $1 }'''
142-
143-
elseif g:querytype == s:subcmd_map['Text']
144-
silent execute g:codequery_find_text_cmd . ' ' . a:word
145-
call codequery#query#prettify_qf_layout_and_map_keys(getqflist())
146-
147-
let g:last_query_word = a:word
148-
let g:last_query_fuzzy = s:fuzzy
149-
return
150-
151-
elseif g:querytype == s:subcmd_map['DefinitionGroup']
152-
echom 'Not Implement !'
153-
return
154-
endif
155-
156-
return [grepformat, grepprg]
157-
endfunction
158-
159-
160-
function! codequery#query#do_query(word)
170+
function! codequery#query#do_query(word) abort
161171
if empty(a:word)
162172
echom 'Invalid Search Term: ' . a:word
163173
return
@@ -197,7 +207,7 @@ function! codequery#query#do_query(word)
197207
endfunction
198208

199209

200-
function! codequery#query#set_options(args)
210+
function! codequery#query#set_options(args) abort
201211
let g:querytype = get(s:subcmd_map, a:args[0])
202212

203213
if index(a:args, '-f') != -1
@@ -208,9 +218,3 @@ function! codequery#query#set_options(args)
208218
let g:append_to_quickfix = 1
209219
endif
210220
endfunction
211-
212-
213-
" =============================================================================
214-
" Entries
215-
216-

0 commit comments

Comments
 (0)