mirror of
https://github.com/airblade/vim-gitgutter.git
synced 2025-11-09 03:53:47 -05:00
Refactor hunk functionality.
This commit is contained in:
@@ -1,5 +1,14 @@
|
|||||||
" number of lines [added, modified, removed]
|
" number of lines [added, modified, removed]
|
||||||
let s:summary = [0, 0, 0]
|
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()
|
function! hunk#summary()
|
||||||
return s:summary
|
return s:summary
|
||||||
@@ -21,4 +30,53 @@ function! hunk#increment_lines_removed(count)
|
|||||||
let s:summary[2] += a:count
|
let s:summary[2] += a:count
|
||||||
endfunction
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ function! GitGutter(file, realtime)
|
|||||||
try
|
try
|
||||||
if !a:realtime || utility#has_fresh_changes(a:file)
|
if !a:realtime || utility#has_fresh_changes(a:file)
|
||||||
let diff = diff#run_diff(a:realtime || utility#has_unsaved_changes(a:file), 1)
|
let diff = diff#run_diff(a:realtime || utility#has_unsaved_changes(a:file), 1)
|
||||||
let s:hunks = diff#parse_diff(diff)
|
call hunk#set_hunks(diff#parse_diff(diff))
|
||||||
let modified_lines = diff#process_hunks(s:hunks)
|
let modified_lines = diff#process_hunks(hunk#hunks())
|
||||||
|
|
||||||
if g:gitgutter_signs
|
if g:gitgutter_signs
|
||||||
call sign#update_signs(a:file, modified_lines)
|
call sign#update_signs(a:file, modified_lines)
|
||||||
@@ -175,39 +175,8 @@ command GitGutterSignsToggle call GitGutterSignsToggle()
|
|||||||
|
|
||||||
" Hunks: jump to next/previous {{{
|
" Hunks: jump to next/previous {{{
|
||||||
|
|
||||||
function! GitGutterNextHunk(count)
|
command -count=1 GitGutterNextHunk call hunk#next_hunk(<count>)
|
||||||
if utility#is_active()
|
command -count=1 GitGutterPrevHunk call hunk#prev_hunk(<count>)
|
||||||
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>)
|
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|
||||||
@@ -220,16 +189,9 @@ function! GitGutterStageHunk()
|
|||||||
" It doesn't make sense to stage a hunk otherwise.
|
" It doesn't make sense to stage a hunk otherwise.
|
||||||
silent write
|
silent write
|
||||||
|
|
||||||
" find current hunk (i.e. which one the cursor is in)
|
" find current hunk
|
||||||
let current_hunk = []
|
let current_hunk = hunk#current_hunk()
|
||||||
let current_line = line('.')
|
if empty(current_hunk)
|
||||||
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
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@@ -249,18 +211,11 @@ function! GitGutterRevertHunk()
|
|||||||
if utility#is_active()
|
if utility#is_active()
|
||||||
" Ensure the working copy of the file is up to date.
|
" Ensure the working copy of the file is up to date.
|
||||||
" It doesn't make sense to stage a hunk otherwise.
|
" It doesn't make sense to stage a hunk otherwise.
|
||||||
write
|
silent write
|
||||||
|
|
||||||
" find current hunk (i.e. which one the cursor is in)
|
" find current hunk
|
||||||
let current_hunk = []
|
let current_hunk = hunk#current_hunk()
|
||||||
let current_line = line('.')
|
if empty(current_hunk)
|
||||||
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
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
@@ -299,7 +254,7 @@ command GitGutterRevertHunk call GitGutterRevertHunk()
|
|||||||
" `line` - refers to the line number where the change starts
|
" `line` - refers to the line number where the change starts
|
||||||
" `count` - refers to the number of lines the change covers
|
" `count` - refers to the number of lines the change covers
|
||||||
function! GitGutterGetHunks()
|
function! GitGutterGetHunks()
|
||||||
return utility#is_active() ? s:hunks : []
|
return utility#is_active() ? hunk#hunks() : []
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
" Returns an array that contains a summary of the current hunk status.
|
" Returns an array that contains a summary of the current hunk status.
|
||||||
|
|||||||
Reference in New Issue
Block a user