2016-11-22 03:36:31 -05:00
|
|
|
let s:available = has('nvim') || (
|
2017-11-24 08:54:40 -05:00
|
|
|
\ has('job') && (
|
2022-09-25 06:10:28 -04:00
|
|
|
\ (has('patch-7-4-1826') && !has('gui_running')) ||
|
|
|
|
\ (has('patch-7-4-1850') && has('gui_running')) ||
|
|
|
|
\ (has('patch-7-4-1832') && has('gui_macvim'))
|
2017-11-24 08:54:40 -05:00
|
|
|
\ )
|
2016-11-22 03:36:31 -05:00
|
|
|
\ )
|
|
|
|
|
2020-12-04 16:15:32 -05:00
|
|
|
let s:jobs = {}
|
|
|
|
|
2016-11-22 03:36:31 -05:00
|
|
|
function! gitgutter#async#available()
|
|
|
|
return s:available
|
|
|
|
endfunction
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! gitgutter#async#execute(cmd, bufnr, handler) abort
|
|
|
|
call gitgutter#debug#log('[async] '.a:cmd)
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
let options = {
|
|
|
|
\ 'stdoutbuffer': [],
|
2018-03-31 10:56:26 -04:00
|
|
|
\ 'buffer': a:bufnr,
|
|
|
|
\ 'handler': a:handler
|
2017-11-24 08:54:40 -05:00
|
|
|
\ }
|
|
|
|
let command = s:build_command(a:cmd)
|
2016-11-22 03:36:31 -05:00
|
|
|
|
|
|
|
if has('nvim')
|
2017-11-24 08:54:40 -05:00
|
|
|
call jobstart(command, extend(options, {
|
|
|
|
\ 'on_stdout': function('s:on_stdout_nvim'),
|
|
|
|
\ 'on_stderr': function('s:on_stderr_nvim'),
|
|
|
|
\ 'on_exit': function('s:on_exit_nvim')
|
|
|
|
\ }))
|
|
|
|
else
|
2020-12-04 16:15:32 -05:00
|
|
|
let job = job_start(command, {
|
2017-11-24 08:54:40 -05:00
|
|
|
\ 'out_cb': function('s:on_stdout_vim', options),
|
|
|
|
\ 'err_cb': function('s:on_stderr_vim', options),
|
|
|
|
\ 'close_cb': function('s:on_exit_vim', options)
|
2016-11-22 03:36:31 -05:00
|
|
|
\ })
|
2020-12-04 16:15:32 -05:00
|
|
|
let s:jobs[s:job_id(job)] = 1
|
2017-11-24 08:54:40 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
2016-11-22 03:36:31 -05:00
|
|
|
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
function! s:build_command(cmd)
|
|
|
|
if has('unix')
|
|
|
|
return ['sh', '-c', a:cmd]
|
|
|
|
endif
|
2016-11-22 03:36:31 -05:00
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
if has('win32')
|
|
|
|
return has('nvim') ? ['cmd.exe', '/c', a:cmd] : 'cmd.exe /c '.a:cmd
|
2016-11-22 03:36:31 -05:00
|
|
|
endif
|
2017-11-24 08:54:40 -05:00
|
|
|
|
|
|
|
throw 'unknown os'
|
2016-11-22 03:36:31 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
function! s:on_stdout_nvim(_job_id, data, _event) dict abort
|
|
|
|
if empty(self.stdoutbuffer)
|
|
|
|
let self.stdoutbuffer = a:data
|
|
|
|
else
|
|
|
|
let self.stdoutbuffer = self.stdoutbuffer[:-2] +
|
|
|
|
\ [self.stdoutbuffer[-1] . a:data[0]] +
|
|
|
|
\ a:data[1:]
|
|
|
|
endif
|
|
|
|
endfunction
|
2016-11-22 03:36:31 -05:00
|
|
|
|
2019-03-27 11:08:56 -04:00
|
|
|
function! s:on_stderr_nvim(_job_id, data, _event) dict abort
|
|
|
|
if a:data != [''] " With Neovim there is always [''] reported on stderr.
|
|
|
|
call self.handler.err(self.buffer)
|
|
|
|
endif
|
2016-11-22 03:36:31 -05:00
|
|
|
endfunction
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:on_exit_nvim(_job_id, exit_code, _event) dict abort
|
|
|
|
if !a:exit_code
|
|
|
|
call self.handler.out(self.buffer, join(self.stdoutbuffer, "\n"))
|
2016-11-22 03:36:31 -05:00
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
function! s:on_stdout_vim(_channel, data) dict abort
|
|
|
|
call add(self.stdoutbuffer, a:data)
|
2016-11-22 03:36:31 -05:00
|
|
|
endfunction
|
|
|
|
|
2018-03-31 10:56:26 -04:00
|
|
|
function! s:on_stderr_vim(channel, _data) dict abort
|
|
|
|
call self.handler.err(self.buffer)
|
2016-11-22 03:36:31 -05:00
|
|
|
endfunction
|
|
|
|
|
2018-12-17 06:28:27 -05:00
|
|
|
function! s:on_exit_vim(channel) dict abort
|
|
|
|
let job = ch_getjob(a:channel)
|
2020-12-04 16:15:32 -05:00
|
|
|
let jobid = s:job_id(job)
|
|
|
|
if has_key(s:jobs, jobid) | unlet s:jobs[jobid] | endif
|
2018-12-17 06:28:27 -05:00
|
|
|
while 1
|
|
|
|
if job_status(job) == 'dead'
|
|
|
|
let exit_code = job_info(job).exitval
|
|
|
|
break
|
|
|
|
endif
|
|
|
|
sleep 5m
|
|
|
|
endwhile
|
|
|
|
|
|
|
|
if !exit_code
|
|
|
|
call self.handler.out(self.buffer, join(self.stdoutbuffer, "\n"))
|
|
|
|
endif
|
2017-11-24 08:54:40 -05:00
|
|
|
endfunction
|
2020-12-04 16:15:32 -05:00
|
|
|
|
|
|
|
function! s:job_id(job)
|
|
|
|
" Vim
|
|
|
|
return job_info(a:job).process
|
|
|
|
endfunction
|