重构 C/C++在 Vim 中(例如像 Eclipse 中那样的方法提取)

发布于 2024-08-25 19:37:11 字数 125 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洛阳烟雨空心柳 2024-09-01 19:37:11

不,虽然 Vim 是一个很好的编辑环境,并且可以通过很多方式进行自定义(代码折叠、语法着色、宏扩展等),但其中大多数都是在语法级别而不是语义级别上完成的。即使代码折叠也只是匹配相反的大括号。

要进行正确的重构,您必须拥有大量有关 AST 的语义知识、在哪个作用域中声明哪些变量等等。像 Eclipse 这样的 IDE 会建立每个词法作用域中定义的变量的缓存,以便它们可以快速参考它们的使用位置,以确定要重命名的内容和位置。

这并不是说你不能从语法上做某些事情;而是说你不能做一些事情。毕竟,人们可以很容易地取出一段代码并将其放入一个单独的函数中。您甚至可以猜测一些参数(例如,找到变量列表,找出哪些变量具有本地声明,删除它们,剩下的就是您的参数。但是 Eclipse 还可以做其他事情,例如确定是否有任何变量是在函数中进行修改,并确保它们通过返回值传回。它还会检查是否有任何抛出的异常,并将它们添加到列表中,

而您可能能够在 Vim 中近似处理其中的一些异常。你确实无法在仅使用 Vim 的环境中使用它,你可以在 Eclipse 中使用类似 Vim 的键绑定,或者查看 eclim

eclim 的主要目标是
Vim 的 Eclipse 功能
编辑。最初的目标是
提供Eclipse的java功能
在 vim 中,但支持各种其他
语言(c/c++、php、python、ruby、
css、html、xml 等)已添加
还有更多计划正在计划中。

Eclim 不是一个应用程序,而是
更多的是两个伟大的融合
项目。第一个,Vim,可以说是
最好的文本编辑器之一
存在。第二个,日食,
提供了许多很棒的工具
各种语言的开发。每个
提供了许多功能,可以
提高开发人员的生产力,但是
两者都还有待解决
想要的。 Vim 缺乏原生 Java 支持
以及许多高级功能
在 Eclipse 中可用。日食,在
另一方面,仍然需要使用
鼠标可以做很多事情,当
与 Vim 相比,提供了小于
编辑文本的理想界面。

这就是 eclim 发挥作用的地方。
而不是尝试编写 IDE
Vim 或 Eclipse、eclim 中的 Vim 编辑器
提供了一个 Eclipse 插件
通过以下方式公开 Eclipse 功能
服务器界面,以及一套Vim
与 Eclipse 通信的插件
通过该接口。

这不仅提供了类似 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:

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.

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.

三生路 2024-09-01 19:37:11

我编写了一个通用的重构插件。 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 ^^').

江南烟雨〆相思醉 2024-09-01 19:37:11

在对 vim 进行了全面搜索并针对 C++ 进行了重构之后,这是我想出的最佳解决方案。

  • Visual Studio 2013 及更高版本 - 用于 C++ 开发和调试的出色 IDE,但没有足够的重构或 vim
  • 安装 vsvim 插件 - 现在,您拥有所有 vim 导航、搜索、字符串替换等(不支持某些高级功能) )
  • 安装 Resharp 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.

  • 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文