Adding a code beautifier script to Xcode

Xcode’s re-indent command is pretty weak compared to the code reformatting built into other IDEs. It fixes the indentation at the beginning on each line, which makes a huge difference in code readability, but I don’t think it does anything else. In Eclipse and VS I’ve gotten used to being fairly lazy about being consistent with my spacing elsewhere and relying on their built in reformatting to clean it up for me. I wanted to change the way Xcode reformats code, since it’s easier to change tools than to break bad habits.

I spent some time searching for command line code beautifiers that support Objective C and only really found one. Uncrustify is open source, which is pretty fortunate since they don’t have an OS X binary download of the current version. The source download includes the needed project files for Xcode, so compiling your own is easy enough. Just open uncrustify.xcodeproj and compile. You could also download another project, Universal Indent GUI which includes an already compiled Uncrustify executable along with a GUI front end.

After I compiled the executable, I copied it and the defaults.cfg file to a new directory and created a couple user scripts in Xcode to use it. The code for both scripts is identical, just adjust it for the correct paths to the files.

#!/bin/sh
echo -n "%%%{PBXSelection}%%%"
/path/to/uncrustify -q -c /path/to/defaults.cfg
echo -n "%%%{PBXSelection}%%%"

The “Reformat all” script will run Uncrustify on the entire document.

The “Reformat selection” is the same except “Input” is set to “Selection” and “Output” is set to “Replace Selection”.

The defaults.cfg does pretty well with obj-c but isn’t perfect. At least it’s consistent. One of these days I’ll find a better config for it it try fixing it myself.

This entry was posted in Apple, Coding and tagged , , . Bookmark the permalink.

16 Responses to Adding a code beautifier script to Xcode

  1. Thomas says:

    If you are still trying to find the best configuration for the Uncrustify code formatting, you may use UniversalIndentGUI for that. It shows you how each of Uncrustifys parameters behaves on the code directly while changing a parameter. You can choose a code file of your choice for that and then save the Uncrustify config file to wherever you want.

    Have fun trying it.

    Thomas

  2. Scott says:

    Thomas-
    I have been playing with UniversalIndentGUI and it does make testing settings really easy.

    My biggest problem is the way it handles spaces around colons. The same config that adds or removes spaces to make 1 space before and after colon in this line:

    [UIView setAnimationDelegate : self];

    Ignores the spacing around the colons in this line:

    if (self = [super initWithNibName:nibNameOrNil bundle: nibBundleOrNil])

    I’ve tried playing with the sp_after_send_oc_colon and sp_before_send_oc_colon parameters but they don’t seem to do anything.

    Overall I’m happy enough with it as it is. The problems are minor and don’t prevent anything from compiling. It’s still 100x better than just using the built in re-indent.

  3. Mike says:

    I had some troubles with your script so I wrote a new one. Maybe this will helps someone out there:

    #!/usr/bin/perl -w

    use FileHandle;
    use File::Temp (“mkdtemp”);
    use File::Copy (“copy”);
    use File::Basename (“basename”);
    use Cwd (“chdir”);

    my $filename = basename(“%%%{PBXFilePath}%%%”);
    my $temporary_directory = mkdtemp(“/tmp/uncrustifyXXXX”);

    my $uncrustify = “/Applications/UniversalIndentGUI/indenters/uncrustify”;
    my $config = “/Applications/UniversalIndentGUI/myIndenters/p31Indenter.cfg”;

    # Write out the file to a temp directory
    open(OUTFILE, “> $temporary_directory/uncrustifyMe.m”) or die “Unable to create temporary file: $!”;
    print OUTFILE while();
    close(OUTFILE);

    chdir(“$temporary_directory”);

    # Write to temp first then read in file and print it
    my $output = `$uncrustify -c $config -o output.m -f uncrustifyMe.m`;

    # Read in file
    open FILE, “< output.m”;
    $file_contents = do { local $/; };

    # Clean up
    unlink “uncrustifyMe.m”;
    unlink “output.m”;
    rmdir “$temporary_directory”;

    print $file_contents;

  4. Yelp Clone says:

    Great Article! Keep up the good work!

  5. Pingback: Format Your Code With Crustify « www.invasivecode.com

  6. Pingback: Format you Code with Crustify! | Matching Brackets

  7. Pingback: Format Your Code With Crustify « iNVASIVECODE

  8. Pingback: Bynomial Code » Code formatting

  9. Uncrustify is a good tool (I’ve tried it on Debian Linux and on OS X Lion), but it’s not a Mac app. Many of Mac/iOS developers love to work with native tools.

    Try StrictCode, source code formatter exclusively for Objective-C developers. Uncompromising simplicity and true flexibility: http://strictcodeapp.com.

  10. Uncrustify is a good tool (I’ve tried it on Debian Linux and on OS X Lion), but it’s not a Mac app. Many of Mac/iOS developers love to work with native tools.

    Try StrictCode, source code formatter exclusively for Objective-C developers. Uncompromising simplicity and true flexibility: strictcodeapp.com.

  11. Uncrustify is a good tool (I’ve tried it on Debian Linux and on OS X Lion), but it’s not a Mac app. Many of Mac/iOS developers love to work with native tools.

    Try StrictCode, source code formatter exclusively for Objective-C developers. Uncompromising simplicity and true flexibility. Welcome: strictcodeapp dot com.

  12. Uncrustify is a good tool (I’ve tried it on Debian Linux and on OS X Lion), but it’s not a Mac app. Many of Mac/iOS developers love to work with native tools.

    Try StrictCode, source code formatter exclusively for Objective-C developers. Uncompromising simplicity and true flexibility.

  13. Sorry for duplication, something wrong with comments…

  14. Pingback: Solution: Hidden Features of Xcode #dev #it #computers | Technical information for you

  15. Pingback: Fixed Hidden Features of Xcode #dev #it #asnwer | Good Answer

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>