Refactor async functions

- create a new async module
- refactor async functions from branch.vim and po.vim to async.vim
- support nvim async correctly
This commit is contained in:
Christian Brabandt
2017-08-23 18:37:24 +02:00
parent 50bfe8dd68
commit 4c74a95045
4 changed files with 279 additions and 203 deletions
+3 -46
View File
@@ -3,7 +3,7 @@
scriptencoding utf-8
function! s:shorten()
function! airline#extensions#po#shorten()
if exists("g:airline#extensions#po#displayed_limit")
let w:displayed_po_limit = g:airline#extensions#po#displayed_limit
if len(b:airline_po_stats) > w:displayed_po_limit - 1
@@ -12,49 +12,6 @@ function! s:shorten()
endif
endfunction
if g:airline#init#async
let s:jobs = {}
function! s:on_stdout(channel, msg) dict abort
let self.buf = a:msg
endfunction
function! s:on_exit(channel) dict abort
if !empty(self.buf)
let b:airline_po_stats = printf("[%s]", self.buf)
else
let b:airline_po_stats = ''
endif
if has_key(s:jobs, self.file)
call remove(s:jobs, self.file)
endif
call s:shorten()
endfunction
function! s:get_msgfmt_stat_async(cmd, file)
if g:airline#init#is_windows || !executable('msgfmt')
" no msgfmt on windows?
return
else
let cmd = ['sh', '-c', a:cmd. shellescape(a:file)]
endif
let options = {'buf': '', 'file': a:file}
if has_key(s:jobs, a:file)
if job_status(get(s:jobs, a:file)) == 'run'
return
elseif has_key(s:jobs, a:file)
call remove(s:jobs, a:file)
endif
endif
let id = job_start(cmd, {
\ 'err_io': 'out',
\ 'out_cb': function('s:on_stdout', options),
\ 'close_cb': function('s:on_exit', options)})
let s:jobs[a:file] = id
endfu
endif
function! airline#extensions#po#apply(...)
if &ft ==# 'po'
call airline#extensions#prepend_to_section('z', '%{airline#extensions#po#stats()}')
@@ -69,7 +26,7 @@ function! airline#extensions#po#stats()
let cmd = 'msgfmt --statistics -o /dev/null -- '
if g:airline#init#async
call s:get_msgfmt_stat_async(cmd, expand('%:p'))
call airline#async#get_msgfmt_stat(cmd, expand('%:p'))
else
let airline_po_stats = system(cmd. shellescape(expand('%:p')))
if v:shell_error
@@ -81,7 +38,7 @@ function! airline#extensions#po#stats()
catch
let b:airline_po_stats = ''
endtry
call s:shorten()
call airline#extensions#po#shorten()
endif
return get(b:, 'airline_po_stats', '')
endfunction