Skip to content

Commit cd3aca7

Browse files
authored
Merge pull request devjoe#10 from devjoe/search-async-vim8
extract and refactor show_result function
2 parents 10a5f63 + 8c26490 commit cd3aca7

File tree

2 files changed

+66
-24
lines changed

2 files changed

+66
-24
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ This Vim plugin is built on top of the great tool [CodeQuery](https://github.com
4040

4141
<br>
4242

43-
## Demo
43+
## Demo
4444

4545
`Choose a query from menu ➙ Get results`
4646

@@ -52,30 +52,32 @@ This Vim plugin is built on top of the great tool [CodeQuery](https://github.com
5252

5353
[more demo and screenshots](https://github.com/devjoe/vim-codequery/wiki/Screenshots-and-Demo)
5454

55+
> These videos are quite old.
56+
> Now vim-codequery works better than what you see. (by supporting Vim8's async feature)
5557
<br>
5658
5759
## Schedule
5860

5961
> **This project is almost ready to be released.**
6062
>
6163
> Main TODO:
62-
> * Use Vim8's new features to enhance usability.
64+
> * ~~Use Vim8's new features to enhance usability.~~
6365
> * ~~Do lazy-loading.~~
6466
> * Test it.
6567
> * Doc it.
6668
>
6769
> Completeness: 92%
6870
> Current Version: v0.8
6971
>
70-
> It is Ok to try it! 👌
72+
> Welcome to try it! 👌
7173
7274
<br>
7375

7476
## Installation
7577

7678
#### 1. Make sure these commands are in your system
7779
<pre>
78-
echo mkdir mv cut find awk stat git(optional)
80+
/bin/sh echo mkdir mv cut find awk stat git(optional)
7981
</pre>
8082

8183
#### 2. Install CodeQuery

autoload/codequery/query.vim

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ function! s:create_grep_options(word) abort
5050
endfunction
5151

5252

53+
function! s:show_result() abort
54+
let results = getqflist()
55+
call codequery#query#prettify_qf_layout_and_map_keys(results)
56+
if !empty(results)
57+
echom 'Found ' . len(results) . ' result' . (len(results) > 1 ? 's' : '')
58+
else
59+
echom 'Result Not Found'
60+
endif
61+
endfunction
62+
63+
5364

5465
" =============================================================================
5566
" Entries
@@ -183,31 +194,61 @@ function! codequery#query#do_query(word) abort
183194
return
184195
endif
185196

186-
" TODO: Rewrite it when Vim8 is coming
187-
" ----------------------------------------------------------------
188-
let grepcmd = g:codequery_append_to_qf ? 'grepadd!' : 'grep!'
189-
let l:grepprg_bak = &l:grepprg
190-
let l:grepformat_bak = &grepformat
197+
if v:version >= 800
198+
echom 'Searching ... [' . a:word . ']'
199+
200+
let job_dict = {'is_append': g:codequery_append_to_qf ? 1 : 0,
201+
\'target_file': tempname(),
202+
\'backup_ef': &errorformat,
203+
\'word' : a:word,
204+
\'callback': function("codequery#query#do_query_callback")}
205+
let options = {'out_io': 'file',
206+
\'out_name': job_dict.target_file,
207+
\'exit_cb': job_dict.callback}
208+
209+
let &errorformat = '%f:%l%m'
210+
let grepprg .= ' \| awk "{ sub(/.*\/\.\//,x) }1"'
211+
let shell_cmd = substitute(grepprg, '\\|', '|', "g")
212+
let shell_cmd = substitute(shell_cmd, "''", "'", "g")
213+
214+
let s:query_job = job_start(['/bin/sh', '-c', shell_cmd], options)
215+
let timer = timer_start(50,
216+
\{-> execute("call job_status(s:query_job)", "")},
217+
\{'repeat': 200})
218+
else
219+
let grepcmd = g:codequery_append_to_qf ? 'grepadd!' : 'grep!'
220+
let l:grepprg_bak = &l:grepprg
221+
let l:grepformat_bak = &grepformat
222+
try
223+
let &l:grepformat = grepformat
224+
let &l:grepprg = grepprg . ' \| awk "{ sub(/.*\/\.\//,x) }1"'
225+
silent execute grepcmd
226+
redraw!
227+
call s:show_result()
228+
finally
229+
let &l:grepprg = l:grepprg_bak
230+
let &grepformat = l:grepformat_bak
231+
let g:codequery_last_query_word = a:word
232+
let g:last_query_fuzzy = g:codequery_fuzzy
233+
endtry
234+
endif
235+
endfunction
236+
237+
238+
function! codequery#query#do_query_callback(job, status) dict
191239
try
192-
let &l:grepformat = grepformat
193-
let &l:grepprg = grepprg . ' \| awk "{ sub(/.*\/\.\//,x) }1"'
194-
silent execute grepcmd
195-
redraw!
196-
197-
let results = getqflist()
198-
call codequery#query#prettify_qf_layout_and_map_keys(results)
199-
if !empty(results)
200-
echom 'Found ' . len(results) . ' results'
240+
if self.is_append
241+
execute "caddfile " . self.target_file
201242
else
202-
echom 'Result Not Found'
243+
execute "cgetfile " . self.target_file
203244
endif
245+
call s:show_result()
204246
finally
205-
let &l:grepprg = l:grepprg_bak
206-
let &grepformat = l:grepformat_bak
207-
let g:codequery_last_query_word = a:word
247+
let g:codequery_last_query_word = self.word
208248
let g:last_query_fuzzy = g:codequery_fuzzy
249+
let &errorformat = self.backup_ef
250+
call delete(self.target_file)
209251
endtry
210-
" ----------------------------------------------------------------
211252
endfunction
212253

213254

@@ -222,4 +263,3 @@ function! codequery#query#set_options(args) abort
222263
let g:codequery_append_to_qf = 1
223264
endif
224265
endfunction
225-

0 commit comments

Comments
 (0)