Provide a hunk text object.

This commit is contained in:
Andy Stewart
2016-04-21 14:08:14 +01:00
parent b3db866aab
commit e48824cd1d
4 changed files with 64 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ Features:
* Ensures signs are always as up to date as possible (but without running more than necessary).
* Quick jumping between blocks of changed lines ("hunks").
* Stage/undo/preview individual hunks.
* Provides a hunk text object.
* Diffs against index (default) or any commit.
* Handles line endings correctly, even with repos that do CRLF conversion.
* Optional line highlighting.
@@ -134,6 +135,20 @@ And you can preview a hunk's changes with `<Leader>hp`. You can of course chang
nmap <Leader>hv <Plug>GitGutterPreviewHunk
```
A hunk text object is provided which works in visual and operator-pending modes.
- `ic` operates on all lines in the current hunk.
- `ac` operates on all lines in the current hunk and any trailing empty lines.
To re-map these, for example to `ih` and `ah`:
```viml
omap ih <Plug>GitGutterTextObjectInnerPending
omap ah <Plug>GitGutterTextObjectOuterPending
xmap ih <Plug>GitGutterTextObjectInnerVisual
xmap ah <Plug>GitGutterTextObjectOuterVisual
```
If you don't want vim-gitgutter to set up any mappings at all, use this:
```viml

View File

@@ -107,3 +107,24 @@ function! gitgutter#hunk#line_adjustment_for_current_hunk()
endfor
return adj
endfunction
function! gitgutter#hunk#text_object(inner)
let hunk = gitgutter#hunk#current_hunk()
if empty(hunk)
return
endif
let [first_line, last_line] = [hunk[2], hunk[2] + hunk[3] - 1]
if ! a:inner
let lnum = last_line
let eof = line('$')
while lnum < eof && empty(getline(lnum + 1))
let lnum +=1
endwhile
let last_line = lnum
endif
execute 'normal! 'first_line.'GV'.last_line.'G'
endfunction

View File

@@ -222,6 +222,14 @@ To change the hunk-staging/undoing/previewing maps (defaults shown):
nmap <Leader>hp <Plug>GitGutterPreviewHunk
<
To change the hunk text object maps (defaults shown):
>
omap ic <Plug>GitGutterTextObjectInnerPending
omap ac <Plug>GitGutterTextObjectOuterPending
xmap ic <Plug>GitGutterTextObjectInnerVisual
xmap ac <Plug>GitGutterTextObjectOuterVisual
<
TO USE A CUSTOM GREP COMMAND
To use a custom invocation for grep, use this:

View File

@@ -94,6 +94,13 @@ command -bar GitGutterUndoHunk call gitgutter#undo_hunk()
command -bar GitGutterRevertHunk echomsg 'GitGutterRevertHunk is deprecated. Use GitGutterUndoHunk'<Bar>call gitgutter#undo_hunk()
command -bar GitGutterPreviewHunk call gitgutter#preview_hunk()
" Hunk text object
onoremap <silent> <Plug>GitGutterTextObjectInnerPending :<C-U>call gitgutter#hunk#text_object(1)<CR>
onoremap <silent> <Plug>GitGutterTextObjectOuterPending :<C-U>call gitgutter#hunk#text_object(0)<CR>
xnoremap <silent> <Plug>GitGutterTextObjectInnerVisual :<C-U>call gitgutter#hunk#text_object(1)<CR>
xnoremap <silent> <Plug>GitGutterTextObjectOuterVisual :<C-U>call gitgutter#hunk#text_object(0)<CR>
" Returns the git-diff hunks for the file or an empty list if there
" aren't any hunks.
"
@@ -156,6 +163,19 @@ if g:gitgutter_map_keys
if !hasmapto('<Plug>GitGutterPreviewHunk') && maparg('<Leader>hp', 'n') ==# ''
nmap <Leader>hp <Plug>GitGutterPreviewHunk
endif
if !hasmapto('<Plug>GitGutterTextObjectInnerPending') && maparg('ic', 'o') ==# ''
omap ic <Plug>GitGutterTextObjectInnerPending
endif
if !hasmapto('<Plug>GitGutterTextObjectOuterPending') && maparg('ac', 'o') ==# ''
omap ac <Plug>GitGutterTextObjectOuterPending
endif
if !hasmapto('<Plug>GitGutterTextObjectInnerVisual') && maparg('ic', 'x') ==# ''
xmap ic <Plug>GitGutterTextObjectInnerVisual
endif
if !hasmapto('<Plug>GitGutterTextObjectOuterVisual') && maparg('ac', 'x') ==# ''
xmap ac <Plug>GitGutterTextObjectOuterVisual
endif
endif
" }}}