Introduce hunk text object

This commit is contained in:
Marco Hinz 2014-10-05 23:58:59 +02:00
parent 8cef26186e
commit ecd0bb0dbc
2 changed files with 48 additions and 0 deletions

View File

@ -33,6 +33,7 @@ TOC *signify-contents*
OPTIONS ........................ |signify-options| OPTIONS ........................ |signify-options|
COMMANDS ....................... |signify-commands| COMMANDS ....................... |signify-commands|
MAPPINGS ....................... |signify-mappings| MAPPINGS ....................... |signify-mappings|
OBJECTS ........................ |signify-objects|
COLORS ......................... |signify-colors| COLORS ......................... |signify-colors|
============================================================================== ==============================================================================
@ -279,6 +280,21 @@ Default mapping: <leader>gt
Toggle line highlighting for lines containing changes. Toggle line highlighting for lines containing changes.
Default mapping: <leader>gh Default mapping: <leader>gh
==============================================================================
OBJECTS *signify-objects*
Sy also provides text objects that operate on the current hunk:
>
ic
>
Operate on all the lines of the current hunk.
>
ac
<
Does the same as ic and also removes all trailing empty lines.
NOTE: Don't be surprised that this also works with "deleted lines".
============================================================================== ==============================================================================
COLORS *signify-colors* COLORS *signify-colors*

View File

@ -89,3 +89,35 @@ function! s:save()
write write
endif endif
endfunction endfunction
" Text object: ac / ic {{{1
function! s:hunk_text_object(emptylines) abort
if !exists('b:sy')
return
endif
let lnum = line('.')
let hunks = filter(copy(b:sy.hunks), 'v:val.start <= lnum && v:val.end >= lnum')
if empty(hunks)
return
endif
execute hunks[0].start
normal! V
if a:emptylines
let lnum = hunks[0].end
while getline(lnum+1) =~ '^$'
let lnum += 1
endwhile
execute lnum
else
execute hunks[0].end
endif
endfunction
onoremap <silent> ac :<c-u>call <sid>hunk_text_object(1)<cr>
xnoremap <silent> ac :<c-u>call <sid>hunk_text_object(1)<cr>
onoremap <silent> ic :<c-u>call <sid>hunk_text_object(0)<cr>
xnoremap <silent> ic :<c-u>call <sid>hunk_text_object(0)<cr>