Files
vim-gitgutter/autoload/gitgutter/hunk.vim
Andy Stewart e5eb9e6ecf 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.
2014-10-16 11:50:20 +02:00

97 lines
2.1 KiB
VimL

" number of lines [added, modified, removed]
let s:summary = [0, 0, 0]
let s:hunks = []
function! gitgutter#hunk#set_hunks(hunks)
let s:hunks = a:hunks
endfunction
function! gitgutter#hunk#hunks()
return s:hunks
endfunction
function! gitgutter#hunk#summary()
return s:summary
endfunction
function! gitgutter#hunk#reset()
let s:summary = [0, 0, 0] " TODO: is bling/airline expecting [-1, -1, -1]?
endfunction
function! gitgutter#hunk#increment_lines_added(count)
let s:summary[0] += a:count
endfunction
function! gitgutter#hunk#increment_lines_modified(count)
let s:summary[1] += a:count
endfunction
function! gitgutter#hunk#increment_lines_removed(count)
let s:summary[2] += a:count
endfunction
function! gitgutter#hunk#next_hunk(count)
if gitgutter#utility#is_active()
let current_line = line('.')
let hunk_count = 0
for hunk in s:hunks
if hunk[2] > current_line
let hunk_count += 1
if hunk_count == a:count
execute 'normal!' hunk[2] . 'G'
break
endif
endif
endfor
endif
endfunction
function! gitgutter#hunk#prev_hunk(count)
if gitgutter#utility#is_active()
let current_line = line('.')
let hunk_count = 0
for hunk in reverse(copy(s:hunks))
if hunk[2] < current_line
let hunk_count += 1
if hunk_count == a:count
let target = hunk[2] == 0 ? 1 : hunk[2]
execute 'normal!' target . 'G'
break
endif
endif
endfor
endif
endfunction
" Returns the hunk the cursor is currently in or 0 if the cursor isn't in a
" hunk.
function! gitgutter#hunk#current_hunk()
let current_hunk = []
for hunk in s:hunks
if gitgutter#hunk#cursor_in_hunk(hunk)
let current_hunk = hunk
break
endif
endfor
if len(current_hunk) == 4
return 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