Posted by & filed under Tips.

Your code is beautiful, formatted to your liking and spaced just how you want it, but sometimes you have to work with other people’s code – yuck! If only there was something that would make it look like your code, instead of spaghetti. Artistic Style, or astyle as it is also known, is a great open source application that does exactly that.

So you can quickly turn this:

image

Into this:

image

We plan on implementing astyle into the editor at some point in the future, but I’m not so patient, and I decided to use our plugin mechanism to do it now, and will show you how.

There are two ways to run astyle from inside Understand, as an Interactive Report plugin, or as a custom user tool. Either way, first you’ll need to install or build astyle you can download it from here.

Interactive Report

The Interactive report integrates directly with the file context menu in Understand. Download the plugin from here (right click and save as). To install it, just move the file to SciToolsconfpluginUserIReport and restart Understand.
To run the plugin, right click on a File, select Interactive Reports and Beautify

image

At which point you’ll be presented with the options window:

image

The first field is pretty straight forward, just navigate to where the astyle executable is installed.

The rest of the dialog may seem overwhelming, but the tricky part is figuring out what your “style” is. Astyle has 5 predefined styles, and chances are that one of them will be pretty close to what you like.

image

Astyle’s documentation goes into more detail about each of these styles, but here is a rough overview:

ANSI GNU KR
ANSI style formatting and indenting. Brackets are broken, indentation is 4 spaces. Namespaces, classes, and switches are NOT indented. GNU style formatting and indenting. Brackets are broken, blocks are indented, and indentation is 2 spaces. Namespaces, classes, and switches are NOT indented. Kernighan & Ritchie style formatting and indenting. Brackets are attached, indentation is 4 spaces. Namespaces, classes, and switches are NOT indented.
namespace foospace
{
int Foo()
{
if (isBar)
{
bar();
return 1;
}
else
return 0;
}
}
namespace foospace
{
int Foo()
{
if (isBar)
{
bar();
return 1;
}
else
return 0;
}
}
namespace foospace {
int Foo() {
if (isBar) {
bar();
return 1;
} else
return 0;
}
}
Linux Java
Linux style formatting and indenting. All brackets are Linux style, indentation is 8 spaces. Namespaces, classes, and switches are NOT indented. Java style formatting and indenting. Brackets are attached, indentation is 4 spaces. Switches are NOT indented.
namespace foospace
{
int Foo()
{
if (isBar) {
bar();
return 1;
} else
return 0;
}
}
class foospace {
int Foo() {
if (isBar) {
bar();
return 1;
} else
return 0;
}
}

If one of these appeals to you as is, just select it from the “Predefined Style” section. If you want to modify one, select it and the other options in the dialog will allow you to override the default settings for a style. For more details about what the command does, select “Display the help text” in the Action section:

image

When you are ready to run it, you can either preview the changes in a separate window, or apply them directly to the source code. Of course I would suggest previewing the changes until you figure out exactly what your style is. Note that if your project uses relative paths or named roots, only the preview will work right now since the file naming is different, but you can still copy and paste :)

User Tool

If you already know what astyle command line options you want to use, you can setup a user tool  to quickly transform a file. Again, this only works for projects with absolute paths.

So here are the command line options I want to use with astyle:

astyle –style=ansi -t -B -w

To implement these as a user tool, I select Tools | User Tools | Configure

Then I create a new tool. I name it and provide the path to the astyle executable. Then I provide the parameters. The lipstick icon is optional :)

screenshot.43

I also decided to bind this to a keyboard shortcut (Ctrl Shift B). You can set the keyboard shortcuts in Tool | Options | Key Bindings.

image

Now if I run into an ugly file, I can just hit Ctrl Shift B, and bam, pretty code!