r220984 - clang-format: [Java] Improve line breaks around annotations.

Daniel Jasper djasper at google.com
Fri Oct 31 11:23:49 PDT 2014


Author: djasper
Date: Fri Oct 31 13:23:49 2014
New Revision: 220984

URL: http://llvm.org/viewvc/llvm-project?rev=220984&view=rev
Log:
clang-format: [Java] Improve line breaks around annotations.

Before:
  @SomeAnnotation("With some really looooooooooooooong text") private static final
      long something = 0L;

  void SomeFunction(@Nullable
                    String something) {}

After:
  @SomeAnnotation("With some really looooooooooooooong text")
  private static final long something = 0L;

  void SomeFunction(@Nullable String something) {}

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/lib/Format/FormatToken.h
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestJava.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=220984&r1=220983&r2=220984&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Fri Oct 31 13:23:49 2014
@@ -464,6 +464,7 @@ unsigned ContinuationIndenter::addTokenO
       PreviousNonComment->Type != TT_TemplateCloser &&
       PreviousNonComment->Type != TT_BinaryOperator &&
       PreviousNonComment->Type != TT_JavaAnnotation &&
+      PreviousNonComment->Type != TT_LeadingJavaAnnotation &&
       Current.Type != TT_BinaryOperator && !PreviousNonComment->opensScope())
     State.Stack.back().BreakBeforeParameter = true;
 
@@ -538,9 +539,11 @@ unsigned ContinuationIndenter::getNewLin
     return State.Stack.back().QuestionColumn;
   if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0)
     return State.Stack.back().VariablePos;
-  if ((PreviousNonComment && (PreviousNonComment->ClosesTemplateDeclaration ||
-                              PreviousNonComment->Type == TT_AttributeParen ||
-                              PreviousNonComment->Type == TT_JavaAnnotation)) ||
+  if ((PreviousNonComment &&
+       (PreviousNonComment->ClosesTemplateDeclaration ||
+        PreviousNonComment->Type == TT_AttributeParen ||
+        PreviousNonComment->Type == TT_JavaAnnotation ||
+        PreviousNonComment->Type == TT_LeadingJavaAnnotation)) ||
       (!Style.IndentWrappedFunctionNames &&
        (NextNonComment->is(tok::kw_operator) ||
         NextNonComment->Type == TT_FunctionDeclarationName)))

Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=220984&r1=220983&r2=220984&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Fri Oct 31 13:23:49 2014
@@ -48,6 +48,7 @@ enum TokenType {
   TT_InlineASMColon,
   TT_JavaAnnotation,
   TT_LambdaLSquare,
+  TT_LeadingJavaAnnotation,
   TT_LineComment,
   TT_ObjCBlockLBrace,
   TT_ObjCBlockLParen,

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=220984&r1=220983&r2=220984&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Oct 31 13:23:49 2014
@@ -190,6 +190,8 @@ private:
           CurrentToken->Type = TT_AttributeParen;
         if (Left->Previous && Left->Previous->Type == TT_JavaAnnotation)
           CurrentToken->Type = TT_JavaAnnotation;
+        if (Left->Previous && Left->Previous->Type == TT_LeadingJavaAnnotation)
+          CurrentToken->Type = TT_LeadingJavaAnnotation;
 
         if (!HasMultipleLines)
           Left->PackingKind = PPK_Inconclusive;
@@ -835,7 +837,12 @@ private:
         Current.Type = TT_TrailingAnnotation;
       } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
                  Current.Previous->is(tok::at)) {
-        Current.Type = TT_JavaAnnotation;
+        const FormatToken& AtToken = *Current.Previous;
+        if (!AtToken.Previous ||
+            AtToken.Previous->Type == TT_LeadingJavaAnnotation)
+          Current.Type = TT_LeadingJavaAnnotation;
+        else
+          Current.Type = TT_JavaAnnotation;
       }
     }
   }
@@ -1460,6 +1467,9 @@ unsigned TokenAnnotator::splitPenalty(co
     return 150;
   }
 
+  if (Left.Type == TT_LeadingJavaAnnotation)
+    return 1;
+
   if (Right.Type == TT_TrailingAnnotation &&
       (!Right.Next || Right.Next->isNot(tok::l_paren))) {
     // Moving trailing annotations to the next line is fine for ObjC method
@@ -1811,7 +1821,7 @@ bool TokenAnnotator::mustBreakBefore(con
         Left.Previous->is(tok::char_constant))
       return true;
   } else if (Style.Language == FormatStyle::LK_Java) {
-    if (Left.Type == TT_JavaAnnotation && Right.isNot(tok::l_paren) &&
+    if (Left.Type == TT_LeadingJavaAnnotation && Right.isNot(tok::l_paren) &&
         Line.Last->is(tok::l_brace))
       return true;
     if (Right.is(tok::plus) && Left.is(tok::string_literal) && Right.Next &&
@@ -1835,7 +1845,7 @@ bool TokenAnnotator::canBreakBefore(cons
     return false;
   if (Left.Tok.getObjCKeywordID() == tok::objc_interface)
     return false;
-  if (Left.Type == TT_JavaAnnotation)
+  if (Left.Type == TT_JavaAnnotation || Left.Type == TT_LeadingJavaAnnotation)
     return true;
   if (Right.Type == TT_StartOfName ||
       Right.Type == TT_FunctionDeclarationName || Right.is(tok::kw_operator))

Modified: cfe/trunk/unittests/Format/FormatTestJava.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJava.cpp?rev=220984&r1=220983&r2=220984&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJava.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJava.cpp Fri Oct 31 13:23:49 2014
@@ -84,9 +84,15 @@ TEST_F(FormatTestJava, Annotations) {
                "  }\n"
                "});");
 
+  verifyFormat("void SomeFunction(@Nullable String something) {\n"
+               "}");
+
   verifyFormat("@Partial @Mock DataLoader loader;");
   verifyFormat("@SuppressWarnings(value = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")\n"
                "public static int iiiiiiiiiiiiiiiiiiiiiiii;");
+
+  verifyFormat("@SomeAnnotation(\"With some really looooooooooooooong text\")\n"
+               "private static final long something = 0L;");
 }
 
 TEST_F(FormatTestJava, Generics) {





More information about the cfe-commits mailing list