Code Formatting in Xcode

Jonah Williams ·

Using Xcode 4? Take a look at Code Formatting in Xcode 4


Working in Xcode I almost forgot how much I like IntelliJ’s code formatting tools. Luckily for me Mike Smith recently pointed out that UniversalIndentGUI can be used to setup your own code formatting in Xcode.

UniversalIndentGUI supports a large number of code formatting tools but Uncrustify seems to be the only one with objective-c support. An up to date build of Uncrustify (version 0.53) is included with UniversalIndentGUI so I did not need to build my own.

I setup a set of styling rules using UniversalIndentGUI’s live preview and saved the resulting config file. I was then able to add a set of custom user scripts in Xcode to run Uncrustify against the current file or selection. I found that I had to specify the source as objective-c to prevent Uncrustify form running some C/C++ styling rules which I did not want.

Uncrustify custom scripts in Xcode

Uncrustify custom scripts in Xcode

#!/bin/sh

~/Applications/UniversalIndentGUI/indenters/uncrustify -l OC -q -c ~/Applications/UniversalIndentGUI/config/uncrustify_obj_c.cfg

Now I can quickly clean up most of the minor formatting inconsistencies which creep into my code. Now I just need to polish my config file until it better matches my expectations for code formatting. In defining my preferred code style I have tried to follow the example provided by:

 

My current config file for Uncrustify is available here: uncrustify_obj_c.cfg if you would like to use it as a starting point.


My config file is now also available as a gist: https://gist.github.com/841452 for anyone who would like to suggest improvements or create their own fork.