Vim PHP 全能完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1224838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vim PHP omni completion
提问by Karl
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+OI'd expect omnicompletion to return all the functions in the class Blog.
我正在尝试在 Vim 中正确地获得 PHP 自动完成功能。现在,当我执行 a$blog = new Blog(); $blog->然后点击时,CTRL+X CTRL+O我希望 omnicompletion 返回 class 中的所有函数Blog。
Instead, it returns all functions for the entire project. I've built ctags for my project like so: ctags -R *
相反,它返回整个项目的所有函数。我已经为我的项目构建了 ctags,如下所示:ctags -R *
Is there any way to make the autocompletion context-aware?
有没有办法让自动完成上下文感知?
回答by Jess Telford
catchmeifyoutry's answerpoints 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.
catchmeifyoutry 的回答指出了一种解决方法,例如/* @var $myVar myClass */在您使用 omnicomplete 的行之前添加注释,但是这很麻烦,而且由于编写注释所花费的时间,您也可以自己编写函数名称。
Solution: phpComplete
解决方案:phpComplete
It is a Vim script: phpComplete
它是一个 Vim 脚本: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);
你仍然需要为你的类生成一个标签文件,但你可以在文件中使用 omni complete,就像这样(根据脚本页面上的描述修改);
This patch allows for in-file checking so you don't need the comment.
$blog = new Blog; ... $blog->Blah(); // <-- complete without commentIt also allows support for singleton instantiations:
$instance = Class::getInstance(); $instance->completeMe(); // sweet completion
此补丁允许文件内检查,因此您不需要注释。
$blog = new Blog; ... $blog->Blah(); // <-- complete without comment它还允许支持单例实例化:
$instance = Class::getInstance(); $instance->completeMe(); // sweet completion
回答by ax.
" 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!
回答by catchmeifyoutry
Omnicompletion will only work if the tag file contains boththe class definition, andthe variable declaration.
如果标签文件包含Omnicompletion只会工作,既类的定义,并且变量声明。
Straightforward solution
简单的解决方案
In general that means that you will need to save and (re)generate the tagsfile afterthe class Blog { ... }and $blog = new Blog();parts, but beforetrying $blog-><C-X><C-O>.
This is because the PHP omni-complete function will look for the class declaration of the $blogvariable in the tags file.
一般来说这意味着你将需要保存和(重新)生成标签文件后的class Blog { ... }和$blog = new Blog();,但部分之前尝试$blog-><C-X><C-O>。这是因为 PHP omni-complete 函数会$blog在标签文件中查找变量的类声明。
(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:
vim 文档 ( :help ft-php-omni) 还定义了一种不需要在标签文件中索引变量的方法,而是使用前一行的特定注释:
/* @var $myVar myClass */
$myVar->
However, the class definition still doesneed 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 :(
然而,类定义仍然没有需要在标签文件,和注释,需要您要使用的全方位完整的每次。所以输入一个新的 PHP 文件仍然不会给你很好的全方位完成:(
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...
也许可以即时生成未保存缓冲区的临时标签文件(如 taglist 插件),并允许 omni-complete 也使用它??虽然我不是一个伟大的 vim 黑客...
回答by Andi
This one works as expected:
这个按预期工作:
https://github.com/shawncplus/phpcomplete.vim
https://github.com/shawncplus/phpcomplete.vim
I am just missing the function parameters in the pveview!
我只是缺少pveview中的函数参数!
回答by Thanh Nguyen
The following works better. Taken from http://weierophinney.net/matthew/archives/134-exuberant-ctags-with-PHP-in-Vim.html
以下效果更好。取自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.
即使有上述情况,似乎也存在一些问题。例如 phpcomplete 似乎不支持实例变量的方法。
$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.
在尝试让 phpcomplete 在过去几个小时内正常工作后,我对任何也在尝试的人的建议是停止。它不能很好地工作,不值得麻烦。
回答by Kaleb Pederson
In C++, I run the following to get better context sensitivity:
在 C++ 中,我运行以下命令以获得更好的上下文敏感性:
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.
它并不完美,但在 ctags 将额外信息添加到上述命令指定的标签文件后,vim 可以更好地处理完成。
回答by Matthieu
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.
我尝试了很多东西:PHPComplete、Padawan 等等。这是我能找到的最好的。
In case you are interested, I wrote as well an article how to do a PHP IDE with Vim.
回答by Aleh Kashnikau
I've created a vim pluginfor my padawan.php completion server. Checkout this videoto see how it works.
我为我的padawan.php 完成服务器创建了一个vim 插件。查看此视频以了解其工作原理。
回答by Xiaohui Lam
try
尝试
curl -L -s https://git.io/ide | sh
then relaunch your nvim. You might got code completion, and goto definition features.
然后重新启动你的 nvim。您可能会获得代码完成和转到定义功能。
*Currently, it's only available for neovim
*目前只支持neovim

