Blog


Tip – Beautify your code

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 SciTools\conf\plugin\User\IReport\ 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!

Tags: , ,


9 Responses to “Tip – Beautify your code”

  1. Dave Kellogg Says:

    In addition to Astyle, I wish that there was some utility to format comments:
    1. Indent paragraph comments at the nesting level.
    2. Wrap paragraph and end-line comments.
    3. Virtally align end-line comments.
    4. Indent end-line comments at column xx.
    5. Merge adjacent end-line comments into a single multi-line comment.

  2. KevinG Says:

    Better support for comment editing is important, and I’ve sent this to the editor engineer to see what he can do. Thanks for the feedback, Dave.
    KG

  3. Leigh Says:

    Kevin,
    I’ve just spotted the blog entry.
    I’m having a problem with the Interactive Reports method. I’m not sure what you mean by ‘drag the file into Understand’. I’ve been dragging an dropping the file onto an instance of the Understand application. This seems to have no effect. I see a ‘Interactive Reports’ entry on the context menus but it’s greyed out. I’m not sure whether ‘Interactive Reports’ was there on those menus before or not.
    Regards,
    Leigh.

  4. KevinG Says:

    If the drag and drop doesn’t work, you can also save the file to sti/conf/scripts/local/.upl and Understand will find it after a restart.
    Thanks,
    KevinG

  5. User Tools enhancements (input/output) | SciTools Blog Says:

    [...] example of replacing an entire file would be when using astyle to format your code. In that blog posting we had to overwrite the current file, and then reload it. Now we can feed the [...]

  6. SciTools Blog » Blog Archive » Code Refactoring Says:

    [...] Tip – Beautify your code [...]

  7. GM Says:

    In my windows installation the folder to hold the astyle.upl script is C:\Program Files\SciTools\conf\plugin\User\IReport\ that differs a llitle bit from the one shown above.
    Regards,
    GM

  8. KevinG Says:

    Good point, the plugin folders have changed recently. I’ll update the blog entry to reflect that.
    KevinG

  9. KevinG Says:

    Updated the IReport plugin to work on architectures, or the entire project. Also added an extra field for specifying options not listed in the plugin, such as the padding switches.