在Vim中重构C / C ++(例如Eclipse中的方法提取) [英] Refactoring C/C++ in Vim (e.g. method extraction like in Eclipse)

查看:188
本文介绍了在Vim中重构C / C ++(例如Eclipse中的方法提取)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Vim中是否有任何插件或内置方法来执行C或C ++代码的重构,像Eclipse中的重构工具?



我特别热衷于从Eclipse中提取方法重构工具,该工具将从新方法中确定参数,并通常还会将一个变量用作返回值。

解决方案

不,虽然Vim是一个很好的编辑环境,可以通过很多方式进行定制(代码折叠,语法着色,宏扩展等),其中大部分是在语法层面上完成的,而不是语义水平。即使代码折叠只是匹配相反的大括号。



要进行适当的重构,您必须对AST有很多语义知识,哪些变量在哪个范围内被声明,等等。像Eclipse这样的IDE会建立一个在每个词汇范围内定义的变量的缓存,以便他们可以快速回溯到使用它们的位置来确定重命名以及在哪里。



这不是说你不能在语法上做一些事情;毕竟,只需拿出一个代码块就可以轻松地把它放到一个单独的函数中。您甚至可以猜测某些参数(例如,找到变量列表,找出哪些具有本地声明,删除它们以及剩下的参数),但是Eclipse也会做其他的事情,比如说是否有任何变量在函数中修改,并确保它们被返回值返回,还会检查任何抛出的异常,并将其添加到列表中。



净效果是,虽然您可能可以在Vim中接近其中的一些,但是您实际上无法使其在Vim环境中工作,您可以在Eclipse中使用类似Vim的键盘绑定或查看在 eclim 。从主页:

$ b $ eclim的主要目标是将
的Eclipse功能带到Vim
编辑器中,最初的目标是
在vim中提供Eclipse的Java功能
,但支持各种其他
$ b $ c






$ b

Eclim不是一个应用程序,而是
更多的是两个伟大的
项目的集成。第一个,Vim可以说是

中最好的文本编辑器之一。第二个Eclipse,
为各种语言的
开发提供了许多很棒的工具。每个
提供许多功能,可以
增加开发人员的生产力,但
仍然需要
的需求。 Vim缺少本机Java支持
和Eclipse中可用的许多高级功能
。 Eclipse的
另一方面,仍然需要使用
的鼠标进行许多操作,而当
与Vim相比时,提供了一个不到
的理想界面来编辑文本。



这是eclim发挥的地方。
eclim
不是试图在
Vim或Vim编辑器中编写一个IDE,而是提供一个Eclipse插件,
通过一个
的服务器来显示Eclipse的功能界面,以及一组Vim
插件,可以通过该界面与Eclipse
通信。


这不仅给了一个像Eclipse一样的环境,它 Eclipse。但是您仍然可以获得vim的导航和文本编辑功能。尽管重构支持的文档并不表示,但这听起来可能适合您的需要它提供了一种提取方法功能。


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.

解决方案

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.

这篇关于在Vim中重构C / C ++(例如Eclipse中的方法提取)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆