Refactor hunk functionality.

This commit is contained in:
Andy Stewart
2014-03-17 10:48:05 +01:00
parent fd98657d2f
commit eee8ba4cff
2 changed files with 71 additions and 58 deletions

View File

@@ -1,5 +1,14 @@
" number of lines [added, modified, removed]
let s:summary = [0, 0, 0]
let s:hunks = []
function! hunk#set_hunks(hunks)
let s:hunks = a:hunks
endfunction
function! hunk#hunks()
return s:hunks
endfunction
function! hunk#summary()
return s:summary
@@ -21,4 +30,53 @@ function! hunk#increment_lines_removed(count)
let s:summary[2] += a:count
endfunction
function! hunk#next_hunk(count)
if 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! hunk#prev_hunk(count)
if 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
execute 'normal!' hunk[2] . '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! hunk#current_hunk()
let current_hunk = []
let current_line = line('.')
for hunk in s:hunks
if current_line >= hunk[2] && current_line < hunk[2] + (hunk[3] == 0 ? 1 : hunk[3])
let current_hunk = hunk
break
endif
endfor
if len(current_hunk) == 4
return current_hunk
endif
endfunction