This is an archive of the discontinued LLVM Phabricator instance.

Add a new clang-format style option ObjCBlockResetsIndent.
Needs ReviewPublic

Authored by yixiang on Dec 3 2016, 12:39 AM.

Details

Reviewers
djasper
Summary

Add a new clang-format style option ObjCBlockResetsIndent of type bool. It defaults to false.
When true, ObjC blocks resets indentation to that of its owner block.
The resetting behavior is often desirable, as it leaves more columns for the content of the ObjC block.

E.g. for an unformatted file test.m like this:

- (void)test {
[self callAVeryVeryVeryVeryVeryVeryLongAsyncMethodWith:@"Some param"
completionHandler:^() {
[self callAnotherLongMethodHereWith:@"param"];
}];
}

- (void)test2 {
[self callAsychronousMethodWith:@"Some param"
failureHandler:^() {
[self handleFailure];
}
successHandler:^() {
[self handleSuccess];
}];
}

The formatted result with ObjCBlockResetsIndent=false (or omitted) is this:

// clang-format -style='{ObjCBlockResetsIndent: false}' test.m
// OR
// clang-format test.m
- (void)test {
  [self callAVeryVeryVeryVeryVeryVeryLongAsyncMethodWith:@"Some param"
                                       completionHandler:^() {
                                         [self callAnotherLongMethodHereWith:@"param"];
                                       }];
}

- (void)test2 {
  [self callAsychronousMethodWith:@"Some param"
      failureHandler:^() {
        [self handleFailure];
      }
      successHandler:^() {
        [self handleSuccess];
      }];
}

The formatted result with ObjCBlockResetsIndent=true is this:

// clang-format -style='{ObjCBlockResetsIndent: true}' test.m
- (void)test {
  [self callAVeryVeryVeryVeryVeryVeryLongAsyncMethodWith:@"Some param"
                                       completionHandler:^() {
    [self callAnotherLongMethodHereWith:@"param"];
  }];
}

- (void)test2 {
  [self callAsychronousMethodWith:@"Some param"
      failureHandler:^() {
        [self handleFailure];
      }
      successHandler:^() {
        [self handleSuccess];
      }];
}

Event Timeline

yixiang updated this revision to Diff 80175.Dec 3 2016, 12:39 AM
yixiang retitled this revision from to Add a new clang-format style option ObjCBlockResetsIndent..
yixiang updated this object.
yixiang added a reviewer: djasper.
yixiang added subscribers: cfe-commits, klimek.
djasper edited edge metadata.Dec 3 2016, 12:48 AM

I think generally, this makes sense.

Can you add tests in unittests/Format/FormatTest.cpp (probably next to TEST_F(FormatTest, FormatsBlocks) {..})?
What's the behavior when there is more than one block?

Also note that we have some requirements for additional options (mainly to ensure long-term maintainability):
http://clang.llvm.org/docs/ClangFormatStyleOptions.html#adding-additional-style-options

yixiang updated this object.Dec 3 2016, 1:01 AM
yixiang edited edge metadata.
yixiang updated this revision to Diff 80176.Dec 3 2016, 2:43 AM
  • Added unit test for ObjCBlockResetsIndent.

Can you add tests in unittests/Format/FormatTest.cpp (probably next to TEST_F(FormatTest, FormatsBlocks) {..})?
What's the behavior when there is more than one block?

Unit tests added.

Also note that we have some requirements for additional options (mainly to ensure long-term maintainability):
http://clang.llvm.org/docs/ClangFormatStyleOptions.html#adding-additional-style-options

Thanks for pointing that out to me.

yixiang updated this object.Dec 3 2016, 3:02 PM
yixiang updated this revision to Diff 80192.Dec 3 2016, 3:03 PM
  • Don't reset indent for statements with multiple blocks.
yixiang added a comment.EditedDec 5 2016, 11:59 AM

Discover a case where my code breaks. When the block signature has a return type in it, the indent doesn't gets reset properly. I need to fix this.

[UPDATE]

The testing code was with incorrect syntax. The code is actually ready for review.

Ping.

For the additional requirements of adding a style options:

  • be used in a project of significant size (have dozens of contributors)
    • Yes, it's been used by almost all the iOS projects inside Google, including Google Maps.
  • have a publicly accessible style guide
  • have a person willing to contribute and maintain patches
    • I'm willing to do the maintenance, I'm confident that I can find backup contributors too.