]> granicus.if.org Git - clang/commitdiff
Formatter/ObjC: In dictionary literals, break after ':', not before it.
authorNico Weber <nicolasweber@gmx.de>
Sun, 26 May 2013 05:39:26 +0000 (05:39 +0000)
committerNico Weber <nicolasweber@gmx.de>
Sun, 26 May 2013 05:39:26 +0000 (05:39 +0000)
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

lib/Format/TokenAnnotator.cpp
lib/Format/TokenAnnotator.h
unittests/Format/FormatTest.cpp

index 1fc9f9cbc1d899cbb67772b82afe194fd362751d..f2c4da39dc79b10050e3bd2901a998a729ab4e4d 100644 (file)
@@ -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;
index c72730e30c374bae7eda8e24d838d08f6e137593..e0d6d047e1225912f83393e7cdd5f0761121d224 100644 (file)
@@ -40,6 +40,7 @@ enum TokenType {
   TT_ObjCArrayLiteral,
   TT_ObjCBlockLParen,
   TT_ObjCDecl,
+  TT_ObjCDictLiteral,
   TT_ObjCForIn,
   TT_ObjCMethodExpr,
   TT_ObjCMethodSpecifier,
index add746611693d7fae8ac64c4ef04bccdc1b1fdbc..8d56d7236efe901df87163396fa75e791bf3a1ec 100644 (file)
@@ -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) {