Is there a way to make Xcode not reformat formatted code, or is there a tool like uncrustify that can format source code that uses blocks?
With blocks in objective-c, code has become hard to read. One solution is to write out the block definition and put curly braces on new lines, like this:
dispatch_async(dispatch_get_global_queue(0, 0), ^(void)
{
//block of code
});
And:
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop)
{
//block of code
}];
Beautiful and easy!
But Xcode does not preserve this format, and I can't find a way to make uncrustify output code this way. Uncrustify is really good, but perhaps blocks are too new?
Any ideas?
[disclaimer: I know this may turn into "troll hour", but code should be as easy to read as possible, and having braces on the same column makes things much more clear (to me). Especially if you have several blocks within a block. So if you don't like code looking like this, please try to just ignore the question.]
The latest (about 2 months old or so) update to uncrustify almost solves the problem. Just set the following items in your config file:
indent_oc_block = true
indent_with_tabs = 0
indent_columns = 4 # set to the same as indent_switch_case
indent_switch_case = 4 # set to the same as indent_columns
(I used indent_with_tabs = 0 because I could't get it to work with tabs. Probably not necessary.)
And of course, for new line after/before {} set all you want of the nl_some_parameter_brace to "force".
Now uncrustify will handle your code, it will not insert new lines into blocks for you, code like this will remain ugly:
dispatch_async(dispatch_get_global_queue(0, 0), ^(void) {
//code
}
If someone finds a way to make it insert new lines appropriately, please tell me.
Thanks @ipmcc for the update on uncrustify.
Edit: Yes, xCode obfuscates the code whenever you copy/paste. I use this great xCode plugin to ease the workflow: https://github.com/benoitsan/BBUncrustifyPlugin-Xcode
Edit 2: Uncrustify does not handle nested blocks very well (still better than Xcode). Eg, nested blocks becomes:
dispatch_async(dispatch_get_global_queue(0, 0), ^(void)
{
[array enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop)
{
[array enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop)
{
NSLog(@"the pumpkin pie!");
}];
}];
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With