How to format source code with braces on new lines with blocks and Xcode?
Asked Answered
P

2

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

Pish answered 20/4, 2012 at 9:16 Comment(5)
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 ^ :(Buprestid
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.Pish
Try building the latest version of uncrustify from GitHub. It has much improved blocks support over the year old released version 0.59.Lw
@Lw Yes, uncrustify is much better now than the old release. However, it still does not solve this problem.Pish
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.Unapproachable
P
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!");
                }];
        }];
});
Pish answered 17/3, 2013 at 10:21 Comment(0)
W
-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

Werby answered 13/2, 2013 at 19:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.