Vim: Context sensitive code completion for PHP

Posted by eddy147 on Super User See other posts from Super User or by eddy147
Published on 2012-10-08T14:02:07Z Indexed on 2012/10/09 21:45 UTC
Read the original article Hit count: 224

Filed under:
|

Vim gives me too much options when I use code completion. In a class, and type $class-> it gives me about a zillion options, so not only from the class itself but also from php, all globals ever created, in short: a mess.

I only want to have the options from the class itself (or the parent subtype class it extends from), so context or scope sensitive code completion, just like Netbeans for example. How can I do that?

My current configuration is this:

I am using ctags, and created 1 ctags file for our (big) application in the root.

This is the .ctags file I used to create the ctags file:

-R
-h ".php"
--exclude=.svn
--languages=+PHP,-JavaScript
--tag-relative=yes
--regex-PHP=/abstract\s+class\s+([^ ]+)/\1/c/
--regex-PHP=/interface\s+([^ ]+)/\1/c/
--regex-PHP=/(public\s+|static\s+|protected\s+|private\s+)\$([^ \t=]+)/\2/p/
--regex-PHP=/const\s+([^ \t=]+)/\1/d/
--regex-PHP=/final\s+(public\s+|static\s+|abstract\s+|protected\s+|private\s+)function\s+\&?\s*([^ (]+)/\2/f/
--PHP-kinds=+cdf
--fields=+iaS

This is the .vimrc file:

" autocomplete funcs and identifiers for languages
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

" exuberant ctags
" the magic is the ';' at end. it will make vim tags file search go up from current directory until it finds one.
set tags=projectrootdir/tags;
map <F8> :!ctags

" TagList
" :tag getUser => Jump to getUser method
" :tn (or tnext) => go to next search result
" :tp (or tprev) => to to previous search result
" :ts (or tselect) => List the current tags
" => Go back to last tag location
" +Left click => Go to definition of a method
" More info:
" http://vimdoc.sourceforge.net/htmldoc/tagsrch.html (official documentation)
" http://www.vim.org/tips/tip.php?tip_id=94 (a vim tip)
let Tlist_Ctags_Cmd = "~/bin/ctags"
let Tlist_WinWidth = 50
map <F4> :TlistToggle<cr>

"see http://vim.wikia.com/wiki/Make_Vim_completion_popup_menu_work_just_like_in_an_IDE
" will change the 'completeopt' option so that Vim's popup menu doesn't select the first completion item, but rather just inserts the longest common text of all matches
:set completeopt=longest,menuone
" will change the behavior of the <Enter> key when the popup menu is visible. In that case the Enter key will simply select the highlighted menu item, just as <C-Y> does
:inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
"
inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
  \ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'

inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
  \ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'

© Super User or respective owner

Related posts about php

Related posts about vim