mirror of
https://github.com/airblade/vim-gitgutter.git
synced 2025-11-11 04:53:46 -05:00
Add hunk previewing.
This commit is contained in:
@@ -7,7 +7,7 @@ Features:
|
|||||||
* Shows signs for added, modified, and removed lines.
|
* Shows signs for added, modified, and removed lines.
|
||||||
* Ensures signs are always as up to date as possible (but without running more than necessary).
|
* Ensures signs are always as up to date as possible (but without running more than necessary).
|
||||||
* Quick jumping between blocks of changed lines ("hunks").
|
* Quick jumping between blocks of changed lines ("hunks").
|
||||||
* Stage/revert individual hunks.
|
* Stage/revert/preview individual hunks.
|
||||||
* Optional line highlighting.
|
* Optional line highlighting.
|
||||||
* Fully customisable (signs, sign column, line highlights, mappings, extra git-diff arguments, etc).
|
* Fully customisable (signs, sign column, line highlights, mappings, extra git-diff arguments, etc).
|
||||||
* Can be toggled on/off.
|
* Can be toggled on/off.
|
||||||
@@ -103,6 +103,12 @@ nmap <Leader>ha <Plug>GitGutterStageHunk
|
|||||||
nmap <Leader>hu <Plug>GitGutterRevertHunk
|
nmap <Leader>hu <Plug>GitGutterRevertHunk
|
||||||
```
|
```
|
||||||
|
|
||||||
|
And you can preview a hunk's changes with `<Leader>hp`. You can of course change this mapping, e.g:
|
||||||
|
|
||||||
|
```viml
|
||||||
|
nmap <Leader>hv <Plug>GitGutterPreviewHunk
|
||||||
|
```
|
||||||
|
|
||||||
If you don't want vim-gitgutter to set up any mappings at all, use this:
|
If you don't want vim-gitgutter to set up any mappings at all, use this:
|
||||||
|
|
||||||
```viml
|
```viml
|
||||||
|
|||||||
@@ -178,13 +178,18 @@ function! diff#process_modified_and_removed(modifications, from_count, to_count,
|
|||||||
let a:modifications[-1] = [a:to_line + offset - 1, 'modified_removed']
|
let a:modifications[-1] = [a:to_line + offset - 1, 'modified_removed']
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! diff#generate_diff_for_hunk(hunk)
|
function! diff#generate_diff_for_hunk(hunk, keep_header)
|
||||||
return diff#discard_hunks(diff#run_diff(0, 0), a:hunk)
|
let diff = diff#discard_hunks(diff#run_diff(0, 0), a:hunk, a:keep_header)
|
||||||
|
if !a:keep_header
|
||||||
|
" Discard summary line
|
||||||
|
let diff = join(split(diff, '\n')[1:-1], "\n")
|
||||||
|
endif
|
||||||
|
return diff
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! diff#discard_hunks(diff, hunk_to_keep)
|
function! diff#discard_hunks(diff, hunk_to_keep, keep_header)
|
||||||
let modified_diff = []
|
let modified_diff = []
|
||||||
let keep_line = 1 " start by keeping header
|
let keep_line = a:keep_header
|
||||||
for line in split(a:diff, '\n')
|
for line in split(a:diff, '\n')
|
||||||
let hunk_info = diff#parse_hunk(line)
|
let hunk_info = diff#parse_hunk(line)
|
||||||
if len(hunk_info) == 4 " start of new hunk
|
if len(hunk_info) == 4 " start of new hunk
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ function! gitgutter#stage_hunk()
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
" construct a diff
|
" construct a diff
|
||||||
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk)
|
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 1)
|
||||||
|
|
||||||
" apply the diff
|
" apply the diff
|
||||||
call system(utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk)
|
call system(utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk)
|
||||||
@@ -142,7 +142,7 @@ function! gitgutter#revert_hunk()
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
" construct a diff
|
" construct a diff
|
||||||
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk)
|
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 1)
|
||||||
|
|
||||||
" apply the diff
|
" apply the diff
|
||||||
call system(utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk)
|
call system(utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk)
|
||||||
@@ -152,4 +152,32 @@ function! gitgutter#revert_hunk()
|
|||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! gitgutter#preview_hunk()
|
||||||
|
if utility#is_active()
|
||||||
|
silent write
|
||||||
|
|
||||||
|
" find current hunk
|
||||||
|
let current_hunk = hunk#current_hunk()
|
||||||
|
if empty(current_hunk)
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
" construct a diff
|
||||||
|
let diff_for_hunk = diff#generate_diff_for_hunk(current_hunk, 0)
|
||||||
|
|
||||||
|
" preview the diff
|
||||||
|
silent! wincmd P
|
||||||
|
if !&previewwindow
|
||||||
|
execute 'bo ' . &previewheight . ' new'
|
||||||
|
set previewwindow
|
||||||
|
setlocal filetype=diff buftype=nowrite
|
||||||
|
endif
|
||||||
|
|
||||||
|
execute "%delete_"
|
||||||
|
call append(0, split(diff_for_hunk, "\n"))
|
||||||
|
|
||||||
|
wincmd p
|
||||||
|
endif
|
||||||
|
endfunction
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|||||||
@@ -105,6 +105,9 @@ Commands for staging or reverting individual hunks:
|
|||||||
:GitGutterRevertHunk *:GitGutterRevertHunk*
|
:GitGutterRevertHunk *:GitGutterRevertHunk*
|
||||||
Revert the hunk the cursor is in.
|
Revert the hunk the cursor is in.
|
||||||
|
|
||||||
|
:GitGutterPreviewHunk *:GitGutterPreviewHunk*
|
||||||
|
Preview the hunk the cursor is in.
|
||||||
|
|
||||||
===============================================================================
|
===============================================================================
|
||||||
5. CUSTOMISATION *GitGutterCustomisation*
|
5. CUSTOMISATION *GitGutterCustomisation*
|
||||||
|
|
||||||
|
|||||||
@@ -77,8 +77,9 @@ command GitGutterSignsToggle call gitgutter#signs_toggle()
|
|||||||
command -count=1 GitGutterNextHunk call hunk#next_hunk(<count>)
|
command -count=1 GitGutterNextHunk call hunk#next_hunk(<count>)
|
||||||
command -count=1 GitGutterPrevHunk call hunk#prev_hunk(<count>)
|
command -count=1 GitGutterPrevHunk call hunk#prev_hunk(<count>)
|
||||||
|
|
||||||
command GitGutterStageHunk call gitgutter#stage_hunk()
|
command GitGutterStageHunk call gitgutter#stage_hunk()
|
||||||
command GitGutterRevertHunk call gitgutter#revert_hunk()
|
command GitGutterRevertHunk call gitgutter#revert_hunk()
|
||||||
|
command GitGutterPreviewHunk call gitgutter#preview_hunk()
|
||||||
|
|
||||||
" Returns the git-diff hunks for the file or an empty list if there
|
" Returns the git-diff hunks for the file or an empty list if there
|
||||||
" aren't any hunks.
|
" aren't any hunks.
|
||||||
@@ -127,8 +128,9 @@ if g:gitgutter_map_keys
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
|
|
||||||
nnoremap <silent> <Plug>GitGutterStageHunk :GitGutterStageHunk<CR>
|
nnoremap <silent> <Plug>GitGutterStageHunk :GitGutterStageHunk<CR>
|
||||||
nnoremap <silent> <Plug>GitGutterRevertHunk :GitGutterRevertHunk<CR>
|
nnoremap <silent> <Plug>GitGutterRevertHunk :GitGutterRevertHunk<CR>
|
||||||
|
nnoremap <silent> <Plug>GitGutterPreviewHunk :GitGutterPreviewHunk<CR>
|
||||||
|
|
||||||
if g:gitgutter_map_keys
|
if g:gitgutter_map_keys
|
||||||
if !hasmapto('<Plug>GitGutterStageHunk') && maparg('<Leader>hs', 'n') ==# ''
|
if !hasmapto('<Plug>GitGutterStageHunk') && maparg('<Leader>hs', 'n') ==# ''
|
||||||
@@ -137,6 +139,9 @@ if g:gitgutter_map_keys
|
|||||||
if !hasmapto('<Plug>GitGutterRevertHunk') && maparg('<Leader>hr', 'n') ==# ''
|
if !hasmapto('<Plug>GitGutterRevertHunk') && maparg('<Leader>hr', 'n') ==# ''
|
||||||
nmap <Leader>hr <Plug>GitGutterRevertHunk
|
nmap <Leader>hr <Plug>GitGutterRevertHunk
|
||||||
endif
|
endif
|
||||||
|
if !hasmapto('<Plug>GitGutterPreviewHunk') && maparg('<Leader>hp', 'n') ==# ''
|
||||||
|
nmap <Leader>hp <Plug>GitGutterPreviewHunk
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" }}}
|
" }}}
|
||||||
|
|||||||
Reference in New Issue
Block a user