重构 C/C++在 Vim 中(例如像 Eclipse 中那样的方法提取)
Vim 中是否有任何插件或内置方法可以对 C 或 C++ 代码进行重构,类似于 Eclipse 中的重构工具?
我特别热衷于 Eclipse 中的提取方法重构工具,它将确定新方法的参数,并且通常还会猜测用作返回值的变量。
Are there any plugins or built-in methods in Vim for performing refactoring on C or C++ code, something like the refactoring tools in Eclipse?
I'm especially keen on the extract method refactoring tool from Eclipse that will determine parameters from new methods and typically also guess a variable to use as the return value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,虽然 Vim 是一个很好的编辑环境,并且可以通过很多方式进行自定义(代码折叠、语法着色、宏扩展等),但其中大多数都是在语法级别而不是语义级别上完成的。即使代码折叠也只是匹配相反的大括号。
要进行正确的重构,您必须拥有大量有关 AST 的语义知识、在哪个作用域中声明哪些变量等等。像 Eclipse 这样的 IDE 会建立每个词法作用域中定义的变量的缓存,以便它们可以快速参考它们的使用位置,以确定要重命名的内容和位置。
这并不是说你不能从语法上做某些事情;而是说你不能做一些事情。毕竟,人们可以很容易地取出一段代码并将其放入一个单独的函数中。您甚至可以猜测一些参数(例如,找到变量列表,找出哪些变量具有本地声明,删除它们,剩下的就是您的参数。但是 Eclipse 还可以做其他事情,例如确定是否有任何变量是在函数中进行修改,并确保它们通过返回值传回。它还会检查是否有任何抛出的异常,并将它们添加到列表中,
而您可能能够在 Vim 中近似处理其中的一些异常。你确实无法在仅使用 Vim 的环境中使用它,你可以在 Eclipse 中使用类似 Vim 的键绑定,或者查看 eclim。
这不仅提供了类似 Eclipse 的环境,而且它就是 Eclipse。但您仍然可以获得 vim 的导航和文本编辑功能。听起来这可能适合您的需求,尽管 重构支持 上的文档并未指出它提供了提取方法功能。
No, although Vim is a good environment for editing, and can be customised in a lot of ways (code folding, syntax colouring, macro expansion etc.) most of these are done on the syntax level, rather than the semantic level. Even the code folding just matches up opposing braces.
To do a proper refactoring, you have to have a lot of semantic knowledge about the AST, what variables are declared in which scope, and so on. IDEs like Eclipse build up a cache of the variables defined in each lexical scope, so that they can quickly refer back to where they are used in terms of determining what to rename and where.
That's not to say that you can't do some things syntactically; after all, one can just take out a block of code and put it into a separate function easily enough. You might even be able to guess at some parameters (e.g. find a list of the variables, find out which ones have local declarations, remove them and what's left are your parameters. But Eclipse also does other things—like figuring out whether any variables are modified in the function, and ensuring they're passed back by the return value. It also checks for any thrown exceptions, and add them to the list.
The net effect is that whilst you may be able to approximate some of these in Vim, you really aren't going to be able to get this working in a Vim-only enviornment. You could either use a Vim-like keybinding in Eclipse proper, or look at eclim. From the home page:
This not only gives an Eclipse-like environment, it is Eclipse. But you still get the navigation and text editing features of vim. It sounds like this might suit your needs, although the documentation on refactoring support doesn't indicate that it provides an extract method functionality.
我编写了一个通用的重构插件。 C++ 是主要处理的语言之一(因为它是我工作中的主要语言)。支持方法提取。
对于 C++,该插件能够(感谢 ctags)推断出大部分(但不幸的是并非总是全部 - 感谢 ctags...)进出提取函数的变量。
我仍然需要编写一个小对话框来选择如何交换输入/输出变量(常量引用、右值引用、复制、指针、元组、结构等)(顺便说一句,作为 GUI,欢迎提供帮助不是我的事^^')。
I've written a generic refactoring plugin. C++ is one of the primary languages handled (as it's my primary language at work). Method extraction is supported.
For C++, the plugin is able (thanks to ctags) to deduce most (but unfortunately not always all -- thanks to ctags...) of the variables coming in and out of the extracted function.
I still have to write a little dialog box to select how the in/out variables shall be exchanged (const ref, rvalue ref, copy, pointer, tuples, struct, and so on) (BTW, help is welcome as GUIs are not my thing ^^').
在对 vim 进行了全面搜索并针对 C++ 进行了重构之后,这是我想出的最佳解决方案。
这是我发现的 C++ 开发、调试和重构的最佳组合。让我的速度至少提高 3 倍 - 5 倍。希望它也对您有帮助。
After searching high and low for vim with refactoring for C++, this is the best solution I have come up with.
This is the best combination I found for C++ development, debugging and refactoring. Makes me at least 3x - 5x faster. Hope it helps you as well.