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:

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.

In the Macro Explorer, expand the ReformatCode module and then the MakeCodeNicer macro.

You'll see the SelectionOnly and WholeFile methods.

Open a C, C++, C#, Java, or JavaScript source file to be reformatted. Make sure it compiles successfully!

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.

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:

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

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.

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.

Share

About the Author

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

The tool is very nice and usefull , especially when different users with different styles are dealing with the same code.
I tried the tool, and therefore I would like to add a comment.
Some people are writing code by using " { " on a different line , and others put " { " on the same line: Sample :
for
{
}

or

for {
}

Me am intereseted in having on the same line , and here rise my question: It is possible to make it configurable to support both variants ?