Prompt user to choose hunk when overlapping.

See #556.
This commit is contained in:
Andy Stewart
2018-10-18 15:39:55 +01:00
parent 4bd95abe6a
commit 0597380f6b
3 changed files with 73 additions and 1 deletions

View File

@@ -332,8 +332,14 @@ endfunction
" Returns a diff for the current hunk. " Returns a diff for the current hunk.
function! gitgutter#diff#hunk_diff(bufnr, full_diff) " Assumes there is only 1 current hunk unless the optional argument is given,
" in which case the cursor is in two hunks and the argument specifies the one
" to choose.
"
" Optional argument: 0 (to use the first hunk) or 1 (to use the second).
function! gitgutter#diff#hunk_diff(bufnr, full_diff, ...)
let modified_diff = [] let modified_diff = []
let hunk_index = 0
let keep_line = 1 let keep_line = 1
" Don't keepempty when splitting because the diff we want may not be the " Don't keepempty when splitting because the diff we want may not be the
" final one. Instead add trailing NL at end of function. " final one. Instead add trailing NL at end of function.
@@ -341,6 +347,12 @@ function! gitgutter#diff#hunk_diff(bufnr, full_diff)
let hunk_info = gitgutter#diff#parse_hunk(line) let hunk_info = gitgutter#diff#parse_hunk(line)
if len(hunk_info) == 4 " start of new hunk if len(hunk_info) == 4 " start of new hunk
let keep_line = gitgutter#hunk#cursor_in_hunk(hunk_info) let keep_line = gitgutter#hunk#cursor_in_hunk(hunk_info)
if a:0 && hunk_index != a:1
let keep_line = 0
endif
let hunk_index += 1
endif endif
if keep_line if keep_line
call add(modified_diff, line) call add(modified_diff, line)

View File

@@ -93,6 +93,19 @@ function! s:current_hunk() abort
return current_hunk return current_hunk
endfunction endfunction
" Returns truthy if the cursor is in two hunks (which can only happen if the
" cursor is on the first line and lines above have been deleted and lines
" immediately below have been deleted) or falsey otherwise.
function! s:cursor_in_two_hunks()
let hunks = gitgutter#hunk#hunks(bufnr(''))
if line('.') == 1 && len(hunks) > 1 && hunks[0][2:3] == [0, 0] && hunks[1][2:3] == [1, 0]
return 1
endif
return 0
endfunction
" A line can be in 0 or 1 hunks, with the following exception: when the first " A line can be in 0 or 1 hunks, with the following exception: when the first
" line(s) of a file has been deleted, and the new second line (and " line(s) of a file has been deleted, and the new second line (and
" optionally below) has been deleted, the new first line is in two hunks. " optionally below) has been deleted, the new first line is in two hunks.
@@ -161,6 +174,17 @@ function! s:hunk_op(op)
if empty(s:current_hunk()) if empty(s:current_hunk())
call gitgutter#utility#warn('cursor is not in a hunk') call gitgutter#utility#warn('cursor is not in a hunk')
elseif s:cursor_in_two_hunks()
let choice = input('Choose hunk: upper or lower (u/l)? ')
" Clear input
normal! :<ESC>
if choice =~ 'u'
call a:op(gitgutter#diff#hunk_diff(bufnr, diff, 0))
elseif choice =~ 'l'
call a:op(gitgutter#diff#hunk_diff(bufnr, diff, 1))
else
call gitgutter#utility#warn('did not recognise your choice')
endif
else else
call a:op(gitgutter#diff#hunk_diff(bufnr, diff)) call a:op(gitgutter#diff#hunk_diff(bufnr, diff))
endif endif

View File

@@ -455,6 +455,42 @@ function Test_undo_nearby_hunk()
endfunction endfunction
function Test_overlapping_hunk_op()
func Answer(char)
call feedkeys(a:char."\<CR>")
endfunc
" Undo upper
execute '3d'
execute '1d'
call s:trigger_gitgutter()
normal gg
call timer_start(100, {-> Answer('u')} )
GitGutterUndoHunk
call s:trigger_gitgutter()
let expected = [
\ 'line=2 id=3000 name=GitGutterLineRemoved',
\ ]
call assert_equal(expected, s:signs('fixture.txt'))
" Undo lower
execute '1d'
call s:trigger_gitgutter()
normal gg
call timer_start(100, {-> Answer('l')} )
GitGutterUndoHunk
call s:trigger_gitgutter()
let expected = [
\ 'line=1 id=3000 name=GitGutterLineRemovedFirstLine',
\ ]
call assert_equal(expected, s:signs('fixture.txt'))
endfunction
function Test_write_option() function Test_write_option()
set nowrite set nowrite