Simplify async callbacks for setting repo path.

This commit is contained in:
Daniel Hahler
2019-05-29 13:06:05 +01:00
committed by Andy Stewart
parent 5bb4f14ed3
commit 50932df59a

View File

@@ -114,6 +114,19 @@ function! gitgutter#utility#repo_path(bufnr, shellesc) abort
return a:shellesc ? gitgutter#utility#shellescape(p) : p return a:shellesc ? gitgutter#utility#shellescape(p) : p
endfunction endfunction
let s:set_path_handler = {}
function! s:set_path_handler.out(buffer, path) abort
let path = s:strip_trailing_new_line(a:path)
call gitgutter#utility#setbufvar(a:buffer, 'path', path)
endfunction
function! s:set_path_handler.err(buffer) abort
call gitgutter#utility#setbufvar(a:buffer, 'path', -2)
endfunction
function! gitgutter#utility#set_repo_path(bufnr) abort function! gitgutter#utility#set_repo_path(bufnr) abort
" Values of path: " Values of path:
" * non-empty string - path " * non-empty string - path
@@ -124,24 +137,8 @@ function! gitgutter#utility#set_repo_path(bufnr) abort
let cmd = gitgutter#utility#cd_cmd(a:bufnr, g:gitgutter_git_executable.' ls-files --error-unmatch --full-name -z -- '.gitgutter#utility#shellescape(s:filename(a:bufnr))) let cmd = gitgutter#utility#cd_cmd(a:bufnr, g:gitgutter_git_executable.' ls-files --error-unmatch --full-name -z -- '.gitgutter#utility#shellescape(s:filename(a:bufnr)))
if g:gitgutter_async && gitgutter#async#available() if g:gitgutter_async && gitgutter#async#available()
if has('lambda') let handler = copy(s:set_path_handler)
call gitgutter#async#execute(cmd, a:bufnr, { call gitgutter#async#execute(cmd, a:bufnr, handler)
\ 'out': {bufnr, path -> gitgutter#utility#setbufvar(bufnr, 'path', s:strip_trailing_new_line(path))},
\ 'err': {bufnr -> gitgutter#utility#setbufvar(bufnr, 'path', -2)},
\ })
else
if has('nvim') && !has('nvim-0.2.0')
call gitgutter#async#execute(cmd, a:bufnr, {
\ 'out': function('s:set_path'),
\ 'err': function('s:not_tracked_by_git')
\ })
else
call gitgutter#async#execute(cmd, a:bufnr, {
\ 'out': function('s:set_path'),
\ 'err': function('s:set_path', [-2])
\ })
endif
endif
else else
let path = gitgutter#utility#system(cmd) let path = gitgutter#utility#system(cmd)
if v:shell_error if v:shell_error
@@ -152,20 +149,6 @@ function! gitgutter#utility#set_repo_path(bufnr) abort
endif endif
endfunction endfunction
if has('nvim') && !has('nvim-0.2.0')
function! s:not_tracked_by_git(bufnr)
call s:set_path(a:bufnr, -2)
endfunction
endif
function! s:set_path(bufnr, path)
if a:bufnr == -2
let [bufnr, path] = [a:path, a:bufnr]
call gitgutter#utility#setbufvar(bufnr, 'path', path)
else
call gitgutter#utility#setbufvar(a:bufnr, 'path', s:strip_trailing_new_line(a:path))
endif
endfunction
function! gitgutter#utility#cd_cmd(bufnr, cmd) abort function! gitgutter#utility#cd_cmd(bufnr, cmd) abort
let cd = s:unc_path(a:bufnr) ? 'pushd' : (gitgutter#utility#windows() ? 'cd /d' : 'cd') let cd = s:unc_path(a:bufnr) ? 'pushd' : (gitgutter#utility#windows() ? 'cd /d' : 'cd')