11

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.]

Olof_t
  • 766
  • 9
  • 22
  • I have problems with formatting code blocks within parameters as well. I have not been able to find settings to modify the indentation of code and parentheses after a ^ :( – Yogurt May 17 '12 at 21:46
  • 2
    No, there simply isn't any settings for that. However, when using small blocks xCode understands that if you first type `^(void)` and then enter, the following brackets will be correctly placed. – Olof_t Jun 24 '12 at 17:20
  • 3
    Try building the latest version of uncrustify from GitHub. It has much improved blocks support over the year old released version 0.59. – nschum Sep 28 '12 at 12:54
  • @nschum Yes, uncrustify is much better now than the old release. However, it still does not solve this problem. – Olof_t Oct 12 '12 at 14:59
  • 1
    It's my belief that you can get the latest uncrustify to do this correctly, but it's also my experience that Xcode will screw it back up the next time you are working in that code. – ipmcc Feb 05 '13 at 23:43

2 Answers2

3

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!");
                }];
        }];
});
Olof_t
  • 766
  • 9
  • 22
-1

XCode uses built in "Code Snippets" for autocompletion. Open the Code Snippet Library by clicking on the {} icon in the Library Pane. You cannot edit snippets directly within XCode itself, but the app SnippetEdit allows you to edit them.

I suggest you make a backup of the 'codesnippets' file before editing them. It's located in /Applications/Xcode.app/Contents/PlugIns/IDECodeSnippetLibrary.ideplugin/Contents/Resources/SystemCodeSnippets.codesnippets

jacob
  • 3,507
  • 2
  • 21
  • 26