r182716 - Formatter/ObjC: In dictionary literals, break after ':', not before it.
Nico Weber
nicolasweber at gmx.de
Sat May 25 22:39:26 PDT 2013
Author: nico
Date: Sun May 26 00:39:26 2013
New Revision: 182716
URL: http://llvm.org/viewvc/llvm-project?rev=182716&view=rev
Log:
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.
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/TokenAnnotator.h
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=182716&r1=182715&r2=182716&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Sun May 26 00:39:26 2013
@@ -254,8 +254,18 @@ private:
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 @@ private:
// 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 @@ private:
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 @@ bool TokenAnnotator::canBreakBefore(cons
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;
Modified: cfe/trunk/lib/Format/TokenAnnotator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.h?rev=182716&r1=182715&r2=182716&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.h (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.h Sun May 26 00:39:26 2013
@@ -40,6 +40,7 @@ enum TokenType {
TT_ObjCArrayLiteral,
TT_ObjCBlockLParen,
TT_ObjCDecl,
+ TT_ObjCDictLiteral,
TT_ObjCForIn,
TT_ObjCMethodExpr,
TT_ObjCMethodSpecifier,
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=182716&r1=182715&r2=182716&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Sun May 26 00:39:26 2013
@@ -4098,6 +4098,10 @@ TEST_F(FormatTest, ObjCLiterals) {
verifyFormat(
"NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n"
" @\"processInfo\" : [NSProcessInfo processInfo] };");
+ verifyFormat(
+ "@{ NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
+ " regularFont, };");
+
}
TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
More information about the cfe-commits
mailing list