]> granicus.if.org Git - clang/commitdiff
clang-format: [Java] Wrap after each function annotation.
authorDaniel Jasper <djasper@google.com>
Tue, 21 Oct 2014 08:24:18 +0000 (08:24 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 21 Oct 2014 08:24:18 +0000 (08:24 +0000)
Before:
  @Override public String toString() { .. }

After:
  @Override
  public String toString() { .. }

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@220274 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/ContinuationIndenter.cpp
lib/Format/FormatToken.h
lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTestJava.cpp

index ead1da99c2ddba23c5ca4788378a1827d599046e..4c93c7f548ceeba99a4561281169f4ff97129011 100644 (file)
@@ -458,6 +458,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
       !PreviousNonComment->isOneOf(tok::comma, tok::semi) &&
       PreviousNonComment->Type != TT_TemplateCloser &&
       PreviousNonComment->Type != TT_BinaryOperator &&
+      PreviousNonComment->Type != TT_JavaAnnotation &&
       Current.Type != TT_BinaryOperator && !PreviousNonComment->opensScope())
     State.Stack.back().BreakBeforeParameter = true;
 
@@ -533,7 +534,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
   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_AttributeParen ||
+                              PreviousNonComment->Type == TT_JavaAnnotation)) ||
       (!Style.IndentWrappedFunctionNames &&
        (NextNonComment->is(tok::kw_operator) ||
         NextNonComment->Type == TT_FunctionDeclarationName)))
index dc2c8a466efe77ab588a6bb4cdfb6d6b6f4c23f0..425a7c836483f3d54bf466cc4f0ece16725ce9c7 100644 (file)
@@ -46,6 +46,7 @@ enum TokenType {
   TT_ImplicitStringLiteral,
   TT_InheritanceColon,
   TT_InlineASMColon,
+  TT_JavaAnnotation,
   TT_LambdaLSquare,
   TT_LineComment,
   TT_ObjCBlockLBrace,
index 7fbd2f29d5fe9c3c3e05794275ed946564ec58ba..ce3d899900745e4a6905246a5f3bc47a1ecb4941 100644 (file)
@@ -826,6 +826,9 @@ private:
         // Line.MightBeFunctionDecl can only be true after the parentheses of a
         // function declaration have been found.
         Current.Type = TT_TrailingAnnotation;
+      } else if (Style.Language == FormatStyle::LK_Java && Current.Previous &&
+                 Current.Previous->is(tok::at)) {
+        Current.Type = TT_JavaAnnotation;
       }
     }
   }
@@ -1787,6 +1790,9 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
     if (Right.is(tok::char_constant) && Left.is(tok::plus) && Left.Previous &&
         Left.Previous->is(tok::char_constant))
       return true;
+  } else if (Style.Language == FormatStyle::LK_Java) {
+    if (Left.Type == TT_JavaAnnotation && Line.MightBeFunctionDecl)
+      return true;
   }
 
   return false;
index 2bca3bbe8926181f6ec9a32f03d7ef5ff8ad4c3e..18d945d0afa52c42cb33604f475b2e6a998758b8 100644 (file)
@@ -64,5 +64,14 @@ TEST_F(FormatTestJava, ThrowsDeclarations) {
                "    throws LooooooooooooooooooooooooooooongException {}");
 }
 
+TEST_F(FormatTestJava, Annotations) {
+  verifyFormat("@Override\n"
+               "public String toString() {\n}");
+  verifyFormat("@Override\n"
+               "@Nullable\n"
+               "public String getNameIfPresent() {\n}");
+  verifyFormat("@Partial @Mock DataLoader loader;");
+}
+
 } // end namespace tooling
 } // end namespace clang