Vim PHP omni completion

前端 未结 9 1012
醉话见心
醉话见心 2020-12-07 12:44

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 omn

相关标签:
9条回答
  • 2020-12-07 12:48

    You can use a pretty powerful combo:

    • Phpactor
    • nvim-completion-manager

    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.

    0 讨论(0)
  • 2020-12-07 12:49

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

    0 讨论(0)
  • 2020-12-07 12:52

    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
    
    0 讨论(0)
  • 2020-12-07 12:52

    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.

    0 讨论(0)
  • 2020-12-07 12:55

    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...

    0 讨论(0)
  • 2020-12-07 13:01

    This one works as expected:

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

    I am just missing the function parameters in the pveview!

    0 讨论(0)
提交回复
热议问题