[clang] 70c9867 - [clang-format] Add option for not breaking line before ObjC params
via cfe-commits
cfe-commits at lists.llvm.org
Sat Feb 1 09:39:49 PST 2020
Author: mydeveloperday
Date: 2020-02-01T17:39:34Z
New Revision: 70c98671fa7b395a52829b91782393f4c2613562
URL: https://github.com/llvm/llvm-project/commit/70c98671fa7b395a52829b91782393f4c2613562
DIFF: https://github.com/llvm/llvm-project/commit/70c98671fa7b395a52829b91782393f4c2613562.diff
LOG: [clang-format] Add option for not breaking line before ObjC params
Summary:
>From `clang-format` version 3.7.0 and up, , there is no way to keep following format of ObjectiveC block:
```
- (void)_aMethod
{
[self.test1 t:self w:self callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {
u = c;
}]
}
```
Regardless of the change in `.clang-format` configuration file, all parameters will be lined up so that colons will be on the same column, like following:
```
- (void)_aMethod
{
[self.test1 t:self
w:self
callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {
u = c;
}]
}
```
Considering with ObjectiveC, the first code style is cleaner & more readable for some people, I've added a config option: `ObjCDontBreakBeforeNestedBlockParam` (boolean) so that if it is enable, the first code style will be favored.
Reviewed By: MyDeveloperDay
Patch By: ghvg1313
Tags: #clang, #clang-format
Differential Revision: https://reviews.llvm.org/D70926
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/ContinuationIndenter.cpp
clang/lib/Format/Format.cpp
clang/unittests/Format/FormatTestObjC.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index fd77e1e727a9..981542451b29 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -2017,6 +2017,29 @@ the configuration (without a prefix: ``Auto``).
[self onOperationDone];
}];
+**ObjCBreakBeforeNestedBlockParam** (``bool``)
+ Break parameters list into lines when there is nested block
+ parameters in a fuction call.
+
+ .. code-block:: c++
+
+ false:
+ - (void)_aMethod
+ {
+ [self.test1 t:self w:self callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {
+ u = c;
+ }]
+ }
+ true:
+ - (void)_aMethod
+ {
+ [self.test1 t:self
+ w:self
+ callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {
+ u = c;
+ }]
+ }
+
**ObjCSpaceAfterProperty** (``bool``)
Add a space after ``@property`` in Objective-C, i.e. use
``@property (readonly)`` instead of ``@property(readonly)``.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6219d6b072cb..eae0dfb12151 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -158,8 +158,11 @@ clang-format
It helps avoid having the closing bracket align with the switch statement's
closing bracket (when ``IndentCaseLabels`` is ``false``).
+- Option ``ObjCBreakBeforeNestedBlockParam`` has been added to optionally apply
+ linebreaks for function arguments declarations before nested blocks.
+
.. code-block:: c++
-
+
switch (fool) { vs. switch (fool) {
case 1: case 1: {
{ bar();
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 3891029a6f3a..82afaa721ed9 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1688,6 +1688,29 @@ struct FormatStyle {
/// ``@property (readonly)`` instead of ``@property(readonly)``.
bool ObjCSpaceAfterProperty;
+ /// Break parameters list into lines when there is nested block
+ /// parameters in a fuction call.
+ /// \code
+ /// false:
+ /// - (void)_aMethod
+ /// {
+ /// [self.test1 t:self w:self callback:^(typeof(self) self, NSNumber
+ /// *u, NSNumber *v) {
+ /// u = c;
+ /// }]
+ /// }
+ /// true:
+ /// - (void)_aMethod
+ /// {
+ /// [self.test1 t:self
+ /// w:self
+ /// callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {
+ /// u = c;
+ /// }]
+ /// }
+ /// \endcode
+ bool ObjCBreakBeforeNestedBlockParam;
+
/// Add a space in front of an Objective-C protocol list, i.e. use
/// ``Foo <Protocol>`` instead of ``Foo<Protocol>``.
bool ObjCSpaceBeforeProtocolList;
@@ -2178,6 +2201,8 @@ struct FormatStyle {
NamespaceMacros == R.NamespaceMacros &&
ObjCBinPackProtocolList == R.ObjCBinPackProtocolList &&
ObjCBlockIndentWidth == R.ObjCBlockIndentWidth &&
+ ObjCBreakBeforeNestedBlockParam ==
+ R.ObjCBreakBeforeNestedBlockParam &&
ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty &&
ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList &&
PenaltyBreakAssignment == R.PenaltyBreakAssignment &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 1ea7eb031d36..e3c9f7ce5f0c 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -861,8 +861,10 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
// Any break on this level means that the parent level has been broken
// and we need to avoid bin packing there.
bool NestedBlockSpecialCase =
- !Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
- State.Stack[State.Stack.size() - 2].NestedBlockInlined;
+ (!Style.isCpp() && Current.is(tok::r_brace) && State.Stack.size() > 1 &&
+ State.Stack[State.Stack.size() - 2].NestedBlockInlined) ||
+ (Style.Language == FormatStyle::LK_ObjC && Current.is(tok::r_brace) &&
+ State.Stack.size() > 1 && !Style.ObjCBreakBeforeNestedBlockParam);
if (!NestedBlockSpecialCase)
for (unsigned i = 0, e = State.Stack.size() - 1; i != e; ++i)
State.Stack[i].BreakBeforeParameter = true;
@@ -1380,7 +1382,8 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
(!BinPackInconclusiveFunctions &&
Current.PackingKind == PPK_Inconclusive)));
- if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen) {
+ if (Current.is(TT_ObjCMethodExpr) && Current.MatchingParen &&
+ Style.ObjCBreakBeforeNestedBlockParam) {
if (Style.ColumnLimit) {
// If this '[' opens an ObjC call, determine whether all parameters fit
// into one line and put one per line if they don't.
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 6f585ae915a5..83e95982aa44 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -506,6 +506,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("NamespaceMacros", Style.NamespaceMacros);
IO.mapOptional("ObjCBinPackProtocolList", Style.ObjCBinPackProtocolList);
IO.mapOptional("ObjCBlockIndentWidth", Style.ObjCBlockIndentWidth);
+ IO.mapOptional("ObjCBreakBeforeNestedBlockParam",
+ Style.ObjCBreakBeforeNestedBlockParam);
IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty);
IO.mapOptional("ObjCSpaceBeforeProtocolList",
Style.ObjCSpaceBeforeProtocolList);
@@ -807,6 +809,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None;
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
LLVMStyle.ObjCBlockIndentWidth = 2;
+ LLVMStyle.ObjCBreakBeforeNestedBlockParam = true;
LLVMStyle.ObjCSpaceAfterProperty = false;
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp
index 063eb5f58f1f..edbf092d0421 100644
--- a/clang/unittests/Format/FormatTestObjC.cpp
+++ b/clang/unittests/Format/FormatTestObjC.cpp
@@ -1398,6 +1398,37 @@ TEST_F(FormatTestObjC, DisambiguatesCallsFromStructuredBindings) {
"}");
}
+TEST_F(FormatTestObjC, BreakLineBeforeNestedBlockParam) {
+ Style = getGoogleStyle(FormatStyle::LK_ObjC);
+ Style.ObjCBreakBeforeNestedBlockParam = false;
+ Style.ColumnLimit = 0;
+
+ verifyFormat("[self.test1 t:self callback:^(typeof(self) self, NSNumber *u, "
+ "NSNumber *v) {\n"
+ " u = v;\n"
+ "}]");
+
+ verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
+ "NSNumber *u, NSNumber *v) {\n"
+ " u = v;\n"
+ "}]");
+
+ verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
+ "NSNumber *u, NSNumber *v) {\n"
+ " u = c;\n"
+ "} w:self callback2:^(typeof(self) self, NSNumber *a, NSNumber "
+ "*b, NSNumber *c) {\n"
+ " b = c;\n"
+ "}]");
+
+ Style.ColumnLimit = 80;
+ verifyFormat(
+ "[self.test_method a:self b:self\n"
+ " callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {\n"
+ " u = v;\n"
+ " }]");
+}
+
} // end namespace
} // end namespace format
} // end namespace clang
More information about the cfe-commits
mailing list