Vim:PHP 的上下文相关代码完成

Vim:PHP 的上下文相关代码完成

当我使用代码完成功能时,Vim 会给我太多选项。在类中输入 $class-> 会给我大约无数个选项,不仅有类本身的选项,还有 php 中创建的所有全局变量的选项,简而言之:一团糟。

我只想拥有来自类本身(或它从中扩展的父子类)的选项,因此上下文或范围敏感的代码完成,就像 Netbeans 一样。我该怎么做?

我当前的配置是这样的:

我正在使用 ctags,并在根目录中为我们的(大)应用程序创建了 1 个 ctags 文件。

这是我用来创建 ctags 文件的 .ctags 文件:

-R
-h ".php"
--exclude=.svn
--languages=+PHP,-JavaScript
--tag-relative=yes
--regex-PHP=/abstract\s+class\s+([^ ]+)/\1/c/
--regex-PHP=/interface\s+([^ ]+)/\1/c/
--regex-PHP=/(public\s+|static\s+|protected\s+|private\s+)\$([^ \t=]+)/\2/p/
--regex-PHP=/const\s+([^ \t=]+)/\1/d/
--regex-PHP=/final\s+(public\s+|static\s+|abstract\s+|protected\s+|private\s+)function\s+\&?\s*([^ (]+)/\2/f/
--PHP-kinds=+cdf
--fields=+iaS

这是 .vimrc 文件:

" autocomplete funcs and identifiers for languages
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

" exuberant ctags
" the magic is the ';' at end. it will make vim tags file search go up from current directory until it finds one.
set tags=projectrootdir/tags;
map <F8> :!ctags

" TagList
" :tag getUser => Jump to getUser method
" :tn (or tnext) => go to next search result
" :tp (or tprev) => to to previous search result
" :ts (or tselect) => List the current tags
" => Go back to last tag location
" +Left click => Go to definition of a method
" More info:
" http://vimdoc.sourceforge.net/htmldoc/tagsrch.html (official documentation)
" http://www.vim.org/tips/tip.php?tip_id=94 (a vim tip)
let Tlist_Ctags_Cmd = "~/bin/ctags"
let Tlist_WinWidth = 50
map <F4> :TlistToggle<cr>

"see http://vim.wikia.com/wiki/Make_Vim_completion_popup_menu_work_just_like_in_an_IDE
" will change the 'completeopt' option so that Vim's popup menu doesn't select the first completion item, but rather just inserts the longest common text of all matches
:set completeopt=longest,menuone
" will change the behavior of the <Enter> key when the popup menu is visible. In that case the Enter key will simply select the highlighted menu item, just as <C-Y> does
:inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
"
inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
  \ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'

inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
  \ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'

答案1

tags使用默认的 PHPComplete,每次执行时您都必须生成$foo = new MyClass。如果您这样做,您将获得正确的完成。

但是还有另一个 PHPComplete 脚本可以解决这个问题。你应该尝试一下

相关内容