Vim PHP omni completion
Solution 1
catchmeifyoutry's answer points out a work-around by adding a comment such as /* @var $myVar myClass */
immediately before the line on which you use omnicomplete, however this is cumbersome and for the time it takes to write the comment, you may as well have written the function name yourself.
Solution: phpComplete
It is a Vim script: phpComplete
You will still need a tags file generated for your classes, but you can then use omni complete within the file, like so (modified from the description on the script's page);
This patch allows for in-file checking so you don't need the comment.
$blog = new Blog; ... $blog->Blah(); // <-- complete without comment
It also allows support for singleton instantiations:
$instance = Class::getInstance(); $instance->completeMe(); // sweet completion
Solution 2
" 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!
Solution 3
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...
Solution 4
This one works as expected:
https://github.com/shawncplus/phpcomplete.vim
I am just missing the function parameters in the pveview!
Solution 5
The following works better. Taken from http://weierophinney.net/matthew/archives/134-exuberant-ctags-with-PHP-in-Vim.html
ctags \
-f ~/.vim/tags \
-h ".php" -R \
--exclude="\.svn" \
--totals=yes \
--tag-relative=yes \
--PHP-kinds=+ivcf \
--regex-PHP='/(abstract)?\s+class\s+([^ ]+)/\2/c/' \
--regex-PHP='/(static|abstract|public|protected|private)\s+function\s+(\&\s+)?([^ (]+)/\3/f/' \
--regex-PHP='/interface\s+([^ ]+)/\1/i/' \
--regex-PHP='/\$([a-zA-Z_][a-zA-Z0-9_]*)/\1/v/' \
Even with the above, there seems to be some issues. e.g. phpcomplete doesn't seem to support methods of instance vars.
$this->objA = new SomeClass();
$this->objA-><do_autocomplete> #fails
However,
$objA = new SomeClass();
$objA-><do_autocomplete> #works
After trying to get phpcomplete working for the last few hours my advice to anyone also trying, is to STOP. It doesn't work well and is not worth the trouble.
Admin
Updated on March 03, 2020Comments
-
Admin about 4 years
I'm trying to get PHP autocompletion right in Vim. Right now when I do a
$blog = new Blog(); $blog->
and then hitCTRL+X CTRL+O
I'd expect omnicompletion to return all the functions in the classBlog
.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?