Pure Heart, Clean Code

A month or two ago while processing through a lot of old code, particularly a lot that I didn’t originally write (or at least I’m claiming I didn’t write), it dawned on me how pleasant a world it would be if I could quickly reformat the code – to make the code bend to my will… curly braces on their own line as God intended them… whitespaces where they belonged, and not where they aren’t, etc. So, the adventure began.

From many years with other languages and editors and IDEs, the concept that this would not only be missing from Xcode, but somewhat difficult to obtain was a bit foreign. Nonetheless, with a bit of Internet research, an external tool called Uncrustify, and some experimenting, I coerced Xcode to do my bidding. Oh, before I get started – the Edit->Format->Re-Indent option in Xcode is worthless, so we’ll just ignore it for this outing.

To note, I tinkered with multiple external formatters and the weak internal xcode formatter before I finally settled with uncrustify. Uncrustify has fairly good Objective-C support, can easily be integrated with xcode as a user script, and provides a centralized formatter for pretty much all languages that xcode natively supports.

First stop, acquire Uncrustify. I built mine from source, but if you want an easier route, it can easily be installed by using MacPorts.

Edit User Scripts in Xcode

Once you’ve verified that you have a functional version of uncrustify (uncrustify –version at the terminal command prompt should do the trick – I’m currently using 0.55), the next step is to integrate uncrustify’s usage into Xcode. We need to add a custom script, so open up Xcode, head to the Scripts menu (look for the funky little scroll-like icon) and choose Open Scripts Folder. We need to create a new script with the following:


#!/bin/sh
echo -n "%%%{PBXSelection}%%%"
/usr/local/bin/uncrustify -q -c ~/uncrustify.cfg -l oc+
echo -n "%%%{PBXSelection}%%%"

Input will be Entire Document. Directory will be Selection. Output will be Replace Document Contents. Errors will be Merge with Script Output. I set this script to have a key combination action of Shift-Command-P (P for Pretty).

The only thing left to do is to configure uncrustify to format our code in the specific dialect of style that we prefer. This task is actually quite a bit more difficult than expected. Uncrustify uses a very simple but daunting configuration file. My recommendation, take one of the supplied sample configs (ben2.cfg is very good), merge in the objc.cfg sample, and tweak as necessary. Once you’re done, place the configuration file where specified in your script (above, I used ~/uncrustify.cfg). For those interested, I’ve attached my uncrustify.cfg. I’m still tweaking it here and there, so it’s definitely a work in progress, but it does the job for the most part.

Now, any time my cursor is an editor, a simple Shift-Command-P will reformat my code quickly and to my liking. I find myself now often typing code very hastily without regards to style and formatting and following up with a Shift-Command-P to make it nice and readable.

Be Sociable, Share!

Tags: , ,

One Response to “Pure Heart, Clean Code”

  1. Pure Heart, Clean Code – Part 2 | Life Is Rich Says:

    [...] few months back, I gave a short tutorial on how to integrate a code formatter / beautifier (uncrustify) into Xcode 3. Unfortunately (or [...]

Leave a Reply