Refactoring: all funcrefs to sy#repo#get_diff() use same boilerplate code

This commit is contained in:
Marco Hinz 2019-08-06 21:15:05 +02:00
parent b7ea1a1bb2
commit ad6d7a9635
No known key found for this signature in database
GPG Key ID: 1C980A1B657B4A4F
3 changed files with 141 additions and 158 deletions

View File

@ -75,40 +75,12 @@ function! sy#start() abort
call sy#verbose('Update is already in progress.', vcs) call sy#verbose('Update is already in progress.', vcs)
else else
call sy#verbose('Updating signs.', vcs) call sy#verbose('Updating signs.', vcs)
call sy#repo#get_diff_start(vcs, function('sy#repo#job_exit_show_signs')) call sy#repo#get_diff(vcs, function('sy#sign#set_signs'))
endif endif
endfor endfor
endif endif
endfunction endfunction
" Function: #set_signs {{{1
function! sy#set_signs(sy, vcs, diff) abort
call sy#verbose('set_signs()', a:vcs)
if a:sy.stats == [-1, -1, -1]
let a:sy.stats = [0, 0, 0]
endif
if empty(a:diff)
call sy#verbose('No changes found.', a:vcs)
let a:sy.stats = [0, 0, 0]
call sy#sign#remove_all_signs(a:sy.buffer)
return
endif
if get(g:, 'signify_line_highlight')
call sy#highlight#line_enable()
else
call sy#highlight#line_disable()
endif
call sy#sign#process_diff(a:sy, a:vcs, a:diff)
if exists('#User#Signify')
execute 'doautocmd' (s:has_doau_modeline ? '<nomodeline>' : '') 'User Signify'
endif
endfunction
" Function: #stop {{{1 " Function: #stop {{{1
function! sy#stop(bufnr) abort function! sy#stop(bufnr) abort
let sy = getbufvar(a:bufnr, 'sy') let sy = getbufvar(a:bufnr, 'sy')

View File

@ -6,7 +6,7 @@ scriptencoding utf-8
function! sy#repo#detect() abort function! sy#repo#detect() abort
for vcs in s:vcs_list for vcs in s:vcs_list
let b:sy.detecting += 1 let b:sy.detecting += 1
call sy#repo#get_diff_start(vcs, function('sy#repo#job_exit_show_signs')) call sy#repo#get_diff(vcs, function('sy#sign#set_signs'))
endfor endfor
endfunction endfunction
@ -23,7 +23,7 @@ endfunction
" Function: s:callback_nvim_exit {{{1 " Function: s:callback_nvim_exit {{{1
function! s:callback_nvim_exit(_job_id, exitval, _event) dict abort function! s:callback_nvim_exit(_job_id, exitval, _event) dict abort
call self.func(self.bufnr, self.vcs, a:exitval, self.stdoutbuf) return s:handle_diff(self, a:exitval)
endfunction endfunction
" Function: s:callback_vim_stdout {{{1 " Function: s:callback_vim_stdout {{{1
@ -41,65 +41,12 @@ function! s:callback_vim_close(channel) dict abort
endif endif
sleep 10m sleep 10m
endwhile endwhile
call self.func(self.bufnr, self.vcs, exitval, self.stdoutbuf) return s:handle_diff(self, exitval)
endfunction endfunction
" Function: #job_exit_show_signs {{{1 " Function: sy#get_diff {{{1
function! sy#repo#job_exit_show_signs(bufnr, vcs, exitval, diff) abort function! sy#repo#get_diff(vcs, func) abort
call sy#verbose('sy#repo#job_exit_show_signs()', a:vcs) call sy#verbose('sy#repo#get_diff()', a:vcs)
let sy = getbufvar(a:bufnr, 'sy')
if empty(sy)
call sy#verbose(printf('No b:sy found for %s', bufname(a:bufnr)), a:vcs)
return
elseif !empty(sy.updated_by) && sy.updated_by != a:vcs
call sy#verbose(printf('Signs already got updated by %s.', sy.updated_by), a:vcs)
return
elseif empty(sy.vcs) && sy.active
let sy.detecting -= 1
endif
call sy#repo#get_diff_{a:vcs}(sy, a:exitval, a:diff)
call setbufvar(a:bufnr, 'sy_job_id_'.a:vcs, 0)
endfunction
" Function: #job_exit_preview_hunk {{{1
function! sy#repo#job_exit_preview_hunk(_bufnr, _vcs, _exitval, diff) abort
let in_hunk = 0
let hunk = []
for line in a:diff
if in_hunk
if line[:2] == '@@ '
silent! wincmd P
if !&previewwindow
noautocmd botright new
endif
call setline(1, hunk)
silent! %foldopen!
setlocal previewwindow filetype=diff buftype=nofile bufhidden=delete
noautocmd wincmd p
return
endif
call add(hunk, line)
elseif line[:2] == '@@ ' && s:is_line_in_hunk(line)
let in_hunk = 1
endif
endfor
endfunction
function! s:is_line_in_hunk(hunkline)
let curline = line('.')
let [old_line, new_line, old_count, new_count] = sy#sign#parse_hunk(a:hunkline)
if curline >= new_line && curline <= (new_line + new_count)
return 1
endif
return 0
endfunction
" Function: sy#get_diff_start {{{1
function! sy#repo#get_diff_start(vcs, func) abort
call sy#verbose('sy#repoget_diff_start()', a:vcs)
let job_id = get(b:, 'sy_job_id_'.a:vcs) let job_id = get(b:, 'sy_job_id_'.a:vcs)
" Neovim " Neovim
@ -163,71 +110,71 @@ function! sy#repo#get_diff_start(vcs, func) abort
endif endif
endfunction endfunction
" Function: s:get_diff_end {{{1 " Function: s:handle_diff {{{1
function! s:get_diff_end(sy, found_diff, vcs, diff) abort function! s:handle_diff(options, exitval) abort
call sy#verbose('get_diff_end()', a:vcs) call sy#verbose('s:handle_diff()', a:options.vcs)
if a:found_diff
if index(a:sy.vcs, a:vcs) == -1 let sy = getbufvar(a:options.bufnr, 'sy')
let a:sy.vcs += [a:vcs] if empty(sy)
endif call sy#verbose(printf('No b:sy found for %s', bufname(a:options.bufnr)), a:options.vcs)
call sy#set_signs(a:sy, a:vcs, a:diff) return
else elseif !empty(sy.updated_by) && sy.updated_by != a:options.vcs
call sy#verbose('No valid diff found. Disabling this VCS.', a:vcs) call sy#verbose(printf('Signs already got updated by %s.', sy.updated_by), a:options.vcs)
return
elseif empty(sy.vcs) && sy.active
let sy.detecting -= 1
endif endif
let [found_diff, diff] = s:check_diff_{a:options.vcs}(a:exitval, a:options.stdoutbuf)
if found_diff
if index(sy.vcs, a:options.vcs) == -1
let sy.vcs += [a:options.vcs]
endif
call a:options.func(sy, a:options.vcs, diff)
else
call sy#verbose('No valid diff found. Disabling this VCS.', a:options.vcs)
endif
call setbufvar(a:options.bufnr, 'sy_job_id_'.a:options.vcs, 0)
endfunction endfunction
" Function: #get_diff_git {{{1 " Function: s:check_diff_git {{{1
function! sy#repo#get_diff_git(sy, exitval, diff) abort function! s:check_diff_git(exitval, diff) abort
call sy#verbose('get_diff_git()', 'git') return a:exitval ? [0, []] : [1, a:diff]
let [found_diff, diff] = a:exitval ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'git', diff)
endfunction endfunction
" Function: #get_diff_yadm {{{1 " Function: s:check_diff_yadm {{{1
function! sy#repo#get_diff_yadm(sy, exitval, diff) abort function! s:check_diff_yadm(exitval, diff) abort
call sy#verbose('get_diff_yadm()', 'yadm') return a:exitval ? [0, []] : [1, a:diff]
let [found_diff, diff] = a:exitval ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'yadm', diff)
endfunction endfunction
" Function: #get_diff_hg {{{1 " Function: s:check_diff_hg {{{1
function! sy#repo#get_diff_hg(sy, exitval, diff) abort function! s:check_diff_hg(exitval, diff) abort
call sy#verbose('get_diff_hg()', 'hg') return a:exitval ? [0, []] : [1, a:diff]
let [found_diff, diff] = a:exitval ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'hg', diff)
endfunction endfunction
" Function: #get_diff_svn {{{1 " Function: s:check_diff_svn {{{1
function! sy#repo#get_diff_svn(sy, exitval, diff) abort function! s:check_diff_svn(exitval, diff) abort
call sy#verbose('get_diff_svn()', 'svn') return a:exitval ? [0, []] : [1, a:diff]
let [found_diff, diff] = a:exitval ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'svn', diff)
endfunction endfunction
" Function: #get_diff_bzr {{{1 " Function: s:check_diff_bzr {{{1
function! sy#repo#get_diff_bzr(sy, exitval, diff) abort function! s:check_diff_bzr(exitval, diff) abort
call sy#verbose('get_diff_bzr()', 'bzr') return (a:exitval =~ '[012]') ? [1, a:diff] : [0, []]
let [found_diff, diff] = (a:exitval =~ '[012]') ? [1, a:diff] : [0, []]
call s:get_diff_end(a:sy, found_diff, 'bzr', diff)
endfunction endfunction
" Function: #get_diff_darcs {{{1 " Function: s:check_diff_darcs {{{1
function! sy#repo#get_diff_darcs(sy, exitval, diff) abort function! s:check_diff_darcs(exitval, diff) abort
call sy#verbose('get_diff_darcs()', 'darcs') return a:exitval ? [0, []] : [1, a:diff]
let [found_diff, diff] = a:exitval ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'darcs', diff)
endfunction endfunction
" Function: #get_diff_fossil {{{1 " Function: s:check_diff_fossil {{{1
function! sy#repo#get_diff_fossil(sy, exitval, diff) abort function! s:check_diff_fossil(exitval, diff) abort
call sy#verbose('get_diff_fossil()', 'fossil') return a:exitval ? [0, []] : [1, a:diff]
let [found_diff, diff] = a:exitval ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'fossil', diff)
endfunction endfunction
" Function: #get_diff_cvs {{{1 " Function: s:check_diff_cvs {{{1
function! sy#repo#get_diff_cvs(sy, exitval, diff) abort function! s:check_diff_cvs(exitval, diff) abort
call sy#verbose('get_diff_cvs()', 'cvs')
let [found_diff, diff] = [0, []] let [found_diff, diff] = [0, []]
if a:exitval == 1 if a:exitval == 1
for diffline in a:diff for diffline in a:diff
@ -239,35 +186,27 @@ function! sy#repo#get_diff_cvs(sy, exitval, diff) abort
elseif a:exitval == 0 && len(a:diff) == 0 elseif a:exitval == 0 && len(a:diff) == 0
let found_diff = 1 let found_diff = 1
endif endif
call s:get_diff_end(a:sy, found_diff, 'cvs', diff) return [found_diff, diff]
endfunction endfunction
" Function: #get_diff_rcs {{{1 " Function: s:check_diff_rcs {{{1
function! sy#repo#get_diff_rcs(sy, exitval, diff) abort function! s:check_diff_rcs(exitval, diff) abort
call sy#verbose('get_diff_rcs()', 'rcs') return (a:exitval == 2) ? [0, []] : [1, a:diff]
let [found_diff, diff] = a:exitval == 2 ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'rcs', diff)
endfunction endfunction
" Function: #get_diff_accurev {{{1 " Function: s:check_diff_accurev {{{1
function! sy#repo#get_diff_accurev(sy, exitval, diff) abort function! s:check_diff_accurev(exitval, diff) abort
call sy#verbose('get_diff_accurev()', 'accurev') return (a:exitval >= 2) ? [0, []] : [1, a:diff]
let [found_diff, diff] = (a:exitval >= 2) ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'accurev', diff)
endfunction endfunction
" Function: #get_diff_perforce {{{1 " Function: s:check_diff_perforce {{{1
function! sy#repo#get_diff_perforce(sy, exitval, diff) abort function! s:check_diff_perforce(exitval, diff) abort
call sy#verbose('get_diff_perforce()', 'perforce') return a:exitval ? [0, []] : [1, a:diff]
let [found_diff, diff] = a:exitval ? [0, []] : [1, a:diff]
call s:get_diff_end(a:sy, found_diff, 'perforce', diff)
endfunction endfunction
" Function: #get_diff_tfs {{{1 " Function: s:check_diff_tfs {{{1
function! sy#repo#get_diff_tfs(sy, exitval, diff) abort function! s:check_diff_tfs(exitval, diff) abort
call sy#verbose('get_diff_tfs()', 'tfs') return a:exitval ? [0, []] : [1, s:strip_context(a:diff)]
let [found_diff, diff] = a:exitval ? [0, []] : [1, s:strip_context(a:diff)]
call s:get_diff_end(a:sy, found_diff, 'tfs', diff)
endfunction endfunction
" Function: #get_stats {{{1 " Function: #get_stats {{{1
@ -342,10 +281,54 @@ endfunction
" Function: #preview_hunk {{{1 " Function: #preview_hunk {{{1
function! sy#repo#preview_hunk() abort function! sy#repo#preview_hunk() abort
if exists('b:sy') && has_key(b:sy, 'updated_by') if exists('b:sy') && has_key(b:sy, 'updated_by')
call sy#repo#get_diff_start(b:sy.updated_by, function('sy#repo#job_exit_preview_hunk')) call sy#repo#get_diff(b:sy.updated_by, function('s:preview_hunk'))
endif endif
endfunction endfunction
function! s:preview_hunk(_sy, vcs, diff) abort
call sy#verbose('s:preview_hunk()', a:vcs)
let in_hunk = 0
let hunk = []
for line in a:diff
if in_hunk
if line[:2] == '@@ '
break
endif
call add(hunk, line)
elseif line[:2] == '@@ ' && s:is_line_in_hunk(line)
let in_hunk = 1
endif
endfor
if !in_hunk
return
endif
silent! wincmd P
if !&previewwindow
noautocmd botright new
endif
call setline(1, hunk)
silent! %foldopen!
setlocal previewwindow filetype=diff buftype=nofile bufhidden=delete
" With :noautocmd wincmd p, the first line of the preview window would show
" the 'cursorline', although it's not focused. Use feedkeys() instead.
noautocmd call feedkeys("\<c-w>p", 'nt')
endfunction
function! s:is_line_in_hunk(hunkline)
let curline = line('.')
let [old_line, new_line, old_count, new_count] = sy#sign#parse_hunk(a:hunkline)
if curline >= new_line && curline < (new_line + new_count)
return 1
endif
return 0
endfunction
" Function: s:initialize_job {{{1 " Function: s:initialize_job {{{1
function! s:initialize_job(vcs) abort function! s:initialize_job(vcs) abort
let vcs_cmd = s:expand_cmd(a:vcs, g:signify_vcs_cmds) let vcs_cmd = s:expand_cmd(a:vcs, g:signify_vcs_cmds)

View File

@ -233,6 +233,34 @@ function! sy#sign#parse_hunk(diffline) abort
\ ] \ ]
endfunction endfunction
" Function: #set_signs {{{1
function! sy#sign#set_signs(sy, vcs, diff) abort
call sy#verbose('sy#sign#set_signs()', a:vcs)
if a:sy.stats == [-1, -1, -1]
let a:sy.stats = [0, 0, 0]
endif
if empty(a:diff)
call sy#verbose('No changes found.', a:vcs)
let a:sy.stats = [0, 0, 0]
call sy#sign#remove_all_signs(a:sy.buffer)
return
endif
if get(g:, 'signify_line_highlight')
call sy#highlight#line_enable()
else
call sy#highlight#line_disable()
endif
call sy#sign#process_diff(a:sy, a:vcs, a:diff)
if exists('#User#Signify')
execute 'doautocmd' (s:has_doau_modeline ? '<nomodeline>' : '') 'User Signify'
endif
endfunction
" Function: s:add_sign {{{1 " Function: s:add_sign {{{1
function! s:add_sign(sy, line, type, ...) abort function! s:add_sign(sy, line, type, ...) abort
call add(a:sy.lines, a:line) call add(a:sy.lines, a:line)