PoC: make async work with git and Neovim

This commit is contained in:
Marco Hinz 2017-01-17 16:17:16 +01:00
parent 3ad10c65b5
commit e72ccb952d
No known key found for this signature in database
GPG Key ID: 1C980A1B657B4A4F
2 changed files with 43 additions and 55 deletions

View File

@ -8,6 +8,13 @@ let g:sy_cache = {}
let s:has_doau_modeline = v:version > 703 || v:version == 703 && has('patch442') let s:has_doau_modeline = v:version > 703 || v:version == 703 && has('patch442')
" Function: #verbose {{{1
function! sy#verbose(msg) abort
if &verbose
echomsg printf('[sy] %s', a:msg)
endif
endfunction
" Function: #start {{{1 " Function: #start {{{1
function! sy#start() abort function! sy#start() abort
if g:signify_locked if g:signify_locked
@ -24,9 +31,10 @@ function! sy#start() abort
return return
endif endif
function! s:chdir() function! s:chdir()
return haslocaldir() ? 'lcd' : (exists(':tcd') && haslocaldir(-1, 0)) ? 'tcd' : 'cd' return haslocaldir()
\ ? 'lcd'
\ : (exists(':tcd') && haslocaldir(-1, 0)) ? 'tcd' : 'cd'
endfunction endfunction
" sy_info is used in autoload/sy/repo " sy_info is used in autoload/sy/repo
@ -38,8 +46,8 @@ function! sy#start() abort
\ 'file': sy#util#escape(fnamemodify(sy_path, ':t')), \ 'file': sy#util#escape(fnamemodify(sy_path, ':t')),
\ } \ }
" new buffer.. add to list of registered files
if !exists('b:sy') || b:sy.path != sy_path if !exists('b:sy') || b:sy.path != sy_path
call sy#verbose('Register new file: '. sy_path)
let b:sy = { let b:sy = {
\ 'path' : sy_path, \ 'path' : sy_path,
\ 'buffer': bufnr(''), \ 'buffer': bufnr(''),
@ -51,65 +59,39 @@ function! sy#start() abort
if get(g:, 'signify_disable_by_default') if get(g:, 'signify_disable_by_default')
return return
endif endif
" register buffer as active
let b:sy.active = 1 let b:sy.active = 1
call sy#repo#detect(1) call sy#repo#detect(1)
" let [ diff, b:sy.type ] = sy#repo#detect()
" if b:sy.type == 'unknown'
" call sy#disable()
" return
" endif
" " register file as active with found VCS
" let b:sy.stats = [0, 0, 0]
" let dir = fnamemodify(b:sy.path, ':h')
" if !has_key(g:sy_cache, dir)
" let g:sy_cache[dir] = b:sy.type
" endif
" if empty(diff)
" " no changes found
" return
" endif
" inactive buffer.. bail out
elseif !b:sy.active elseif !b:sy.active
call sy#verbose('Inactive buffer: '. sy_path)
return return
" retry detecting VCS
elseif b:sy.type == 'unknown' elseif b:sy.type == 'unknown'
call sy#verbose('Retry detecting VCS: '. sy_path)
call sy#repo#detect(0) call sy#repo#detect(0)
" update signs
else else
let diff = sy#repo#get_diff_{b:sy.type}()[1] call sy#verbose('Update signs: '. sy_path)
let b:sy.id_top = g:id_top call sy#repo#get_diff_{b:sy.type}(0)
endif endif
call sy#update_signs(diff, type)
endfunction endfunction
function! sy#update_signs(diff, type, do_register) abort function! sy#update_signs(diff, do_register) abort
if b:sy.type == 'unknown' " if b:sy.type == 'unknown'
echomsg 'DEBUG: type unknown' " echomsg 'DEBUG: type unknown'
" no VCS found " " no VCS found
call sy#disable() " call sy#disable()
return " return
endif " endif
if do_register if a:do_register
" register file as active with found VCS " register file as active with found VCS
let b:sy.stats = [0, 0, 0] let b:sy.stats = [0, 0, 0]
let dir = fnamemodify(b:sy.path, ':h')
let dir = fnamemodify(b:sy.path, ':h') let dir = fnamemodify(b:sy.path, ':h')
if !has_key(g:sy_cache, dir) if !has_key(g:sy_cache, dir)
let g:sy_cache[dir] = b:sy.type let g:sy_cache[dir] = b:sy.type
endif endif
if empty(diff) if empty(a:diff)
" no changes found " no changes found
return return
endif endif

View File

@ -3,7 +3,7 @@
scriptencoding utf-8 scriptencoding utf-8
" Function: #detect {{{1 " Function: #detect {{{1
function! sy#repo#detect() abort function! sy#repo#detect(do_register) abort
let vcs_list = s:vcs_list let vcs_list = s:vcs_list
" Simple cache. If there is a registered VCS-controlled file in this " Simple cache. If there is a registered VCS-controlled file in this
" directory already, assume that this file is probably controlled by " directory already, assume that this file is probably controlled by
@ -16,7 +16,7 @@ function! sy#repo#detect() abort
endif endif
for type in vcs_list for type in vcs_list
call sy#repo#get_diff_{type}() call sy#repo#get_diff_{type}(a:do_register)
endfor endfor
" let [istype, diff] = sy#repo#get_diff_{type}() " let [istype, diff] = sy#repo#get_diff_{type}()
" if istype " if istype
@ -40,25 +40,31 @@ endfunction
" Function: s:callback_exit {{{1 " Function: s:callback_exit {{{1
function! s:callback_exit(_job_id, exitcode, _event) dict abort function! s:callback_exit(_job_id, exitcode, _event) dict abort
let [found_diff, diff] = a:exitcode ? [0, ''] : [1, diff] let [found_diff, diff] = a:exitcode ? [0, ''] : [1, join(self.stdoutbuf, "\n")]
call sy#update_signs(found_diff, diff) " echomsg 'DEBUG: '. diff
if found_diff
let b:sy.type = 'git'
endif
if !self.do_register
let b:sy.id_top = g:id_top
endif
call sy#update_signs(diff, self.do_register)
endfunction endfunction
" Function: #get_diff_git {{{1 " Function: #get_diff_git {{{1
function! sy#repo#get_diff_git() abort function! sy#repo#get_diff_git(do_register) abort
let cmd = (has('win32') && &shell =~ 'cmd') let cmd = s:expand_cmd(g:signify_vcs_cmds.git, b:sy_info.file)
\ ? g:signify_vcs_cmds.git let cmd = (has('win32') && &shell =~ 'cmd') ? cmd : ['sh', '-c', cmd]
\ : ['sh', '-c', g:signify_vcs_cmds.git]
let cmd = s:expand_cmd(cmd, b:sy_info.file)
if exists('s:job_id_git') if exists('s:job_id_git')
silent! call jobstop(s:job_id_git) silent! call jobstop(s:job_id_git)
endif endif
execute b:sy_info.chdir fnameescape(b:sy_info.dir) execute b:sy_info.chdir fnameescape(b:sy_info.dir)
try try
let s:job_id_git = jobstart(cmd, { let s:job_id_git = jobstart(cmd, {
\ 'stdoutbuf': [], \ 'stdoutbuf': [],
\ 'on_stdout': function('s:callback_stdout_nvim'), \ 'do_register': a:do_register,
\ 'on_exit': function('s:callback_exit'), \ 'on_stdout': function('s:callback_stdout_nvim'),
\ 'on_exit': function('s:callback_exit'),
\ }) \ })
finally finally
execute b:sy_info.chdir b:sy_info.cwd execute b:sy_info.chdir b:sy_info.cwd