Warn when cursor not in a hunk.

This commit is contained in:
Andy Stewart
2015-03-05 12:19:02 +01:00
parent 1e97331587
commit 186dada663
2 changed files with 34 additions and 24 deletions

View File

@@ -156,11 +156,15 @@ function! gitgutter#stage_hunk()
" It doesn't make sense to stage a hunk otherwise.
silent write
if empty(gitgutter#hunk#current_hunk())
call gitgutter#utility#warn('cursor is not in a hunk')
else
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk('stage')
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk)
" refresh gitgutter's view of buffer
silent execute "GitGutter"
endif
silent! call repeat#set("\<Plug>GitGutterStageHunk", -1)<CR>
endif
@@ -172,11 +176,15 @@ function! gitgutter#revert_hunk()
" It doesn't make sense to stage a hunk otherwise.
silent write
if empty(gitgutter#hunk#current_hunk())
call gitgutter#utility#warn('cursor is not in a hunk')
else
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk('revert')
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk)
" reload file
silent edit
endif
silent! call repeat#set("\<Plug>GitGutterRevertHunk", -1)<CR>
endif
@@ -186,6 +194,9 @@ function! gitgutter#preview_hunk()
if gitgutter#utility#is_active()
silent write
if empty(gitgutter#hunk#current_hunk())
call gitgutter#utility#warn('cursor is not in a hunk')
else
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk('preview')
silent! wincmd P
@@ -200,6 +211,7 @@ function! gitgutter#preview_hunk()
wincmd p
endif
endif
endfunction
" }}}

View File

@@ -63,8 +63,8 @@ function! gitgutter#hunk#prev_hunk(count)
endif
endfunction
" Returns the hunk the cursor is currently in or 0 if the cursor isn't in a
" hunk.
" Returns the hunk the cursor is currently in or an empty list if the cursor
" isn't in a hunk.
function! gitgutter#hunk#current_hunk()
let current_hunk = []
@@ -75,9 +75,7 @@ function! gitgutter#hunk#current_hunk()
endif
endfor
if len(current_hunk) == 4
return current_hunk
endif
endfunction
function! gitgutter#hunk#cursor_in_hunk(hunk)