Skip to content

Commit

Permalink
Refactor hunk functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
airblade committed Mar 17, 2014
1 parent fd98657 commit eee8ba4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 59 deletions.
58 changes: 58 additions & 0 deletions autoload/hunk.vim
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

73 changes: 14 additions & 59 deletions plugin/gitgutter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ function! GitGutter(file, realtime)
endif
try
if !a:realtime || utility#has_fresh_changes(a:file)
let diff = diff#run_diff(a:realtime || utility#has_unsaved_changes(a:file), 1)
let s:hunks = diff#parse_diff(diff)
let modified_lines = diff#process_hunks(s:hunks)
let diff = diff#run_diff(a:realtime || utility#has_unsaved_changes(a:file), 1)
call hunk#set_hunks(diff#parse_diff(diff))
let modified_lines = diff#process_hunks(hunk#hunks())

if g:gitgutter_signs
call sign#update_signs(a:file, modified_lines)
Expand Down Expand Up @@ -175,39 +175,8 @@ command GitGutterSignsToggle call GitGutterSignsToggle()

" Hunks: jump to next/previous {{{

function! GitGutterNextHunk(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
command -count=1 GitGutterNextHunk call GitGutterNextHunk(<count>)

function! GitGutterPrevHunk(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
command -count=1 GitGutterPrevHunk call GitGutterPrevHunk(<count>)
command -count=1 GitGutterNextHunk call hunk#next_hunk(<count>)
command -count=1 GitGutterPrevHunk call hunk#prev_hunk(<count>)

" }}}

Expand All @@ -220,16 +189,9 @@ function! GitGutterStageHunk()
" It doesn't make sense to stage a hunk otherwise.
silent write

" find current hunk (i.e. which one the cursor is in)
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
" find current hunk
let current_hunk = hunk#current_hunk()
if empty(current_hunk)
return
endif

Expand All @@ -249,18 +211,11 @@ function! GitGutterRevertHunk()
if utility#is_active()
" Ensure the working copy of the file is up to date.
" It doesn't make sense to stage a hunk otherwise.
write

" find current hunk (i.e. which one the cursor is in)
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
silent write

" find current hunk
let current_hunk = hunk#current_hunk()
if empty(current_hunk)
return
endif

Expand Down Expand Up @@ -299,7 +254,7 @@ command GitGutterRevertHunk call GitGutterRevertHunk()
" `line` - refers to the line number where the change starts
" `count` - refers to the number of lines the change covers
function! GitGutterGetHunks()
return utility#is_active() ? s:hunks : []
return utility#is_active() ? hunk#hunks() : []
endfunction

" Returns an array that contains a summary of the current hunk status.
Expand Down

0 comments on commit eee8ba4

Please sign in to comment.