Code Formatting in Xcode

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.

This entry was posted in Mobile and tagged , , . Bookmark the permalink.
  • http://curiouscreature.net/blog Jason Kratz

    Wow. Talk about a timely post :) I just found this after getting really fed up with not being able to control source formatting in Xcode when working on iPhone code for the first time. I’m so used to what is in Eclipse and NetBeans and being able to control everything. Giving up that in Xcode is more than annoying.

    Thanks!

  • Tobias Patton

    Would you care to publish you config file? Thanks.

    • http://www.carbonfive.com jonah

      Sure thing, I’ve added a link to it to the bottom of the post. Hope you find it useful.

  • Tobias Patton

    Thanks!

    If you pick up the latest source of uncrustify (0.54) and build it, there’s a new parameter:

    align_oc_msg_colon=true

    that will align objective-c multi-line messages so that the colons are aligned.

  • Derek Clarkson

    Hi, I’m just picking up Objective C and Xcode and was actually quite surprised at the lack of some formatting and refactoring tools I found. I’m coming from a Java/Eclipse world so I was quite used to extensive tools in these areas. Your posting has really helped with the formatting side of things.

    Thanks.
    Derek

  • http://theagile.biz Dmytro Bischak

    Thanks!
    It works well!
    I’m using your config :)

  • Marcus Smedman

    Excellent, thank you very much!

  • Marcelo Castro

    Felt the same way as Derek. For those who came from Eclipse/Netbeans… code formatting is a real drawback for Xcode.

  • http://newlyplanted.blogspot.com Jedidja

    Wow that definitely helps a lot. Really miss the Ctrl-K,D from Visual Studio for auto-formatting.

  • http://andywaite.com Andy Waite

    Is there any way to automate this so that uncrustify always runs when you save modifications to a file?

  • rh

    Just wanted to throw in my thanks – this makes the transition from VS a lot easier!

  • Pingback: Code Formatting in Xcode 4 | Carbon Five Community

  • Pingback: Formatting Objective C source files [closed] | PHP Developer Resource