Skip to content

Commit 9f8e500

Browse files
author
skywind3000
committed
polish quickfix id
1 parent 1974ce9 commit 9f8e500

File tree

1 file changed

+59
-67
lines changed

1 file changed

+59
-67
lines changed

plugin/asyncrun.vim

Lines changed: 59 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Maintainer: skywind3000 (at) gmail.com, 2016-2022
44
" Homepage: https://github.com/skywind3000/asyncrun.vim
55
"
6-
" Last Modified: 2022/02/20 00:05
6+
" Last Modified: 2022/03/08 14:33
77
"
88
" Run shell command in background and output to quickfix:
99
" :AsyncRun[!] [options] {cmd} ...
@@ -133,8 +133,8 @@ let g:asyncrun_mode = get(g:, 'asyncrun_mode', 0)
133133
" Use quickfix-ID to allow concurrent use of quickfix list and not
134134
" interleave streamed output of a running command with output from
135135
" other plugins
136-
if !exists('g:asyncrun_qf_id')
137-
let g:asyncrun_qf_id = has('patch-8.0.1023') || has('nvim-0.6.1')
136+
if !exists('g:asyncrun_qfid')
137+
let g:asyncrun_qfid = has('patch-8.0.1023') || has('nvim-0.6.1')
138138
endif
139139

140140
" command hook
@@ -314,12 +314,15 @@ if has('nvim')
314314
endif
315315
endif
316316

317+
" check qfid
318+
let s:has_qfid = has('patch-8.0.1023') || has('nvim-0.6.1')
319+
317320

318321
"----------------------------------------------------------------------
319322
"- build in background
320323
"----------------------------------------------------------------------
321324
let s:async_nvim = has('nvim')? 1 : 0
322-
let s:async_info = { 'text':"", 'post':'', 'postsave':'' }
325+
let s:async_info = { 'text':"", 'post':'', 'postsave':'', 'qfid':-1 }
323326
let s:async_output = {}
324327
let s:async_head = 0
325328
let s:async_tail = 0
@@ -346,6 +349,48 @@ if s:async_nvim == 0 && v:version >= 800
346349
let s:async_congest = 0
347350
endif
348351

352+
" append to quickfix
353+
function! s:AppendText(textlist, raw)
354+
let qfid = s:async_info.qfid
355+
if qfid < 0
356+
if a:raw == 0
357+
if and(g:asyncrun_skip, 1) == 0
358+
caddexpr a:textlist
359+
else
360+
noautocmd caddexpr a:textlist
361+
endif
362+
else
363+
let items = []
364+
for text in a:textlist
365+
let items += [{'text': text}]
366+
endfor
367+
if and(g:asyncrun_skip, 1) == 0
368+
call setqflist(items, 'a')
369+
else
370+
noautocmd call setqflist(items, 'a')
371+
endif
372+
unlet items
373+
endif
374+
else
375+
let info = {'id': qfid}
376+
if a:raw == 0
377+
let info.lines = a:textlist
378+
else
379+
let items = []
380+
for text in a:textlist
381+
let items += [{'text': text}]
382+
endfor
383+
let info.items = items
384+
endif
385+
if and(g:asyncrun_skip, 1) == 0
386+
call setqflist([], 'a', info)
387+
else
388+
noautocmd call setqflist([], 'a', info)
389+
endif
390+
unlet info
391+
endif
392+
endfunc
393+
349394
" quickfix window cursor check
350395
function! s:AsyncRun_Job_Cursor()
351396
if &buftype == 'quickfix'
@@ -402,7 +447,6 @@ function! s:AsyncRun_Job_Update(count, ...)
402447
let l:iconv = (encoding != "")? 1 : 0
403448
let l:count = 0
404449
let l:total = 0
405-
let l:empty = [{'text':''}]
406450
let l:check = s:AsyncRun_Job_CheckScroll()
407451
let l:efm1 = &g:efm
408452
let l:efm2 = &l:efm
@@ -437,34 +481,9 @@ function! s:AsyncRun_Job_Update(count, ...)
437481
let l:text = substitute(l:text, '\r$', '', 'g')
438482
if once == 0
439483
if l:text != ''
440-
if l:raw == 0
441-
if exists('s:qf_id')
442-
let qflist = {'id': s:qf_id, 'lines': split(l:text, "\n")}
443-
if and(g:asyncrun_skip, 1) == 0
444-
call setqflist([], 'a', qflist)
445-
else
446-
noautocmd call setqflist([], 'a', qflist)
447-
endif
448-
else
449-
if and(g:asyncrun_skip, 1) == 0
450-
caddexpr l:text
451-
else
452-
noautocmd caddexpr l:text
453-
endif
454-
endif
455-
else
456-
if exists('s:qf_id')
457-
call setqflist([], 'a', {'id': s:qf_id, 'items': [{'text':l:text}]})
458-
else
459-
call setqflist([{'text':l:text}], 'a')
460-
endif
461-
endif
484+
call s:AppendText([l:text], l:raw)
462485
elseif g:asyncrun_trim == 0
463-
if exists('s:qf_id')
464-
call setqflist([], 'a', {'id': s:qf_id, 'items': l:empty})
465-
else
466-
call setqflist(l:empty, 'a')
467-
endif
486+
call s:AppendText([''], l:raw)
468487
endif
469488
else
470489
if l:text != ''
@@ -482,20 +501,7 @@ function! s:AsyncRun_Job_Update(count, ...)
482501
endif
483502
endwhile
484503
if once != 0
485-
if l:raw == 0
486-
if and(g:asyncrun_skip, 1) == 0
487-
caddexpr array
488-
else
489-
noautocmd caddexpr array
490-
endif
491-
else
492-
let items = []
493-
for text in array
494-
let items += [{'text': text}]
495-
endfor
496-
call setqflist(items, 'a')
497-
unlet items
498-
endif
504+
call s:AppendText(array, l:raw)
499505
unlet array
500506
endif
501507
if pathfix != 0
@@ -592,31 +598,20 @@ function! s:AsyncRun_Job_OnFinish()
592598
call timer_stop(s:async_timer)
593599
unlet s:async_timer
594600
endif
595-
if exists('s:qf_id')
596-
unlet s:qf_id
597-
endif
598601
call s:AsyncRun_Job_Update(-1, s:async_info.once)
599602
let l:current = localtime()
600603
let l:last = l:current - s:async_start
601604
let l:check = s:AsyncRun_Job_CheckScroll()
602605
if s:async_code == 0
603606
let l:text = "[Finished in ".l:last." seconds]"
604607
if !s:async_info.strip
605-
if exists('s:qf_id')
606-
call setqflist([], 'a', {'id': s:qf_id, 'items': [{'text':l:text}]})
607-
else
608-
call setqflist([{'text':l:text}], 'a')
609-
endif
608+
call s:AppendText([l:text], 1)
610609
endif
611610
let g:asyncrun_status = "success"
612611
else
613612
let l:text = 'with code '.s:async_code
614613
let l:text = "[Finished in ".l:last." seconds ".l:text."]"
615-
if exists('s:qf_id')
616-
call setqflist([], 'a', {'id': s:qf_id, 'lines': [l:text]})
617-
else
618-
call setqflist([{'text':l:text}], 'a')
619-
endif
614+
call s:AppendText([l:text], 1)
620615
let g:asyncrun_status = "failure"
621616
endif
622617
let s:async_state = 0
@@ -793,6 +788,7 @@ function! s:AsyncRun_Job_Start(cmd)
793788
let s:async_info.auto = s:async_info.autosave
794789
let s:async_info.postsave = ''
795790
let s:async_info.autosave = ''
791+
let s:async_info.qfid = -1
796792
let g:asyncrun_text = s:async_info.text
797793
call s:AutoCmd('Pre')
798794
if s:async_nvim == 0
@@ -877,15 +873,11 @@ function! s:AsyncRun_Job_Start(cmd)
877873
call setqflist([], ' ', l:title)
878874
endif
879875
endif
880-
if g:asyncrun_qf_id == 1
881-
let s:qf_id = getqflist({'id':0}).id
876+
if g:asyncrun_qfid && s:has_qfid
877+
let s:async_info.qfid = getqflist({'id':0}).id
882878
endif
883879
if !s:async_info.strip
884-
if exists('s:qf_id')
885-
call setqflist([], 'a', {'id': s:qf_id, 'lines': [l:arguments]})
886-
else
887-
call setqflist([{'text':l:arguments}], 'a')
888-
endif
880+
call s:AppendText([l:arguments], 1)
889881
endif
890882
let l:name = 'g:AsyncRun_Job_OnTimer'
891883
let s:async_timer = timer_start(100, l:name, {'repeat':-1})
@@ -2100,7 +2092,7 @@ endfunc
21002092
" asyncrun - version
21012093
"----------------------------------------------------------------------
21022094
function! asyncrun#version()
2103-
return '2.9.10'
2095+
return '2.9.11'
21042096
endfunc
21052097

21062098

0 commit comments

Comments
 (0)