Last active
November 5, 2018 05:01
-
-
Save AndrewRadev/d336d5b78931303cdb22 to your computer and use it in GitHub Desktop.
Delete surrounding function call
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Delete surrounding function call | |
" Relies on surround.vim | |
" | |
" function_call(cursor_here) => dsf => cursor_here | |
" | |
" Try `dsf` with more complicated structures: | |
" | |
" nested(function_call(cursor_here)) | |
" nested(cursor_here(chewy_center)) | |
" One::Two.new([cursor_here]) | |
" One::Two.new(Hash[cursor_here]) | |
" | |
nnoremap <silent> dsf :call <SID>DeleteSurroundingFunctionCall()<cr> | |
function! s:DeleteSurroundingFunctionCall() | |
if search('\k\+\zs[([]', 'b', line('.')) <= 0 | |
return | |
endif | |
" what's the opening bracket? | |
let opener = getline('.')[col('.') - 1] | |
" go back one word to get to the beginning of the function call | |
normal! b | |
" now we're on the function's name, see if we should move back some more | |
" e.g. Foo.bar(baz), Bar::Baz.new(qux) | |
let prefix = strpart(getline('.'), 0, col('.') - 1) | |
while prefix =~ '\(\.\|::\)$' | |
if search('\k\+', 'b', line('.')) <= 0 | |
break | |
endif | |
let prefix = strpart(getline('.'), 0, col('.') - 1) | |
endwhile | |
exe 'normal! dt'.opener | |
exe 'normal ds'.opener | |
silent! call repeat#set('dsf') | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment