From: Nico Weber Date: Sun, 26 May 2013 05:39:26 +0000 (+0000) Subject: Formatter/ObjC: In dictionary literals, break after ':', not before it. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f2ff8126e9e9df368f31b1f968d8fc80f99809b3;p=clang 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 1fc9f9cbc1..f2c4da39dc 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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(const AnnotatedLine &Line, 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 c72730e30c..e0d6d047e1 100644 --- a/lib/Format/TokenAnnotator.h +++ b/lib/Format/TokenAnnotator.h @@ -40,6 +40,7 @@ enum TokenType { 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 add7466116..8d56d7236e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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) {