Refactoring C/C++ in Vim (e.g. method extraction like in Eclipse)
Asked Answered
F

3

31

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.

Fredelia answered 18/3, 2010 at 14:41 Comment(2)
Have you seen the script linked here: #2446260 ?Melena
@Randy Morris - I tried the refactor.vim script mentioned in that thread. But it seems to be old and buggy, just throws a lot of errors at me. :(Fredelia
C
19

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.

Camilacamile answered 20/3, 2010 at 8:33 Comment(0)
P
19

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 ^^').

Pendergast answered 7/9, 2011 at 22:50 Comment(2)
A solution based on clang should be even more powerful. Many refactoring options could be supported (clang-rename exists for example, more commands would allow many features). I didn't find anything integrated with vim allowing to extract methods but it's doable: github.com/iZotope/cpp-tools/blob/master/extract-methodGunshy
@Colin Pitrat. I totally agree. IIRC since my answer a few have been startedPendergast
E
1

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.

Evered answered 27/4, 2017 at 2:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.