Home » Php » Vim PHP omni completion

Vim PHP omni completion

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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

Answer:

" 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)

Answer:

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…

Answer:

This one works as expected:

https://github.com/shawncplus/phpcomplete.vim

I am just missing the function parameters in the pveview!

Answer:

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+([^ ]+)//c/' \
--regex-PHP='/(static|abstract|public|protected|private)\s+function\s+(\&\s+)?([^ (]+)//f/' \
--regex-PHP='/interface\s+([^ ]+)//i/' \
--regex-PHP='/$([a-zA-Z_][a-zA-Z0-9_]*)//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.

Answer:

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.

Answer:

You can use a pretty powerful combo:

I tried a lot of stuff: PHPComplete, Padawan and so on. This is the best I could find.

In case you are interested, I wrote as well an article how to do a PHP IDE with Vim.

Answer:

I’ve created a vim plugin for my padawan.php completion server. Checkout this video to see how it works.

Answer:

try

curl -L -s https://git.io/ide | sh

then relaunch your nvim. You might got code completion, and goto definition features.

*Currently, it’s only available for neovim