Skip to content

Instantly share code, notes, and snippets.

@drasill
Last active November 28, 2017 09:38
Show Gist options
  • Save drasill/fff6c596ea56a07ba4046e6835d217ae to your computer and use it in GitHub Desktop.
Save drasill/fff6c596ea56a07ba4046e6835d217ae to your computer and use it in GitHub Desktop.
PHP function textobject
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
@drasill
Copy link
Author

drasill commented Nov 28, 2017

To use it with vim-plug :

Plug 'https://gist.github.com/drasill/fff6c596ea56a07ba4046e6835d217ae.git',
    \ { 'as': 'vim-php-function-object', 'do': 'mkdir -p plugin; cp -f *.vim plugin/' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment