Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C++
Article

Reformat source code

Rate me:
Please Sign up or sign in to vote.
4.97/5 (34 votes)
26 Sep 20055 min read 407.4K   3.8K   82   127
A great macro for reformatting C++ source code.

Introduction

MakeCodeNicer began as a Visual C++ 6.0 macro used for reformatting C/C++ source files on the fly, right inside the editor. It was used by developers who needed to quickly reformat source files (usually written by other people) for their own use.

Visual Studio 2002 and 2003 introduced a new macro engine, which unfortunately broke MakeCodeNicer. Anders Dalvander was kind enough to convert the code so that it would compile, but for some reason, it didn't quite work as expected.

Well, I've finally taken some time to finish Anders' port so that this macro can once again be used inside Microsoft's development environment. I'm going to keep the VC6 version available for download, but since I don't have VC6 installed, I can't support that version any more.

Installation

To install the VS.NET version:

  1. For 2003, extract the zip file to your "My Documents\Visual Studio Projects\VSMacros71\". (For 2005 and later, please see the Notes section below.) Be sure to "Use folder names" when extracting.
  2. Inside Visual Studio, go to Tools/Macros/Load Macro Project.
  3. Select the ReformatCode.vsmacros file (inside the ReformatCode folder).
  4. In the Macro Explorer, expand the ReformatCode module and then the MakeCodeNicer macro.
  5. You'll see the SelectionOnly and WholeFile methods.
  6. Open a C, C++, C#, Java, or JavaScript source file to be reformatted. Make sure it compiles successfully!
  7. If you want the whole file to be reformatted, double click on the WholeFile method and sit back to let it do its thing. While it's running, you'll see an animated cassette in the system tray, which can be right-clicked to interrupt the macro.
  8. If you only want to reformat a portion of the file, select the portion, and double click on the SelectionOnly method.

What it does

Once the macro is done, you'll see that it does several things to your source code:

  • Adds extra spaces in a few places and removes them from other places:
    • Appends a space to if, for, foreach, while, switch, catch, return, throw, lock, and using statements.
    • Removes any spaces before or after method parenthesis.
    • Adds a space before and after operators (such as =, ==, etc.).
    • Appends a space to commas and semicolons (inside for loops).
    • Inserts a space between the // and the first character of inline comments.
  • Breaks single-line if, for, foreach, and while statements into multiple lines (for easier debugging).
  • Breaks up case statements into their own lines and indents them if necessary.
  • Isolates braces ({ }) into lines by themselves.
  • Removes trailing white space from all lines in the file.
  • Removes extraneous blank lines.
  • Accounts for code within quoted strings or characters.
  • Ignores code after inline comments (//).

If you've worked with MFC in the past and followed its conventions, you'll no doubt recognize this macro's formatting. It's clean and very easy to read, in my opinion.

Notes

I ported and tested this macro using C# source files, but it should work with any of the C-derived languages. If you find any problems, please let me know.

I'm also happy to report that Visual Studio .NET 2005 finally makes this macro obsolete. If you need to reformat source code, simply press Ctrl+E, D and the file will be reformatted according to your settings (inside Tools/Options - Text Editor/Language/Formatting).

Enjoy it!

Updates

5 May 2000

  • Fixed cases when code gets incorrectly unindented if one or more curly braces appear inside quoted strings. (Thanks to Bob Eastman for reporting it.)
  • Fixed cases when "operator=(...)" turns into "operator =(...)".
  • Added more up-front validations to ensure the macro can be run properly.
  • Made the macro execute on a temporary file which is then copied onto the target source code. This allows undoing of all of the macro's changes in one step. (Thanks to Nicolas Fleury for suggesting it.)

16 Feb 2001

  • Added a check for Visual C++'s "out of memory" bug which occurs when the macro is invoked on multiple very-large source files. (Thanks to Aaron Sulwer for reporting and helping me test this problem.)
  • Added code to remove spaces before and after -> operators. (Thanks to Dominic Holmes for suggesting it.)
  • Fixed erroneous space removals before certain opening parenthesis and after inlined functions. (Thanks to Joe Woodbury for reporting these.)

26 Apr 2001

  • Changed the logic that fixes less than and greater than operators to only do so when they're inside "for", "while", and "if" statements. Since these operators have so many uses in C++, it's very difficult to accurately fix them across the board. Thanks to Robin Summerhill and Thomas Freudenberg for bringing these to my attention.
  • Replaced Int() with CInt() in two locations. This should prevent a type mismatch error reported by Henning Flessner.

9 July 2002

  • Stopped multi-line preprocessor macros from being reformatted. (Thanks to Niels Harremoës for reporting it.)
  • Fixed improper splitting of lines with multiple semicolons. (Thanks to Jon for reporting it.)
  • Adjusted the FILE DESCRIPTION comment to allow the macro's description to be displayed on the "Add-ins and Macro Files" tab of the Customize box. (Thanks to Michael Martin for reporting it.)

15 Jul 2002

  • Fixed possible Type Mismatch error. (Thanks to henning flessner for reporting it and providing the solution.)
  • Stopped code containing "','," from turning into "', ',". (Thanks to Dirk for reporting it.)

16 Aug 2005

  • Ported to Visual Studio .NET 2003. Thanks to Anders Dalvander for giving me a great jump start in the effort.
  • I converted all variables to be type specific to improve performance. I also created a local variable in most methods to cache the ActiveDocument.Selection.
  • I fixed several minor issues, thanks to the new ability to debug (woo hoo).

15 Sep 2005

  • Fixed conversion of < and > operators when used for template notation. (Thanks to Eddie Parker for reporting the problem.)
  • Ported to Visual Studio .NET 2005 (Beta 2). Now all it does is run the "SmartFormat" method (which can easily be invoked with Ctrl+E, D). This macro has finally become obsolete. :-)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.

Comments and Discussions

 
GeneralRe: Problems with multi-line macros - Fixed Pin
Alvaro Mendez8-Jul-02 12:22
Alvaro Mendez8-Jul-02 12:22 
GeneralRe: Problems with multi-line macros - Fixed Pin
tux*elan28-Mar-03 2:29
tux*elan28-Mar-03 2:29 
GeneralMyFunc() { Pin
HudsonKane23-Jul-01 4:19
HudsonKane23-Jul-01 4:19 
GeneralRe: MyFunc() { Pin
Alvaro Mendez8-Jul-02 11:41
Alvaro Mendez8-Jul-02 11:41 
GeneralRe: MyFunc() { Pin
Giles15-Jul-02 2:12
Giles15-Jul-02 2:12 
GeneralRe: MyFunc() { Pin
Alexandru Savescu15-Jul-02 21:55
Alexandru Savescu15-Jul-02 21:55 
GeneralStrange Pin
Igor Proskuriakov17-Jul-01 5:55
Igor Proskuriakov17-Jul-01 5:55 
GeneralMultiple nested switch statements Pin
sun-woo chung12-Jun-01 0:08
sun-woo chung12-Jun-01 0:08 
GeneralIn this code it not run.... Pin
11-Jun-01 16:32
suss11-Jun-01 16:32 
Generalsorry ,the result is like this: Pin
11-Jun-01 16:36
suss11-Jun-01 16:36 
GeneralI'm very sorry ,but I must correct the describle of this problem... Pin
11-Jun-01 16:41
suss11-Jun-01 16:41 
QuestionBug? Pin
18-May-01 2:13
suss18-May-01 2:13 
AnswerRe: Bug? Pin
Alvaro Mendez21-May-01 6:03
Alvaro Mendez21-May-01 6:03 
GeneralAnother utility Pin
Philippe Lhoste2-May-01 23:16
Philippe Lhoste2-May-01 23:16 
GeneralRe: Another utility Pin
Igor Proskuriakov1-Aug-01 1:59
Igor Proskuriakov1-Aug-01 1:59 
Generalstarting at first column Pin
26-Apr-01 10:47
suss26-Apr-01 10:47 
GeneralUndesireble results Pin
16-Apr-01 20:03
suss16-Apr-01 20:03 
GeneralRe: Undesireble results Pin
Alvaro Mendez17-Apr-01 4:04
Alvaro Mendez17-Apr-01 4:04 
GeneralRe: Undesireble results Pin
18-Apr-01 2:20
suss18-Apr-01 2:20 
GeneralRe: Undesireble results Pin
Alvaro Mendez18-Apr-01 8:21
Alvaro Mendez18-Apr-01 8:21 
GeneralRe: Undesireble results Pin
19-Jul-01 7:29
suss19-Jul-01 7:29 
GeneralRe: Undesireble results Pin
Holger Persch14-May-01 21:57
Holger Persch14-May-01 21:57 
Generaldifferent (better?) switch statement handling Pin
Mark B. Elrod23-Mar-01 18:45
Mark B. Elrod23-Mar-01 18:45 
GeneralType mismatch error Pin
22-Feb-01 3:00
suss22-Feb-01 3:00 
GeneralDoesn't cope well with definitions of templated class instances Pin
21-Feb-01 0:18
suss21-Feb-01 0:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.