Skip to content

Instantly share code, notes, and snippets.

@waldson
Created July 18, 2018 17:14
Show Gist options
  • Save waldson/18d4412975bf39ac64d70aadb5aeac9a to your computer and use it in GitHub Desktop.
Save waldson/18d4412975bf39ac64d70aadb5aeac9a to your computer and use it in GitHub Desktop.
FZF + Ultisnips to generate method completion
"ftplugins/php.vim
function! s:createSnippet(line)
let l:signature = matchstr(a:line, '\v\s+[a-z0-9A-Z_]+\s*\(.*\)')
let l:methodName = substitute(substitute(l:signature, '(.*$', '', 'g'), '\s', '', 'g')
let l:paramList = []
let l:params = substitute(l:signature, '\$[a-zA-Z0-9_]\+', '\=add(l:paramList, submatch(0))', 'g')
let l:templateParams = []
for p in l:paramList
let l:parameterPos = len(l:templateParams) + 1
call add(l:templateParams, '${' . l:parameterPos . ':' . p . '}')
endfor
if l:methodName !=# '__construct'
let l:template = l:methodName . '(' . join(l:templateParams, ', ') . ')$0'
else
let l:template = '(' . join(l:templateParams, ', ') . ')$0'
endif
execute "normal! a\<C-r>=UltiSnips#Anon('" . l:template . "')\<cr>"
endfunction
function! s:fzfphpmethods()
call fzf#run({
\ 'source': "ag -a --php --vimgrep '(public)? +(function\|@method) (.+) *\\(.*\\)'",
\ 'sink': function('s:createsnippet'),
\ 'down': '30%'
\})
endfunction
command! -buffer PhpMethods call <sid>fzfPhpMethods()
inoremap <buffer> <C-l> <esc>:PhpMethods<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment