Changes to clang-format's Objective-C block formatting over the past year have made clang-format's output deviate from what is expected (in my opinion).
There are three changes in particular:
- Method calls with multiple block parameters has each block indented further in
- Nested blocks get indented one level in
- Method calls that end with a block parameter always get forced to a new line and indented
The end of this message has examples of formatting with and without these changes.
This patch undoes one revision which causes methods with multiple block parameters to indent for each parameter (r236598) and adds two new options.
AllowNewlineBeforeBlockParameter makes the change in r235580 optional.
IndentNestedBlocks makes the change in r234304 optional.
Some relevant Bugzilla issues:
https://llvm.org/bugs/show_bug.cgi?id=23585
https://llvm.org/bugs/show_bug.cgi?id=23317
This patch came out of a discussion here https://github.com/square/spacecommander/issues/33, where we're using a custom version of clang-format with these options added. Based on the Bugzilla issues, it seems that we're not alone.
Thanks!
Current trunk:
bin/clang-format -style="{BasedOnStyle: WebKit, ColumnLimit: 0, IndentWidth: 4}" ~/clang-format.m
- (void)test { [self block:^(void) { doStuff(); } completionHandler:^(void) { doStuff(); [self block:^(void) { doStuff(); } completionHandler:^(void) { doStuff(); }]; }]; [[SessionService sharedService] loadWindow:aWindow completionBlock:^(SessionWindow* window) { if (window) { [self doStuff]; } }]; }
With this patch applied:
bin/clang-format -style="{BasedOnStyle: WebKit, ColumnLimit: 0, IndentWidth: 4, IndentNestedBlocks: false, AllowNewlineBeforeBlockParameter: false}"~/clang-format.m
- (void)test { [self block:^(void) { doStuff(); } completionHandler:^(void) { doStuff(); [self block:^(void) { doStuff(); } completionHandler:^(void) { doStuff(); }]; }]; [[SessionService sharedService] loadWindow:aWindow completionBlock:^(SessionWindow* window) { if (window) { [self doStuff]; } }]; }
Perhaps this would be better placed within the custom BraceWrapping options? Something simpler like BeforeBlockParameter.