Vim PHP 全能完成

2021-12-24 00:00:00 autocomplete vim php

我正在尝试在 Vim 中正确地获得 PHP 自动完成功能.现在当我做一个 $blog = new Blog();$blog-> 然后点击CTRL+X CTRL+O 我希望omnicompletion 返回Blog 类中的所有函数.

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.

相反,它返回整个项目的所有函数.我已经为我的项目构建了 ctags,如下所示:ctags -R *

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?

推荐答案

catchmeifyoutry 的回答 通过在您使用 omnicomplete 的行之前添加诸如 /* @var $myVar myClass */ 之类的注释来指出解决方法,但是这很麻烦,而且暂时需要写注释,你也可以自己写函数名.

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.

这是一个 Vim 脚本:phpComplete

It is a Vim script: phpComplete

你仍然需要为你的类生成一个标签文件,但你可以在文件中使用 omni complete,就像这样(从脚本页面的描述中修改);

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

相关文章