Skip to content

Commit

Permalink
Warn when cursor not in a hunk.
Browse files Browse the repository at this point in the history
  • Loading branch information
airblade committed Mar 5, 2015
1 parent 1e97331 commit 186dada
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
50 changes: 31 additions & 19 deletions autoload/gitgutter.vim
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,15 @@ function! gitgutter#stage_hunk()
" It doesn't make sense to stage a hunk otherwise.
silent write

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"
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
Expand All @@ -172,11 +176,15 @@ function! gitgutter#revert_hunk()
" It doesn't make sense to stage a hunk otherwise.
silent write

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)
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
" reload file
silent edit
endif

silent! call repeat#set("\<Plug>GitGutterRevertHunk", -1)<CR>
endif
Expand All @@ -186,19 +194,23 @@ function! gitgutter#preview_hunk()
if gitgutter#utility#is_active()
silent write

let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk('preview')
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
if !&previewwindow
execute 'bo ' . &previewheight . ' new'
set previewwindow
endif
silent! wincmd P
if !&previewwindow
execute 'bo ' . &previewheight . ' new'
set previewwindow
endif

setlocal noro modifiable filetype=diff buftype=nofile bufhidden=delete noswapfile
execute "%delete_"
call append(0, split(diff_for_hunk, "\n"))
setlocal noro modifiable filetype=diff buftype=nofile bufhidden=delete noswapfile
execute "%delete_"
call append(0, split(diff_for_hunk, "\n"))

wincmd p
wincmd p
endif
endif
endfunction

Expand Down
8 changes: 3 additions & 5 deletions autoload/gitgutter/hunk.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -75,9 +75,7 @@ function! gitgutter#hunk#current_hunk()
endif
endfor

if len(current_hunk) == 4
return current_hunk
endif
return current_hunk
endfunction

function! gitgutter#hunk#cursor_in_hunk(hunk)
Expand Down

0 comments on commit 186dada

Please sign in to comment.