Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is type inferred ctags jumping possible in vim?

Tags:

syntax

vim

Given the following code:

// MyClass.h
class A {
    void foo();
}

class B {
    void foo();
}

// MyClass.cpp
void main() {
    A a();
    a.foo();
}

Given that I am using vim and have my ctags generated, if I place my cursor over the foo() in main() and hit ctrl+], I will get a list of the implementations of foo, as there are more than one. If there was only one, then it would jump immediately to that implementation.

Is there a way in vim for the type of a to be inferenced such that when I hit ctrl+], it immediately jumps to the implementation of A::foo() rather than supplying me with a list of choices? It seems like such a plugin should exist and I am just unable to find it.

Update: It appears that there is currently no solution to this problem, so I have selected exclipy's answer below. Should a solution present itself and a new answer be made available, I will update the answer to this question.

like image 730
Cory Klein Avatar asked Sep 12 '11 19:09

Cory Klein


1 Answers

What you want to do isn't possible unless Vim can actually parse the entire C++ translation unit, as a C++ compiler would. This is far beyond the scope of ctags, which uses very simple heuristics to merely tokenize the vicinity of the cursor.

So the obvious solution to this is... plug a C++ parser into Vim! There is actually a plugin called clang_complete, which already does most of the heavy lifting of hooking into the Clang C++ parser. From this base, it should be a simple matter to extend it to use the Clang API to implement jump-to-definition.

In fact, I have started working on such a feature, through two projects: clang_indexer, which crawls the source tree to build an index on disk, and my fork of clang_complete, which adds the feature of querying the index for usages of the symbol under the cursor. This is actually a bit more than what you're after because I'm aiming for a "find all references" feature (with the option of filtering the results to just definitions).

It's at a very early stage and I'm only doing it for myself, so don't expect it to be very polished solution.

like image 153
exclipy Avatar answered Sep 21 '22 15:09

exclipy