From f9c0889a3ba57d70abc7434a874b482e8c504f27 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Thu, 27 Oct 2022 00:35:21 +0800 Subject: [PATCH] perf(git): use normal highlight for non-errors --- bundle/git.vim/autoload/git/push.vim | 96 +++++++++++++++------------- 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/bundle/git.vim/autoload/git/push.vim b/bundle/git.vim/autoload/git/push.vim index 462964bb4..bd4c63e67 100644 --- a/bundle/git.vim/autoload/git/push.vim +++ b/bundle/git.vim/autoload/git/push.vim @@ -10,70 +10,80 @@ let s:NOTI = SpaceVim#api#import('notify') function! git#push#run(...) abort - let cmd = ['git', 'push'] - if len(a:1) > 0 - let cmd += a:1 - endif - call s:JOB.start(cmd, - \ { - \ 'on_stdout' : function('s:on_stdout'), - \ 'on_stderr' : function('s:on_stderr'), - \ 'on_exit' : function('s:on_exit'), - \ } - \ ) + let cmd = ['git', 'push'] + if len(a:1) > 0 + let cmd += a:1 + endif + call s:JOB.start(cmd, { + \ 'on_stdout' : function('s:on_stdout'), + \ 'on_stderr' : function('s:on_stderr'), + \ 'on_exit' : function('s:on_exit'), + \ } + \ ) endfunction function! s:on_exit(...) abort - let data = get(a:000, 2) - if data != 0 - call s:NOTI.notify('Git push failed!', 'WarningMsg') - else - call s:NOTI.notify('Git push done!') - endif + let data = get(a:000, 2) + if data != 0 + for line in s:std_data.stderr + call s:NOTI.notify(line, 'WarningMsg') + endfor + else + for line in s:std_data.stderr + call s:NOTI.notify(line) + endfor + endif endfunction function! s:on_stdout(id, data, event) abort - for line in filter(a:data, '!empty(v:val)') - call s:NOTI.notify(line, 'Normal') - endfor + for line in filter(a:data, '!empty(v:val)') + call s:NOTI.notify(line, 'Normal') + endfor endfunction + +" https://stackoverflow.com/questions/57016157/how-to-stop-git-from-writing-non-errors-to-stderr +" +" why git push normal info to stderr + +let s:std_data = { + \ 'stderr' : [], + \ 'stdout' : [], + \ } function! s:on_stderr(id, data, event) abort - for line in filter(a:data, '!empty(v:val)') - call s:NOTI.notify(line, 'WarningMsg') - endfor + call extend(s:std_data.stderr, a:data) endfunction function! s:options() abort - return [ - \ '-u', - \ ] + return [ + \ '-u', + \ ] endfunction function! git#push#complete(ArgLead, CmdLine, CursorPos) abort - let str = a:CmdLine[:a:CursorPos-1] - if str =~# '^Git\s\+push\s\+-$' - return join(s:options(), "\n") - elseif str =~# '^Git\s\+push\s\+[^ ]*$' || str =~# '^Git\s\+push\s\+-u\s\+[^ ]*$' - return join(s:remotes(), "\n") - else - let remote = matchstr(str, '\(Git\s\+push\s\+\)\@<=[^ ]*') - return s:remote_branch(remote) - endif + let str = a:CmdLine[:a:CursorPos-1] + if str =~# '^Git\s\+push\s\+-$' + return join(s:options(), "\n") + elseif str =~# '^Git\s\+push\s\+[^ ]*$' || str =~# '^Git\s\+push\s\+-u\s\+[^ ]*$' + return join(s:remotes(), "\n") + else + let remote = matchstr(str, '\(Git\s\+push\s\+\)\@<=[^ ]*') + return s:remote_branch(remote) + endif endfunction function! s:remotes() abort - return map(systemlist('git remote'), 'trim(v:val)') + return map(systemlist('git remote'), 'trim(v:val)') endfunction function! s:remote_branch(remote) abort - let branchs = systemlist('git branch -a') - if v:shell_error - return '' - else - let branchs = join(map(filter(branchs, 'v:val =~ "\s*remotes/" . a:remote . "/[^ ]*$"'), 'trim(v:val)[len(a:remote) + 9:]'), "\n") - return branchs - endif + let branchs = systemlist('git branch -a') + if v:shell_error + return '' + else + let branchs = join(map(filter(branchs, 'v:val =~ "\s*remotes/" . a:remote . "/[^ ]*$"'), 'trim(v:val)[len(a:remote) + 9:]'), "\n") + return branchs + endif endfunction