Format Your Code With Crustify
The problem
Don’t you love your code? I mean, it is our product as developers. In the same way that a baker knows how to do and create delicious bread, we create source code…. beautiful source code. The code has to do its duty right and efficiently but it also has to be beautiful. There is nothing better that see your code after some time and feel that this is your code with the correct style.
There is another think to take into account. When we review a piece of code, if the code is not formatted in the way we expected we are adding lot of brain stress due to a non expecting format, avoiding we to focus properly on the most important, what the code does.
As you go coding you will make your way to the right format, because you will see tones of code on books, Apple examples, articles and more. But, there are tools that can help upfront.
Crustify
Yes, yes, I know you can format using XCode itself, but it is somehow limited. You can control some indexing and others, but if you want more, you can have a look to Crustify.
With a bit of initial effort, Crustify will give you full control on the code format with more than 400 settings to adjust. Here I will guide you throw the steps to set it up so you can tell by yourself.
Download and install it
So, lets focus. Go and download the source code from the Crustify site. There you will find our friend GIT, do not panic, use:
1 | git clone git://github.com/bengardner/uncrustify.git |
This will create a folder with all the code inside. The next step is to build it. Like the old days use:
1 2 | ./configure make |
On the “src” folder you will have the “crustify” executable. Move it to your “/usr/local/bin”, and you are done. You can test on the terminal somewhere with the command “crustify -h”.
Include in XCode
I can show how to run it on the command line, but I’m sure you want to go to the point and include it in XCode. Here we deal with Objective-C so first lets take the basic configuration that comes with the source code. Take the file “etc/objc.cfg” and save it where ever you think is right (your configurations folder maybe!). It contains a default configuration good enough to start with.
This configuration file though is loosely documented so it is mandatory you to read the 70 lines file “documentation/htdocs/configuration.txt” to understand some of its simple enough syntax.
If you want to explore further the effect of every setting in your code you can download the UniversalIndentGUI. There, select Crustify and you will be able to, graphically, play with the setting so you get the best of the tool.
Better if you go adjusting your configuration as you go, so let’s set up and easy way to run Crustify from XCode. I borrow the small script from Scott from HackerToys
1 2 3 4 | #!/bin/sh echo -n "%%%{PBXSelection}%%%" /path/to/uncrustify -q -c /path/to/defaults.cfg echo -n "%%%{PBXSelection}%%%" |
The only thing we need to do is add a new user script in XCode as shown on the picture (use you own path).

You may assign a shortcut if you use it very often. Now you are done. Just open any file and run the script using the script menu of XCode.
Enjoy your coding!
I need to say this again, the code is your product so… make it shine!!



