views:

1867

answers:

5

Hey,

I'm trying to get php autocompletion right in VIM. Right now when I do a $blog = new Blog(); $blog-> and then hit CTRL+X CTRL+O I'd expect omnicompletion to return all the functions in the class Blog.

Instead, it returns all functions for the entire project. I've built ctags for my project like so: ctags -R *

Is there any way to make the autocompletion context-aware?

Thanks, Karl

+1  A: 
" Assuming Vim 7 (full version) is installed,
"   adding the following to your ~/.vimrc should work.

filetype plugin on
au FileType php set omnifunc=phpcomplete#CompletePHP

" You might also find this useful
" PHP Generated Code Highlights (HTML & SQL)                                              

let php_sql_query=1                                                                                        
let php_htmlInStrings=1

" Hope this helps!

(via http://www.linuxquestions.org/questions/linux-software-2/vim-omin-completion-for-php-621940/#post3155311)

ax
That's what I have, but it's still not context aware...
Karl
+1  A: 

In C++, I run the following to get better context sensitivity:

ctags '--c++-kinds=+p' '--fields=+iaS' '--extra=+q'

It's not perfect, but after ctags adds the extra information to the tags file as specified by the above command vim handles completion better.

Kaleb Pederson
A: 

I've got the same problem. Omni completion only seems to work if you include the class file into the document you are editing. I'm not sure if theres a workaround or a proper way of configuring it.

Jonas Wouters
+1  A: 

Omnicompletion will only work if the tag file contains both the class definition, and the variable declaration.

Straightforward solution

In general that means that you will need to save and (re)generate the tags file after the class Blog { ... } and $blog = new Blog(); parts, but before trying $blog-> <C-X><C-O>. This is because the PHP omni-complete function will look for the class declaration of the $blog variable in the tags file.

(BTW if you have opened the tags file in a buffer, reload it after regenerating it.)

Alternative

The vim documentation (:help ft-php-omni) also defines a way which doesn't require the variable to be indexed in the tags file, but uses instead a specific comment on the preceding line:

/* @var $myVar myClass */
$myVar->

However, the class definition still does need to be in the tag file, and the comment is needed every time you want to use omni-complete. So typing away in a new PHP file still won't give you nice omni-completion :(

Just a thought

Maybe it is possible to generate on-the-fly a temporary tags file (like the taglist plugin) of just the unsaved buffer, and allow omni-complete to use it too?? I'm not a great vim hacker though...

catchmeifyoutry
A: 

phpComplete looks like a great answer to this problem: http://www.vim.org/scripts/script.php?script_id=3171

JessT