Sep 20, 2010

Xcode + AStyle

Anar here...

You know, I think there are many people, who find the auto-formatting of source code in Xcode is really terrible :( I mean, it is very weak (in terms of configuration) for such a descent development environment, which XCode actually is.

Since a very long time I use AStyle (Artistic Style) to format my code. It actually does remarkable job and it is an extremely tunable tool. You can adjust it almost for all, if not for all, indentations schemas...

I have already posted a long time ago a recipe of how to setup astyle for eclipse. Now, it is time to use it in XCode.

I use a user script menu to add my own script, which will than trigger the astyle command to format a source code for me.

Use the User Script menu to add a new script. You should add your script to a section "Text", the type of the script should - a shell script.
Next is the properties of the script:
Input: Entire Document
Directory: Selection
Output: Replace Document Contents
Errors: Display in Alert

And the content of the script:

#!/bin/bash

# I pass selected file instead of a selection, because astyle 1.24
# fails to parse struct with access modifiers. It just hangs instead.
# I also use a tmp file in order to avoid annoying Xcode message, that
# source file was changed outside of Xcode.

# Important: there must be always a new line at the end of the source file.
# Next time I provide a fix for that.


TMP=`mktemp -t xcode.XXXXXXXXXX` || exit 1

cat << 'EOFEOFEOF' > "$TMP"
%%%{PBXAllText}%%%EOFEOFEOF

astyle --options=<***PATH_TO_YOUR_ASTYLE_RC_FILE***> $TMP &>/dev/null

echo -n "%%%{PBXSelection}%%%"
cat $TMP
echo -n "%%%{PBXSelection}%%%"

rm $TMP

Restriction: Be advised, there must be always a new line at the end of the source file!

BTW, don't forget to change that <***PATH_TO_YOUR_ASTYLE_RC_FILE***> to a real path to your astyle resource file ;)

The script above is actually a second version of my script. The first one was much simpler:

#!/bin/sh

echo -n "%%%{PBXSelection}%%%"
/usr/bin/astyle --options=<***PATH_TO_YOUR_ASTYLE_RC_FILE***> <&0 echo -n "%%%{PBXSelection}%%%"
But this version didn't work for me with astyle 1.24. This is why in the second version of the script:
I pass selected file instead of a selection, because astyle 1.24 fails to parse struct with access modifiers. It just hangs instead. I also use a tmp file in order to avoid annoying Xcode message, that source file was changed outside of Xcode.
Finally, just assign some shortcuts (I prefer Options + Command + L) to you script and voilĂ ...

4 comments:

oakleyses said...

louboutin pas cher, nike free, polo ralph lauren outlet online, louis vuitton, polo outlet, nike air max, longchamp pas cher, jordan shoes, ray ban sunglasses, gucci handbags, tiffany and co, jordan pas cher, cheap oakley sunglasses, louis vuitton outlet, oakley sunglasses, christian louboutin uk, nike free run, sac longchamp pas cher, nike air max, oakley sunglasses, longchamp outlet, prada outlet, kate spade outlet, longchamp outlet, louis vuitton, oakley sunglasses wholesale, polo ralph lauren, longchamp outlet, christian louboutin outlet, chanel handbags, louis vuitton outlet, tiffany jewelry, michael kors pas cher, replica watches, replica watches, ray ban sunglasses, ugg boots, burberry pas cher, tory burch outlet, nike roshe, oakley sunglasses, louis vuitton outlet, ray ban sunglasses, prada handbags, ugg boots, nike outlet, christian louboutin shoes, uggs on sale, christian louboutin

oakleyses said...

guess pas cher, north face, lululemon canada, michael kors, michael kors outlet online, true religion outlet, oakley pas cher, nike free uk, michael kors outlet, abercrombie and fitch uk, hollister uk, true religion outlet, hollister pas cher, ray ban pas cher, polo lacoste, north face uk, michael kors outlet online, coach outlet store online, ray ban uk, mulberry uk, burberry handbags, michael kors outlet, hogan outlet, nike blazer pas cher, nike air max, nike air max uk, true religion outlet, uggs outlet, coach outlet, nike air force, nike air max uk, sac hermes, uggs outlet, michael kors outlet, michael kors, michael kors outlet online, nike roshe run uk, vans pas cher, true religion jeans, replica handbags, ralph lauren uk, sac vanessa bruno, burberry outlet, nike tn, coach purses, new balance, michael kors outlet online, kate spade, timberland pas cher

oakleyses said...

hermes belt, hollister, lancel, abercrombie and fitch, chi flat iron, instyler, north face outlet, louboutin, asics running shoes, nfl jerseys, ferragamo shoes, new balance shoes, mont blanc pens, vans outlet, celine handbags, hollister clothing, mcm handbags, mac cosmetics, beats by dre, giuseppe zanotti outlet, converse outlet, nike air max, longchamp uk, oakley, reebok outlet, bottega veneta, nike air max, timberland boots, insanity workout, nike trainers uk, gucci, lululemon, baseball bats, soccer shoes, soccer jerseys, north face outlet, valentino shoes, wedding dresses, hollister, nike roshe run, vans, ray ban, babyliss, jimmy choo outlet, nike huaraches, p90x workout, ghd hair, ralph lauren, herve leger, iphone cases

oakleyses said...

ugg,uggs,uggs canada, barbour uk, moncler outlet, coach outlet, ugg,ugg australia,ugg italia, moncler, thomas sabo, canada goose, moncler outlet, links of london, canada goose outlet, juicy couture outlet, moncler uk, pandora jewelry, montre pas cher, marc jacobs, replica watches, louis vuitton, moncler, canada goose jackets, swarovski, pandora charms, canada goose outlet, toms shoes, moncler, swarovski crystal, doudoune moncler, louis vuitton, moncler, pandora jewelry, ugg, barbour, louis vuitton, hollister, canada goose outlet, karen millen uk, ugg pas cher, supra shoes, canada goose, juicy couture outlet, louis vuitton, wedding dresses, pandora uk, canada goose, ugg uk, canada goose uk, louis vuitton