yixiang created this revision. yixiang added a reviewer: djasper. yixiang added subscribers: cfe-commits, klimek.
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"]; }]; } The formatted result with ObjCBlockResetsIndent=false (or omitted) is this: // clang-format -style='{BasedOnStyle: Google, ObjCBlockResetsIndent: false}' test.m // OR // clang-format -style='{BasedOnStyle: Google}' test.m - (void)test { [self callAVeryVeryVeryVeryVeryVeryLongAsyncMethodWith:@"Some param" completionHandler:^() { [self callAnotherLongMethodHereWith: @"param"]; }]; } The formatted result with ObjCBlockResetsIndent=true is this: // clang-format -style='{BasedOnStyle: Google, ObjCBlockResetsIndent: true}' test.m - (void)test { [self callAVeryVeryVeryVeryVeryVeryLongAsyncMethodWith:@"Some param" completionHandler:^() { [self callAnotherLongMethodHereWith:@"param"]; }]; } https://reviews.llvm.org/D27383 Files: docs/ClangFormatStyleOptions.rst include/clang/Format/Format.h lib/Format/ContinuationIndenter.cpp lib/Format/Format.cpp Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -322,6 +322,7 @@ IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth); + IO.mapOptional("ObjCBlockResetsIndent", Style.ObjCBlockResetsIndent); IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); IO.mapOptional("ObjCSpaceBeforeProtocolList", Style.ObjCSpaceBeforeProtocolList); Index: lib/Format/ContinuationIndenter.cpp =================================================================== --- lib/Format/ContinuationIndenter.cpp +++ lib/Format/ContinuationIndenter.cpp @@ -1037,6 +1037,13 @@ } void ContinuationIndenter::moveStateToNewBlock(LineState &State) { + // For ObjC blocks, reset the indent to that of the owner block if the style + // tells us to do so. + if (Style.ObjCBlockResetsIndent && State.NextToken->is(TT_ObjCBlockLBrace)) { + State.Stack.back().Indent = State.Stack.front().Indent; + State.Stack.back().NestedBlockIndent = + State.Stack.front().NestedBlockIndent; + } unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent; // ObjC block sometimes follow special indentation rules. unsigned NewIndent = Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -500,6 +500,9 @@ /// \brief The number of characters to use for indentation of ObjC blocks. unsigned ObjCBlockIndentWidth; + /// \brief Whether ObjC blocks resets the indent to that of its owner block. + bool ObjCBlockResetsIndent = false; + /// \brief Add a space after ``@property`` in Objective-C, i.e. use /// ``@property (readonly)`` instead of ``@property(readonly)``. bool ObjCSpaceAfterProperty; Index: docs/ClangFormatStyleOptions.rst =================================================================== --- docs/ClangFormatStyleOptions.rst +++ docs/ClangFormatStyleOptions.rst @@ -618,6 +618,9 @@ **ObjCBlockIndentWidth** (``unsigned``) The number of characters to use for indentation of ObjC blocks. +**ObjCBlockResetsIndent** (``bool``) + Whether ObjC blocks resets the indent to that of its owner block. + **ObjCSpaceAfterProperty** (``bool``) Add a space after ``@property`` in Objective-C, i.e. use ``@property (readonly)`` instead of ``@property(readonly)``.
Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -322,6 +322,7 @@ IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth); + IO.mapOptional("ObjCBlockResetsIndent", Style.ObjCBlockResetsIndent); IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); IO.mapOptional("ObjCSpaceBeforeProtocolList", Style.ObjCSpaceBeforeProtocolList); Index: lib/Format/ContinuationIndenter.cpp =================================================================== --- lib/Format/ContinuationIndenter.cpp +++ lib/Format/ContinuationIndenter.cpp @@ -1037,6 +1037,13 @@ } void ContinuationIndenter::moveStateToNewBlock(LineState &State) { + // For ObjC blocks, reset the indent to that of the owner block if the style + // tells us to do so. + if (Style.ObjCBlockResetsIndent && State.NextToken->is(TT_ObjCBlockLBrace)) { + State.Stack.back().Indent = State.Stack.front().Indent; + State.Stack.back().NestedBlockIndent = + State.Stack.front().NestedBlockIndent; + } unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent; // ObjC block sometimes follow special indentation rules. unsigned NewIndent = Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -500,6 +500,9 @@ /// \brief The number of characters to use for indentation of ObjC blocks. unsigned ObjCBlockIndentWidth; + /// \brief Whether ObjC blocks resets the indent to that of its owner block. + bool ObjCBlockResetsIndent = false; + /// \brief Add a space after ``@property`` in Objective-C, i.e. use /// ``@property (readonly)`` instead of ``@property(readonly)``. bool ObjCSpaceAfterProperty; Index: docs/ClangFormatStyleOptions.rst =================================================================== --- docs/ClangFormatStyleOptions.rst +++ docs/ClangFormatStyleOptions.rst @@ -618,6 +618,9 @@ **ObjCBlockIndentWidth** (``unsigned``) The number of characters to use for indentation of ObjC blocks. +**ObjCBlockResetsIndent** (``bool``) + Whether ObjC blocks resets the indent to that of its owner block. + **ObjCSpaceAfterProperty** (``bool``) Add a space after ``@property`` in Objective-C, i.e. use ``@property (readonly)`` instead of ``@property(readonly)``.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits