Formatter/ObjC: In dictionary literals, break after ':', not before it.
Before:
@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
: regularFont, };
Now:
@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :
regularFont, };
':'s in dictionary literals (and the corresponding {}s) are now marked as
TT_ObjCDictLiteral too, which makes further improvements to dict literal
layout possible.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182716 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 1fc9f9c..f2c4da3 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -254,8 +254,18 @@
if (CurrentToken != NULL) {
ScopedContextCreator ContextCreator(*this, tok::l_brace, 1);
AnnotatedToken *Left = CurrentToken->Parent;
+
+ AnnotatedToken *Parent = Left->getPreviousNoneComment();
+ bool StartsObjCDictLiteral = Parent && Parent->is(tok::at);
+ if (StartsObjCDictLiteral) {
+ Contexts.back().ColonIsObjCDictLiteral = true;
+ Left->Type = TT_ObjCDictLiteral;
+ }
+
while (CurrentToken != NULL) {
if (CurrentToken->is(tok::r_brace)) {
+ if (StartsObjCDictLiteral)
+ CurrentToken->Type = TT_ObjCDictLiteral;
Left->MatchingParen = CurrentToken;
CurrentToken->MatchingParen = Left;
next();
@@ -321,6 +331,8 @@
// Colons from ?: are handled in parseConditional().
if (Tok->Parent->is(tok::r_paren) && Contexts.size() == 1) {
Tok->Type = TT_CtorInitializerColon;
+ } else if (Contexts.back().ColonIsObjCDictLiteral) {
+ Tok->Type = TT_ObjCDictLiteral;
} else if (Contexts.back().ColonIsObjCMethodExpr ||
Line.First.Type == TT_ObjCMethodSpecifier) {
Tok->Type = TT_ObjCMethodExpr;
@@ -547,14 +559,15 @@
bool IsExpression)
: ContextKind(ContextKind), BindingStrength(BindingStrength),
LongestObjCSelectorName(0), ColonIsForRangeExpr(false),
- ColonIsObjCMethodExpr(false), FirstObjCSelectorName(NULL),
- FirstStartOfName(NULL), IsExpression(IsExpression),
- CanBeExpression(true) {}
+ ColonIsObjCDictLiteral(false), ColonIsObjCMethodExpr(false),
+ FirstObjCSelectorName(NULL), FirstStartOfName(NULL),
+ IsExpression(IsExpression), CanBeExpression(true) {}
tok::TokenKind ContextKind;
unsigned BindingStrength;
unsigned LongestObjCSelectorName;
bool ColonIsForRangeExpr;
+ bool ColonIsObjCDictLiteral;
bool ColonIsObjCMethodExpr;
AnnotatedToken *FirstObjCSelectorName;
AnnotatedToken *FirstStartOfName;
@@ -1158,9 +1171,11 @@
const AnnotatedToken &Left = *Right.Parent;
if (Right.Type == TT_StartOfName)
return true;
- if (Right.is(tok::colon) && Right.Type == TT_ObjCMethodExpr)
+ if (Right.is(tok::colon) &&
+ (Right.Type == TT_ObjCDictLiteral || Right.Type == TT_ObjCMethodExpr))
return false;
- if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr)
+ if (Left.is(tok::colon) &&
+ (Left.Type == TT_ObjCDictLiteral || Left.Type == TT_ObjCMethodExpr))
return true;
if (Right.Type == TT_ObjCSelectorName)
return true;
diff --git a/lib/Format/TokenAnnotator.h b/lib/Format/TokenAnnotator.h
index c72730e..e0d6d04 100644
--- a/lib/Format/TokenAnnotator.h
+++ b/lib/Format/TokenAnnotator.h
@@ -40,6 +40,7 @@
TT_ObjCArrayLiteral,
TT_ObjCBlockLParen,
TT_ObjCDecl,
+ TT_ObjCDictLiteral,
TT_ObjCForIn,
TT_ObjCMethodExpr,
TT_ObjCMethodSpecifier,
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index add7466..8d56d72 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -4098,6 +4098,10 @@
verifyFormat(
"NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
" @\"processInfo\" : [NSProcessInfo processInfo] };");
+ verifyFormat(
+ "@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
+ " regularFont, };");
+
}
TEST_F(FormatTest, ReformatRegionAdjustsIndent) {