Last active
November 28, 2017 09:38
-
-
Save drasill/fff6c596ea56a07ba4046e6835d217ae to your computer and use it in GitHub Desktop.
PHP function textobject
This file contains 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
call textobj#user#plugin('phpfunction', { | |
\ '-': { | |
\ 'select-a-function': 'CurrentPhpFunctionA', | |
\ 'select-a': 'af', | |
\ 'select-i-function': 'CurrentPhpFunctionI', | |
\ 'select-i': 'if', | |
\ }, | |
\ }) | |
function! CurrentPhpFunctionI() | |
if getline('.') =~# '}' | |
normal! k | |
endif | |
normal! ]M$ | |
let e = getpos('.') | |
normal! [m | |
call search(')', 'bW') | |
normal! %0 | |
let b = getpos('.') | |
if 1 < e[1] - b[1] " is there some code? | |
return ['V', b, e] | |
else | |
return 0 | |
endif | |
endfunction | |
function! CurrentPhpFunctionA() | |
let range = CurrentPhpFunctionI() | |
if range is 0 | |
return 0 | |
endif | |
let [_, ab, ae] = range | |
let ib = ab | |
let nextLine = ae[1] + 1 | |
echo nextLine | |
let non_blank_char_exists_p = getline(nextLine) =~ '^\s*$' | |
echo non_blank_char_exists_p | |
if non_blank_char_exists_p | |
call setpos('.', ae) | |
normal! } | |
let ie = getpos('.') | |
else | |
let ie = ae | |
endif | |
if 0 <= ie[1] - ib[1] " is there some code? | |
return ['V', ib, ie] | |
else | |
return 0 | |
endif | |
endfunction | |
call textobj#user#plugin('phpfunctionwithdockblock', { | |
\ '-': { | |
\ 'select-a-function': 'CurrentPhpFunctionWdbA', | |
\ 'select-a': 'aF', | |
\ 'select-i-function': 'CurrentPhpFunctionWdbI', | |
\ 'select-i': 'iF', | |
\ }, | |
\ }) | |
function! CurrentPhpFunctionWdbA() | |
let range = CurrentPhpFunctionA() | |
if range is 0 | |
return 0 | |
endif | |
let [_, b, e] = range | |
call setpos('.', b) | |
while line('.') > 1 | |
normal k$ | |
let syns = map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")') | |
if index(syns, 'phpComment') < 0 | |
normal j | |
break | |
endif | |
endwhile | |
let nb = getpos('.') | |
if 0 <= e[1] - nb[1] " is there some code? | |
return ['V', nb, e] | |
else | |
return 0 | |
endif | |
endfunction | |
function! CurrentPhpFunctionWdbI() | |
let range = CurrentPhpFunctionI() | |
if range is 0 | |
return 0 | |
endif | |
let [_, b, e] = range | |
call setpos('.', b) | |
while line('.') > 1 | |
normal k$ | |
let syns = map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")') | |
if index(syns, 'phpComment') < 0 | |
normal j | |
break | |
endif | |
endwhile | |
let nb = getpos('.') | |
if 0 <= e[1] - nb[1] " is there some code? | |
return ['V', nb, e] | |
else | |
return 0 | |
endif | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use it with vim-plug :