Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: restricting methods shown by omnicomplete to those present in ctags

I'm editing a simple PHP file with a class which has a few methods, if $bar is an instance of this class and I type

$bar->ctrlxctrlo

I get a popup with a lot of methods (builtin ones) in addition to those of my class and present in the ctags list. How can avoid showing all those external methods and just keep the ones defined in my class?

like image 811
Matteo Riva Avatar asked Nov 14 '22 10:11

Matteo Riva


1 Answers

I'm sure you've seen the VIM documentation (:help ft-c-omni) for this:

When using CTRL-X CTRL-O after a name without any "." or "->" it is completed from the tags file directly. This works for any identifier, also function names. If you want to complete a local variable name, which does not appear in the tags file, use CTRL-P instead.

When using CTRL-X CTRL-O after something that has "." or "->" Vim will attempt to recognize the type of the variable and figure out what members it has. This means only members valid for the variable will be listed.

When a member name already was complete, CTRL-X CTRL-O will add a "." or "->" for composite types.

If I read that correctly, the built-in functionality will, at best, only allow you to use CTRL-P and match the local variables names thus bypassing the TAGS file altogether.

What you really want is a TAGS file that is specific to the translation unit that you're currently working on (header/cpp file). Here are the steps that I would perform if I were trying to solve this problem:

  1. Create a script (or better yet, incorporate into a makefile) the automatic creation of translation unit specific TAG files.
  2. Create a command in vim that unloads existing TAGS files, reloads the file specific TAGS file, and performs regular CTRL-X CTRL-O omni-completion. If necessary, revert to the original TAGS.

Also, it seems to me like you would want to be able to call the script for #1 directly from VIM since you would be frequently changing the current translation unit as you code.

I hope that gets you started in the right direction, and I'd be happy to see / help you with an implementation =).

like image 170
reshen Avatar answered Dec 29 '22 00:12

reshen