s5s
s5s

Reputation: 12154

ctags c++ finds the first instance of function regardless of class

I use ctags (-e) and it works very well. However, when I search for a member function ctags returns the first definition it finds. For example:

ClassA classA;
classA.Initialize();
classA.DoSomething();

ClassB classB;
classB.Initialize();
classB.PerformAction();

If I search for the tag Initialize() (M-. or g^]) on classA.Initialize() I expect to go to ClassA::Initialize(). If it so happens that there is another class with the same method and its tag appears before classA's tag in the tag file then (let's say that's ClassB::Initialize()) I get taken for a ride to ClassB::Initialize().

However, if when I type M-. and I ammend the tag I want to visit to ClassA::Initialize() then I go to ClassA::Initialize(). Is there a way to make this automatic?

This behaviour is consistent across emacs and vim.

Furthermore, with GNU global (gtags) will not even find ClassA::Initialize(). It will just print out all classes that have an Initialize() method.

EDIT:

If the question is yet not clear I'm asking whether there is a plugin which will examine the method (e.g. is it a member function which is accessed via . or ->) and if it is will find the type of object and then look for Object::Method instead of for method.

This is a simple functionality. For example:

  1. Check if tag is a method.
  2. If it is a method is it a member of a class.
  3. If a member of a class parse code before current cursor and find declaration (ClassA classA).
  4. Look for ClassA::Initialize() if all of the above is true instead of Initialize().

Is that any clearer?

Upvotes: 0

Views: 931

Answers (1)

romainl
romainl

Reputation: 196596

Ctags only indexes your code. You only use it to generate a tags file while the searching is done by your editor. How it does the searching is your editor's business and ctags has nothing to do with it.

When you do <C-]> over a method name, Vim jumps to the first match it finds. Because it has no means to analyse your code and your thoughts, it can't decide what match you actually want.

The closest thing it has is the notion of "priority", see :h tag-priority, but it won't really help you.

And the usual way to deal with that limitation is to use a command that lists all the matches, like g] or g<C-]>.

Upvotes: 1

Related Questions