From e48824cd1d98293f5c47ea32de6620fdce681498 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Thu, 21 Apr 2016 14:08:14 +0100 Subject: [PATCH] Provide a hunk text object. --- README.mkd | 15 +++++++++++++++ autoload/gitgutter/hunk.vim | 21 +++++++++++++++++++++ doc/gitgutter.txt | 8 ++++++++ plugin/gitgutter.vim | 20 ++++++++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/README.mkd b/README.mkd index 476054e..cfc1e46 100644 --- a/README.mkd +++ b/README.mkd @@ -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 `hp`. You can of course chang nmap hv 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 GitGutterTextObjectInnerPending +omap ah GitGutterTextObjectOuterPending +xmap ih GitGutterTextObjectInnerVisual +xmap ah GitGutterTextObjectOuterVisual +``` + If you don't want vim-gitgutter to set up any mappings at all, use this: ```viml diff --git a/autoload/gitgutter/hunk.vim b/autoload/gitgutter/hunk.vim index 3d67c5e..24a9a78 100644 --- a/autoload/gitgutter/hunk.vim +++ b/autoload/gitgutter/hunk.vim @@ -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 diff --git a/doc/gitgutter.txt b/doc/gitgutter.txt index 739f7db..86db0df 100644 --- a/doc/gitgutter.txt +++ b/doc/gitgutter.txt @@ -222,6 +222,14 @@ To change the hunk-staging/undoing/previewing maps (defaults shown): nmap hp GitGutterPreviewHunk < +To change the hunk text object maps (defaults shown): +> + omap ic GitGutterTextObjectInnerPending + omap ac GitGutterTextObjectOuterPending + xmap ic GitGutterTextObjectInnerVisual + xmap ac GitGutterTextObjectOuterVisual +< + TO USE A CUSTOM GREP COMMAND To use a custom invocation for grep, use this: diff --git a/plugin/gitgutter.vim b/plugin/gitgutter.vim index 5ff57fb..c3f27b4 100644 --- a/plugin/gitgutter.vim +++ b/plugin/gitgutter.vim @@ -94,6 +94,13 @@ command -bar GitGutterUndoHunk call gitgutter#undo_hunk() command -bar GitGutterRevertHunk echomsg 'GitGutterRevertHunk is deprecated. Use GitGutterUndoHunk'call gitgutter#undo_hunk() command -bar GitGutterPreviewHunk call gitgutter#preview_hunk() +" Hunk text object +onoremap GitGutterTextObjectInnerPending :call gitgutter#hunk#text_object(1) +onoremap GitGutterTextObjectOuterPending :call gitgutter#hunk#text_object(0) +xnoremap GitGutterTextObjectInnerVisual :call gitgutter#hunk#text_object(1) +xnoremap GitGutterTextObjectOuterVisual :call gitgutter#hunk#text_object(0) + + " 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('GitGutterPreviewHunk') && maparg('hp', 'n') ==# '' nmap hp GitGutterPreviewHunk endif + + if !hasmapto('GitGutterTextObjectInnerPending') && maparg('ic', 'o') ==# '' + omap ic GitGutterTextObjectInnerPending + endif + if !hasmapto('GitGutterTextObjectOuterPending') && maparg('ac', 'o') ==# '' + omap ac GitGutterTextObjectOuterPending + endif + if !hasmapto('GitGutterTextObjectInnerVisual') && maparg('ic', 'x') ==# '' + xmap ic GitGutterTextObjectInnerVisual + endif + if !hasmapto('GitGutterTextObjectOuterVisual') && maparg('ac', 'x') ==# '' + xmap ac GitGutterTextObjectOuterVisual + endif endif " }}}