在 Vim 中重构 C/C++(例如在 Eclipse 中提取方法)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2470653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Refactoring C/C++ in Vim (e.g. method extraction like in Eclipse)
提问by Bjarke Freund-Hansen
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?
Vim 中是否有任何插件或内置方法可以对 C 或 C++ 代码执行重构,例如 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.
我特别喜欢 Eclipse 的提取方法重构工具,它可以确定新方法的参数,并且通常还会猜测一个变量用作返回值。
采纳答案by AlBlue
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.
不,虽然 Vim 是一个很好的编辑环境,并且可以通过很多方式进行定制(代码折叠、语法着色、宏扩展等),但其中大部分是在语法级别完成的,而不是语义级别。甚至代码折叠也只是匹配相反的大括号。
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.
要进行适当的重构,您必须对 AST 有很多语义知识,在哪个范围内声明了哪些变量,等等。Eclipse 等 IDE 为每个词法作用域中定义的变量建立了一个缓存,这样它们就可以在确定要重命名的内容和位置时快速返回使用它们的位置。
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.
这并不是说你不能在语法上做一些事情;毕竟,人们可以很容易地取出一段代码并将其放入一个单独的函数中。您甚至可以猜测一些参数(例如,查找变量列表,找出哪些具有本地声明,将它们删除,剩下的是您的参数。但是 Eclipse 还可以做其他事情——比如确定是否有任何变量是在函数中修改,并确保它们被返回值传回。它还检查任何抛出的异常,并将它们添加到列表中。
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:
最终效果是,虽然您可以在 Vim 中近似其中的一些,但您真的无法在仅 Vim 的环境中使其工作。您可以在 Eclipse 中使用类似 Vim 的键绑定,或者查看eclim。从主页:
The primary goal of eclim is to bring Eclipse functionality to the Vim editor. The initial goal was to provide Eclipse's java functionality in vim, but support for various other languages (c/c++, php, python, ruby, css, html, xml, etc.) have been added and several more are planned.
Eclim is less of an application and more of an integration of two great projects. The first, Vim, is arguably one of the best text editors in existence. The second, Eclipse, provides many great tools for development in various languages. Each provides many features that can increase developer productivity, but both still leave something to be desired. Vim lacks native Java support and many of the advanced features available in Eclipse. Eclipse, on the other hand, still requires the use of the mouse for many things, and when compared to Vim, provides a less than ideal interface for editing text.
That is where eclim comes into play. Instead of trying to write an IDE in Vim or a Vim editor in Eclipse, eclim provides an Eclipse plug-in that exposes Eclipse features through a server interface, and a set of Vim plug-ins that communicate with Eclipse over that interface.
eclim 的主要目标是将 Eclipse 功能引入 Vim 编辑器。最初的目标是在 vim 中提供 Eclipse 的 java 功能,但已经添加了对各种其他语言(c/c++、php、python、ruby、css、html、xml 等)的支持,并且计划提供更多支持。
Eclim 与其说是一个应用程序,不如说是两个伟大项目的集成。第一个,Vim,可以说是现存最好的文本编辑器之一。第二个是 Eclipse,它为各种语言的开发提供了许多很棒的工具。每个都提供了许多可以提高开发人员生产力的功能,但仍然有一些不足之处。Vim 缺乏原生 Java 支持和 Eclipse 中可用的许多高级功能。另一方面,Eclipse 在许多方面仍然需要使用鼠标,并且与 Vim 相比,它提供了一个不太理想的文本编辑界面。
这就是 eclim 发挥作用的地方。eclim 不是尝试在 Vim 中编写 IDE 或在 Eclipse 中编写 Vim 编辑器,而是提供了一个 Eclipse 插件,该插件通过服务器接口公开 Eclipse 功能,以及一组通过该接口与 Eclipse 通信的 Vim 插件。
This not only gives an Eclipse-like environment, it isEclipse. 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 supportdoesn't indicate that it provides an extract method functionality.
这不仅提供了一个类似 Eclipse 的环境,它是Eclipse。但是您仍然可以使用 vim 的导航和文本编辑功能。听起来这可能适合您的需求,尽管有关重构支持的文档并未表明它提供了提取方法功能。
回答by Luc Hermitte
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.
我写了一个通用的重构插件。C++ 是处理的主要语言之一(因为它是我工作的主要语言)。支持方法提取。
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 ^^').
对于 C++,该插件能够(感谢 ctags)推断出大部分(但不幸的是并非总是全部——感谢 ctags...)来自提取函数的变量。我仍然需要编写一个小对话框来选择如何交换输入/输出变量(常量引用、右值引用、复制、指针、元组、结构等)(顺便说一句,欢迎帮助,因为 GUI 不是我的东西^^')。
回答by dev_nut
After searching high and low for vim with refactoring for C++, this is the best solution I have come up with.
在使用 C++ 重构搜索 vim 的高低后,这是我想出的最佳解决方案。
- Visual Studio 2013 and higher - Great IDE for C++ development and debugging, but does not have sufficient refactoring or vim
- Install the vsvim plugin - Now, you have all the vim navigation, searching, string replacement, etc. (Some advanced features are not supported)
- Install Resharp C++ - Great for refactoring at the cost of speed. It makes it a bit clunky, but if you want to infer auto generate methods/variables, rename local/global/method variables or functions, Search for usages in the solution, and more features, makes it extremely worth it.
- Visual Studio 2013 及更高版本 - 用于 C++ 开发和调试的出色 IDE,但没有足够的重构或 vim
- 安装 vsvim 插件 - 现在,您拥有所有 vim 导航、搜索、字符串替换等(不支持某些高级功能)
- 安装 Resharp C++ - 非常适合以速度为代价进行重构。这使它有点笨拙,但是如果您想推断自动生成方法/变量、重命名本地/全局/方法变量或函数、在解决方案中搜索用法以及更多功能,则非常值得。
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.
这是我为 C++ 开发、调试和重构找到的最佳组合。让我至少快 3 到 5 倍。希望它也能帮助你。