1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-09 10:00:04 +08:00

feat(git): use notify api to display error

`Git log` should not open log buffer for non-git repository files
This commit is contained in:
wsdjeg 2022-05-19 12:15:28 +08:00
parent ba3d524c13
commit ff2c5a4d6c

View File

@ -1,120 +1,125 @@
let s:JOB = SpaceVim#api#import('job') let s:JOB = SpaceVim#api#import('job')
let s:BUFFER = SpaceVim#api#import('vim#buffer') let s:BUFFER = SpaceVim#api#import('vim#buffer')
let s:NOTI = SpaceVim#api#import('notify')
let g:git_log_pretty = 'tformat:%Cred%h%Creset - %s %Cgreen(%an %ad)%Creset' let g:git_log_pretty = 'tformat:%Cred%h%Creset - %s %Cgreen(%an %ad)%Creset'
let s:bufnr = -1
function! git#log#run(...) abort function! git#log#run(...) abort
if len(a:1) == 1 && a:1[0] ==# '%' if len(a:1) == 1 && a:1[0] ==# '%'
let cmd = ['git', 'log', '--graph', '--date=relative', '--pretty=' . g:git_log_pretty, expand('%')] let cmd = ['git', 'log', '--graph', '--date=relative', '--pretty=' . g:git_log_pretty, expand('%')]
else else
let cmd = ['git', 'log', '--graph', '--date=relative', '--pretty=' . g:git_log_pretty] + a:1 let cmd = ['git', 'log', '--graph', '--date=relative', '--pretty=' . g:git_log_pretty] + a:1
endif endif
let s:bufnr = s:openLogBuffer() call git#logger#info('git-log cmd:' . string(cmd))
let s:lines = [] call s:JOB.start(cmd,
call git#logger#info('git-log cmd:' . string(cmd)) \ {
call s:JOB.start(cmd, \ 'on_stderr' : function('s:on_stderr'),
\ { \ 'on_stdout' : function('s:on_stdout'),
\ 'on_stderr' : function('s:on_stderr'), \ 'on_exit' : function('s:on_exit'),
\ 'on_stdout' : function('s:on_stdout'), \ }
\ 'on_exit' : function('s:on_exit'), \ )
\ }
\ )
endfunction endfunction
function! s:on_stdout(id, data, event) abort function! s:on_stdout(id, data, event) abort
call s:BUFFER.buf_set_lines(s:bufnr, getline('$') ==# '' ? 0 : -1 , -1, 0, a:data) if !bufexists(s:bufnr)
let s:bufnr = s:openLogBuffer()
endif
call s:BUFFER.buf_set_lines(s:bufnr, getline('$') ==# '' ? 0 : -1 , -1, 0, a:data)
endfunction endfunction
function! s:on_stderr(id, data, event) abort function! s:on_stderr(id, data, event) abort
for data in a:data for data in a:data
call git#logger#info('git-log stderr:' . data) let s:NOTI.notify_max_width = strwidth(data) + 5
endfor let s:NOTI.timeout = 3000
call s:NOTI.notify(data, 'WarningMsg')
endfor
endfunction endfunction
function! s:on_exit(id, data, event) abort function! s:on_exit(id, data, event) abort
call git#logger#info('git-log exit data:' . string(a:data)) call git#logger#info('git-log exit data:' . string(a:data))
endfunction endfunction
function! s:openLogBuffer() abort function! s:openLogBuffer() abort
let bp = bufnr('%') let bp = bufnr('%')
edit git://log edit git://log
normal! "_dd normal! "_dd
setl nobuflisted setl nobuflisted
setl nomodifiable setl nomodifiable
setl nonumber norelativenumber setl nonumber norelativenumber
setl buftype=nofile setl buftype=nofile
setl bufhidden=wipe setl bufhidden=wipe
setf git-log setf git-log
nnoremap <buffer><silent> <Cr> :call <SID>show_commit()<CR> nnoremap <buffer><silent> <Cr> :call <SID>show_commit()<CR>
nnoremap <buffer><silent> q :call <SID>close_log_win()<CR> nnoremap <buffer><silent> q :call <SID>close_log_win()<CR>
return bufnr('%') return bufnr('%')
endfunction endfunction
function! git#log#complete(ArgLead, CmdLine, CursorPos) abort function! git#log#complete(ArgLead, CmdLine, CursorPos) abort
return "%\n" . join(getcompletion(a:ArgLead, 'file'), "\n") return "%\n" . join(getcompletion(a:ArgLead, 'file'), "\n")
endfunction endfunction
let s:show_commit_buffer = -1 let s:show_commit_buffer = -1
function! s:show_commit() abort function! s:show_commit() abort
let commit = matchstr(getline('.'), '^[* |\\\/_]\+\zs[a-z0-9A-Z]\+') let commit = matchstr(getline('.'), '^[* |\\\/_]\+\zs[a-z0-9A-Z]\+')
if empty(commit) if empty(commit)
return return
endif endif
if !bufexists(s:show_commit_buffer) if !bufexists(s:show_commit_buffer)
let s:show_commit_buffer = s:openShowCommitBuffer() let s:show_commit_buffer = s:openShowCommitBuffer()
endif endif
let cmd = ['git', 'show', commit] let cmd = ['git', 'show', commit]
let s:show_lines = [] let s:show_lines = []
call s:JOB.start(cmd, call s:JOB.start(cmd,
\ { \ {
\ 'on_stderr' : function('s:on_show_stderr'), \ 'on_stderr' : function('s:on_show_stderr'),
\ 'on_stdout' : function('s:on_show_stdout'), \ 'on_stdout' : function('s:on_show_stdout'),
\ 'on_exit' : function('s:on_show_exit'), \ 'on_exit' : function('s:on_show_exit'),
\ } \ }
\ ) \ )
endfunction endfunction
function! s:on_show_stdout(id, data, event) abort function! s:on_show_stdout(id, data, event) abort
for data in a:data for data in a:data
call git#logger#info('git-show stdout:' . data) call git#logger#info('git-show stdout:' . data)
endfor endfor
let s:show_lines += filter(a:data, '!empty(v:val)') let s:show_lines += filter(a:data, '!empty(v:val)')
endfunction endfunction
function! s:on_show_stderr(id, data, event) abort function! s:on_show_stderr(id, data, event) abort
for data in a:data for data in a:data
call git#logger#info('git-show stderr:' . data) call git#logger#info('git-show stderr:' . data)
endfor endfor
let s:show_lines += filter(a:data, '!empty(v:val)') let s:show_lines += filter(a:data, '!empty(v:val)')
endfunction endfunction
function! s:on_show_exit(id, data, event) abort function! s:on_show_exit(id, data, event) abort
call git#logger#info('git-show exit data:' . string(a:data)) call git#logger#info('git-show exit data:' . string(a:data))
call s:BUFFER.buf_set_lines(s:show_commit_buffer, 0 , -1, 0, s:show_lines) call s:BUFFER.buf_set_lines(s:show_commit_buffer, 0 , -1, 0, s:show_lines)
endfunction endfunction
function! s:openShowCommitBuffer() abort function! s:openShowCommitBuffer() abort
rightbelow vsplit git://show_commit rightbelow vsplit git://show_commit
normal! "_dd normal! "_dd
setl nobuflisted setl nobuflisted
setl nomodifiable setl nomodifiable
setl nonumber norelativenumber setl nonumber norelativenumber
setl buftype=nofile setl buftype=nofile
setl bufhidden=wipe setl bufhidden=wipe
setf git-diff setf git-diff
setl syntax=diff setl syntax=diff
nnoremap <buffer><silent> q :q<CR> nnoremap <buffer><silent> q :q<CR>
return bufnr('%') return bufnr('%')
endfunction endfunction
function! s:close_log_win() abort function! s:close_log_win() abort
call s:closeShowCommitWindow() call s:closeShowCommitWindow()
try try
bp bp
catch /^Vim\%((\a\+)\)\=:E85/ catch /^Vim\%((\a\+)\)\=:E85/
bd bd
endtry endtry
endfunction endfunction
function! s:closeShowCommitWindow() abort function! s:closeShowCommitWindow() abort
if bufexists(s:show_commit_buffer) if bufexists(s:show_commit_buffer)
exe 'bd ' . s:show_commit_buffer exe 'bd ' . s:show_commit_buffer
endif endif
endfunction endfunction