A common problem with large iOS projects is the lack of support for a shared set of formatting rules. This leads over the time to unreadable and hard to maintain sources files.
Xcode Formatter uses uncrustify to easily format your source code as your team exactly wants it to be!
Simply add the CodeFormatter directory in your Xcode project to get :
- Xcode shortcut-based code formatting: a shortcut to format modified sources in the current workspace
- automatic code formatting: add a build phase to your project to format current sources when application builds
- all sources formatting: format all your code with one command line
- your formatting rules shared by project: edit and use a same configuration file with your project dev team
- How to setup the code formatter for your project
The simplest way is to use brew:
$ brew install uncrustify
To install brew:
$ ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
$ which uncrustify
If your uncrustify version is lower than 0.60, you might have to install it manually since modern Objective-C syntax has been added recently, see this post : http://stackoverflow.com/questions/11862964/uncrustify-doesnt-support-for-the-new-objective-c-syntax-of-llvm-4-0/14202765#14202765
You can see this name in the Applications/ directory (or your custom Xcode installation directory). Be carefull if you have multiple instances of Xcode on your mac: ensure that project's one is actually named "Xcode"!
(Why this ? This name is used to find currently opened Xcode files. See CodeFormatter/Uncrustify_opened_Xcode_sources.workflow appleScript).
Copy this file to your ~/Library/Services/ folder (create this folder if needed).
Be careful : by double-clicking the .workflow file, you will install it but the file will be removed! Be sure to leave a copy of it for other users.
- How to format opened files when building the project
sh CodeFormatter/scripts/formatOpenedSources.sh
- How to format opened files with a shortcut
Go to Xcode > Services > Services preferences :
And set your shortcut :
- How to format files in command line
$ sh CodeFormatter/scripts/formatOpenedSources.sh
$ sh CodeFormatter/scripts/formatAllSources.sh PATH
PATH must be replaced by your sources path.
- How to change formatter’s rules
You can use UniversalIndentGUI (http://universalindent.sourceforge.net/) to simplify edition.
- FAQ
Uncrustify (version 0.59) does not handle apple new literals for creating NSDictionary and NSArray like:
NSDictionary *dictionary = @{@0 : @"red",
@1 : @"green",
@2 : @"blue"};
Until a new version of uncrustify fixes this issue, you can use the old way with those literals:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"red", @1,
@"green", @2,
@"blue", @3,
nil];
Yes, everything is possible. Just move your configuration file to your home directory: '~/.uncrustify//uncrustify_objective_c.cfg' and remove 'CodeFormatter' directory from your project. If the configuration file is not found in project directory, this global file is used.