Enable :w in preview window to stage the hunk.

This commit is contained in:
Andy Stewart
2019-08-21 10:27:23 +01:00
parent 46f15ed42a
commit 77fb3fd81e
4 changed files with 43 additions and 2 deletions

View File

@@ -198,7 +198,7 @@ To stage part of any hunk:
* preview the hunk, e.g. `<Leader>hp`;
* move to the preview window, e.g. `:wincmd P`;
* delete the lines you do not want to stage;
* stage the remaining lines, e.g. `<Leader>hs` or `:GitGutterStageHunk`.
* stage the remaining lines: either write (`:w`) the window or stage via `<Leader>hs` or `:GitGutterStageHunk`.
See the FAQ if you want to unstage staged changes.

View File

@@ -315,6 +315,10 @@ function! s:preview(hunk_diff)
call setline(1, body)
normal! gg
cnoreabbrev <buffer> <expr> w getcmdtype() == ':' && getcmdline() == 'w' ? 'GitGutterStageHunk' : 'w'
" Staging hunk from the preview window closes the window anyway.
cnoreabbrev <buffer> <expr> wq getcmdtype() == ':' && getcmdline() == 'wq' ? 'GitGutterStageHunk' : 'wq'
noautocmd wincmd p
endfunction

View File

@@ -170,7 +170,8 @@ Commands for operating on a hunk:~
To stage part of any hunk, first |GitGutterPreviewHunk|
it, then move to the preview window, delete the lines
you do not want to stage, and |GitGutterStageHunk|.
you do not want to stage, and |write| or
|GitGutterStageHunk|.
*gitgutter-:GitGutterUndoHunk*
:GitGutterUndoHunk Undo the hunk the cursor is in.

View File

@@ -548,6 +548,42 @@ function Test_hunk_stage_partial_preview_added()
endfunction
function Test_hunk_stage_preview_write()
call append(5, ['A','B','C','D'])
6
GitGutterPreviewHunk
wincmd P
" preview window
w
" original window
write
let expected = [
\ {'lnum': 6, 'name': 'GitGutterLineAdded'},
\ {'lnum': 7, 'name': 'GitGutterLineAdded'},
\ {'lnum': 8, 'name': 'GitGutterLineAdded'},
\ {'lnum': 9, 'name': 'GitGutterLineAdded'},
\ ]
call s:assert_signs(expected, 'fixture.txt')
call assert_equal([], s:git_diff())
let expected = [
\ 'diff --git a/fixture.txt b/fixture.txt',
\ 'index f5c6aff..975852f 100644',
\ '--- a/fixture.txt',
\ '+++ b/fixture.txt',
\ '@@ -5,0 +6,4 @@ e',
\ '+A',
\ '+B',
\ '+C',
\ '+D',
\ ]
call assert_equal(expected, s:git_diff_staged())
endfunction
function Test_hunk_stage_partial_preview_added_removed()
4,5delete
call append(3, ['A','B','C','D'])