Skip to content

Commit eee8ba4

Browse files
committed
Refactor hunk functionality.
1 parent fd98657 commit eee8ba4

2 files changed

Lines changed: 72 additions & 59 deletions

File tree

autoload/hunk.vim

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
" number of lines [added, modified, removed]
22
let s:summary = [0, 0, 0]
3+
let s:hunks = []
4+
5+
function! hunk#set_hunks(hunks)
6+
let s:hunks = a:hunks
7+
endfunction
8+
9+
function! hunk#hunks()
10+
return s:hunks
11+
endfunction
312

413
function! hunk#summary()
514
return s:summary
@@ -21,4 +30,53 @@ function! hunk#increment_lines_removed(count)
2130
let s:summary[2] += a:count
2231
endfunction
2332

33+
function! hunk#next_hunk(count)
34+
if utility#is_active()
35+
let current_line = line('.')
36+
let hunk_count = 0
37+
for hunk in s:hunks
38+
if hunk[2] > current_line
39+
let hunk_count += 1
40+
if hunk_count == a:count
41+
execute 'normal!' hunk[2] . 'G'
42+
break
43+
endif
44+
endif
45+
endfor
46+
endif
47+
endfunction
48+
49+
function! hunk#prev_hunk(count)
50+
if utility#is_active()
51+
let current_line = line('.')
52+
let hunk_count = 0
53+
for hunk in reverse(copy(s:hunks))
54+
if hunk[2] < current_line
55+
let hunk_count += 1
56+
if hunk_count == a:count
57+
execute 'normal!' hunk[2] . 'G'
58+
break
59+
endif
60+
endif
61+
endfor
62+
endif
63+
endfunction
64+
65+
" Returns the hunk the cursor is currently in or 0 if the cursor isn't in a
66+
" hunk.
67+
function! hunk#current_hunk()
68+
let current_hunk = []
69+
let current_line = line('.')
70+
71+
for hunk in s:hunks
72+
if current_line >= hunk[2] && current_line < hunk[2] + (hunk[3] == 0 ? 1 : hunk[3])
73+
let current_hunk = hunk
74+
break
75+
endif
76+
endfor
77+
78+
if len(current_hunk) == 4
79+
return current_hunk
80+
endif
81+
endfunction
2482

plugin/gitgutter.vim

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ function! GitGutter(file, realtime)
7070
endif
7171
try
7272
if !a:realtime || utility#has_fresh_changes(a:file)
73-
let diff = diff#run_diff(a:realtime || utility#has_unsaved_changes(a:file), 1)
74-
let s:hunks = diff#parse_diff(diff)
75-
let modified_lines = diff#process_hunks(s:hunks)
73+
let diff = diff#run_diff(a:realtime || utility#has_unsaved_changes(a:file), 1)
74+
call hunk#set_hunks(diff#parse_diff(diff))
75+
let modified_lines = diff#process_hunks(hunk#hunks())
7676

7777
if g:gitgutter_signs
7878
call sign#update_signs(a:file, modified_lines)
@@ -175,39 +175,8 @@ command GitGutterSignsToggle call GitGutterSignsToggle()
175175

176176
" Hunks: jump to next/previous {{{
177177

178-
function! GitGutterNextHunk(count)
179-
if utility#is_active()
180-
let current_line = line('.')
181-
let hunk_count = 0
182-
for hunk in s:hunks
183-
if hunk[2] > current_line
184-
let hunk_count += 1
185-
if hunk_count == a:count
186-
execute 'normal!' hunk[2] . 'G'
187-
break
188-
endif
189-
endif
190-
endfor
191-
endif
192-
endfunction
193-
command -count=1 GitGutterNextHunk call GitGutterNextHunk(<count>)
194-
195-
function! GitGutterPrevHunk(count)
196-
if utility#is_active()
197-
let current_line = line('.')
198-
let hunk_count = 0
199-
for hunk in reverse(copy(s:hunks))
200-
if hunk[2] < current_line
201-
let hunk_count += 1
202-
if hunk_count == a:count
203-
execute 'normal!' hunk[2] . 'G'
204-
break
205-
endif
206-
endif
207-
endfor
208-
endif
209-
endfunction
210-
command -count=1 GitGutterPrevHunk call GitGutterPrevHunk(<count>)
178+
command -count=1 GitGutterNextHunk call hunk#next_hunk(<count>)
179+
command -count=1 GitGutterPrevHunk call hunk#prev_hunk(<count>)
211180

212181
" }}}
213182

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

223-
" find current hunk (i.e. which one the cursor is in)
224-
let current_hunk = []
225-
let current_line = line('.')
226-
for hunk in s:hunks
227-
if current_line >= hunk[2] && current_line < hunk[2] + (hunk[3] == 0 ? 1 : hunk[3])
228-
let current_hunk = hunk
229-
break
230-
endif
231-
endfor
232-
if len(current_hunk) != 4
192+
" find current hunk
193+
let current_hunk = hunk#current_hunk()
194+
if empty(current_hunk)
233195
return
234196
endif
235197

@@ -249,18 +211,11 @@ function! GitGutterRevertHunk()
249211
if utility#is_active()
250212
" Ensure the working copy of the file is up to date.
251213
" It doesn't make sense to stage a hunk otherwise.
252-
write
253-
254-
" find current hunk (i.e. which one the cursor is in)
255-
let current_hunk = []
256-
let current_line = line('.')
257-
for hunk in s:hunks
258-
if current_line >= hunk[2] && current_line < hunk[2] + (hunk[3] == 0 ? 1 : hunk[3])
259-
let current_hunk = hunk
260-
break
261-
endif
262-
endfor
263-
if len(current_hunk) != 4
214+
silent write
215+
216+
" find current hunk
217+
let current_hunk = hunk#current_hunk()
218+
if empty(current_hunk)
264219
return
265220
endif
266221

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

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

0 commit comments

Comments
 (0)