vim 中类型推断的 ctags 跳转可能吗?
给出以下代码:
// MyClass.h
class A {
void foo();
}
class B {
void foo();
}
// MyClass.cpp
void main() {
A a();
a.foo();
}
假设我正在使用 vim 并生成了 ctag,如果我将光标放在 main()
中的 foo()
上并点击 ctrl+]
,我将获得 foo
的实现列表,因为有不止一个。如果只有一个,那么它会立即跳转到该实现。
vim 中有没有办法推断 a
的类型,这样当我按下 ctrl+]
时,它会立即跳转到 A::foo 的实现()
而不是给我提供一个选择列表?看来应该有这样的插件,只是我找不到它。
更新:看来目前这个问题还没有解决方案,所以我选择了下面exlipy的答案。如果出现解决方案并提供新答案,我将更新此问题的答案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非 Vim 能够像 C++ 编译器那样真正解析整个 C++ 翻译单元,否则您想要做的事情是不可能的。这远远超出了 ctags 的范围,它使用非常简单的启发式方法仅标记光标附近的区域。
因此,显而易见的解决方案是...将 C++ 解析器插入 Vim!实际上有一个名为 clang_complete 的插件,它已经完成了挂钩 Clang C++ 解析器的大部分繁重工作。以此为基础,扩展它以使用 Clang API 来实现跳转到定义应该是一件简单的事情。
事实上,我已经开始通过两个项目来研究这样的功能: clang_indexer,它抓取源代码树在磁盘上构建索引,以及我的 clang_complete 的分支,它添加了查询索引的功能光标下符号的用法。这实际上比您想要的要多一些,因为我的目标是“查找所有引用”功能(可以选择将结果过滤为仅定义)。
它还处于非常早期的阶段,我只是为自己做的,所以不要指望它是非常完美的解决方案。
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.