Fix staging of hunks coming after deleted lines.

Previously vim-gitgutter generated context-free patches and applied
those to the index.  However when staging a hunk situated after any
deleted lines, the line numbers on the patch were out by the number
of lines deleted, and without any context git would apply the patch
to the wrong part of the file in the index.

This commit ensure patches are generated with 1 line of context,
allowing git to adjust the line numbers appropriately and apply the
patch to the right location.

More lines of context would help git more to adjust line numbers; but
the more context we have the more we group together hunks we would
like to treat separately.
This commit is contained in:
Andy Stewart
2014-10-16 11:50:20 +02:00
parent 1e8201963c
commit e5eb9e6ecf
3 changed files with 32 additions and 40 deletions

View File

@@ -67,15 +67,9 @@ endfunction
" hunk.
function! gitgutter#hunk#current_hunk()
let current_hunk = []
let current_line = line('.')
for hunk in s:hunks
if current_line == 1 && hunk[2] == 0
let current_hunk = hunk
break
endif
if current_line >= hunk[2] && current_line < hunk[2] + (hunk[3] == 0 ? 1 : hunk[3])
if gitgutter#hunk#cursor_in_hunk(hunk)
let current_hunk = hunk
break
endif
@@ -86,3 +80,17 @@ function! gitgutter#hunk#current_hunk()
endif
endfunction
function! gitgutter#hunk#cursor_in_hunk(hunk)
let current_line = line('.')
if current_line == 1 && a:hunk[2] == 0
return 1
endif
if current_line >= a:hunk[2] && current_line < a:hunk[2] + (a:hunk[3] == 0 ? 1 : a:hunk[3])
return 1
endif
return 0
endfunction