2016-11-22 03:36:31 -05:00
|
|
|
let s:available = has('nvim') || (
|
2017-11-24 08:54:40 -05:00
|
|
|
\ has('job') && (
|
|
|
|
\ (has('patch-7-4-1826') && !has('gui_running')) ||
|
|
|
|
\ (has('patch-7-4-1850') && has('gui_running')) ||
|
|
|
|
\ (has('patch-7-4-1832') && has('gui_macvim'))
|
|
|
|
\ )
|
2016-11-22 03:36:31 -05:00
|
|
|
\ )
|
|
|
|
|
|
|
|
function! gitgutter#async#available()
|
|
|
|
return s:available
|
|
|
|
endfunction
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
|
2016-11-22 03:36:31 -05:00
|
|
|
function! gitgutter#async#execute(cmd) abort
|
2017-11-24 08:54:40 -05:00
|
|
|
let options = {
|
|
|
|
\ 'stdoutbuffer': [],
|
|
|
|
\ 'buffer': gitgutter#utility#bufnr()
|
|
|
|
\ }
|
|
|
|
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
|
|
|
|
call job_start(command, {
|
|
|
|
\ '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
|
|
|
\ })
|
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
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
function! s:on_stderr_nvim(_job_id, _data, _event) dict abort
|
|
|
|
" Backward compatibility for nvim < 0.2.0
|
|
|
|
if !has('nvim-0.2.0')
|
2016-11-22 03:36:31 -05:00
|
|
|
let current_buffer = gitgutter#utility#bufnr()
|
2017-11-24 08:54:40 -05:00
|
|
|
call gitgutter#utility#set_buffer(self.buffer)
|
|
|
|
if gitgutter#utility#is_active()
|
2016-11-22 03:36:31 -05:00
|
|
|
call gitgutter#hunk#reset()
|
|
|
|
endif
|
|
|
|
call gitgutter#utility#set_buffer(current_buffer)
|
2017-11-24 08:54:40 -05:00
|
|
|
return
|
2016-11-22 03:36:31 -05:00
|
|
|
endif
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
call s:buffer_exec(self.buffer, function('gitgutter#hunk#reset'))
|
2016-11-22 03:36:31 -05:00
|
|
|
endfunction
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
function! s:on_exit_nvim(_job_id, _data, _event) dict abort
|
|
|
|
" Backward compatibility for nvim < 0.2.0
|
|
|
|
if !has('nvim-0.2.0')
|
2016-11-22 03:36:31 -05:00
|
|
|
let current_buffer = gitgutter#utility#bufnr()
|
2017-11-24 08:54:40 -05:00
|
|
|
call gitgutter#utility#set_buffer(self.buffer)
|
2016-11-22 03:36:31 -05:00
|
|
|
if gitgutter#utility#is_active()
|
2017-11-24 08:54:40 -05:00
|
|
|
call gitgutter#handle_diff(gitgutter#utility#stringify(self.stdoutbuffer))
|
2016-11-22 03:36:31 -05:00
|
|
|
endif
|
|
|
|
call gitgutter#utility#set_buffer(current_buffer)
|
2017-11-24 08:54:40 -05:00
|
|
|
return
|
2016-11-22 03:36:31 -05:00
|
|
|
endif
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
call s:buffer_exec(self.buffer, function('gitgutter#handle_diff', [gitgutter#utility#stringify(self.stdoutbuffer)]))
|
2016-11-22 03:36:31 -05:00
|
|
|
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
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
function! s:on_stderr_vim(_channel, _data) dict abort
|
|
|
|
call s:buffer_exec(self.buffer, function('gitgutter#hunk#reset'))
|
2016-11-22 03:36:31 -05:00
|
|
|
endfunction
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
function! s:on_exit_vim(_channel) dict abort
|
|
|
|
call s:buffer_exec(self.buffer, function('gitgutter#handle_diff', [gitgutter#utility#stringify(self.stdoutbuffer)]))
|
2016-11-22 03:36:31 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
function! s:buffer_exec(buffer, fn)
|
|
|
|
let current_buffer = gitgutter#utility#bufnr()
|
|
|
|
call gitgutter#utility#set_buffer(a:buffer)
|
2016-11-22 03:36:31 -05:00
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
if gitgutter#utility#is_active()
|
|
|
|
call a:fn()
|
2016-11-22 03:36:31 -05:00
|
|
|
endif
|
|
|
|
|
2017-11-24 08:54:40 -05:00
|
|
|
call gitgutter#utility#set_buffer(current_buffer)
|
|
|
|
endfunction
|