clang-format: [Java] Support annotations with parameters.
authorDaniel Jasper <djasper@google.com>
Tue, 21 Oct 2014 09:25:39 +0000 (09:25 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 21 Oct 2014 09:25:39 +0000 (09:25 +0000)
Before:
  @SuppressWarnings
  (value = "unchecked") public void doSomething() { .. }

After:
  @SuppressWarnings(value = "unchecked")
  public void doSomething() { .. }

llvm-svn: 220279

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTestJava.cpp

index ce3d899900745e4a6905246a5f3bc47a1ecb4941..4f32b9f9eb9d632dfa259cb1fad4cc8422b43a48 100644 (file)
@@ -184,6 +184,8 @@ private:
 
         if (Left->Type == TT_AttributeParen)
           CurrentToken->Type = TT_AttributeParen;
+        if (Left->Previous && Left->Previous->Type == TT_JavaAnnotation)
+          CurrentToken->Type = TT_JavaAnnotation;
 
         if (!HasMultipleLines)
           Left->PackingKind = PPK_Inconclusive;
@@ -1791,7 +1793,8 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
         Left.Previous->is(tok::char_constant))
       return true;
   } else if (Style.Language == FormatStyle::LK_Java) {
-    if (Left.Type == TT_JavaAnnotation && Line.MightBeFunctionDecl)
+    if (Left.Type == TT_JavaAnnotation && Right.isNot(tok::l_paren) &&
+        Line.MightBeFunctionDecl)
       return true;
   }
 
index 18d945d0afa52c42cb33604f475b2e6a998758b8..cf48a7a11d123e1348d5c18b2416053c66cfc638 100644 (file)
@@ -70,6 +70,10 @@ TEST_F(FormatTestJava, Annotations) {
   verifyFormat("@Override\n"
                "@Nullable\n"
                "public String getNameIfPresent() {\n}");
+
+  verifyFormat("@SuppressWarnings(value = \"unchecked\")\n"
+               "public void doSomething() {\n}");
+
   verifyFormat("@Partial @Mock DataLoader loader;");
 }