From 50932df59a11775a520eb1a1b427244bad47d6e4 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 29 May 2019 13:06:05 +0100 Subject: [PATCH] Simplify async callbacks for setting repo path. --- autoload/gitgutter/utility.vim | 47 +++++++++++----------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/autoload/gitgutter/utility.vim b/autoload/gitgutter/utility.vim index 470f222..049c364 100644 --- a/autoload/gitgutter/utility.vim +++ b/autoload/gitgutter/utility.vim @@ -114,6 +114,19 @@ function! gitgutter#utility#repo_path(bufnr, shellesc) abort return a:shellesc ? gitgutter#utility#shellescape(p) : p 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 " Values of 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))) if g:gitgutter_async && gitgutter#async#available() - if has('lambda') - call gitgutter#async#execute(cmd, a:bufnr, { - \ '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 + let handler = copy(s:set_path_handler) + call gitgutter#async#execute(cmd, a:bufnr, handler) else let path = gitgutter#utility#system(cmd) if v:shell_error @@ -152,20 +149,6 @@ function! gitgutter#utility#set_repo_path(bufnr) abort endif 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 let cd = s:unc_path(a:bufnr) ? 'pushd' : (gitgutter#utility#windows() ? 'cd /d' : 'cd')