clang-format: [Java] Fix class declaration formatting.
authorDaniel Jasper <djasper@google.com>
Mon, 3 Nov 2014 02:27:28 +0000 (02:27 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 3 Nov 2014 02:27:28 +0000 (02:27 +0000)
Before:
  @SomeAnnotation()
  abstract
      class aaaaaaaaaaaa extends bbbbbbbbbbbbbbb implements cccccccccccc {
  }

After:
  @SomeAnnotation()
  abstract class aaaaaaaaaaaa extends bbbbbbbbbbbbbbb
      implements cccccccccccc {
  }

llvm-svn: 221121

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

index 7071532..f6fbbba 100644 (file)
@@ -428,7 +428,9 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
        !State.Stack.back().AvoidBinPacking) ||
       Previous.Type == TT_BinaryOperator)
     State.Stack.back().BreakBeforeParameter = false;
-  if (Previous.Type == TT_TemplateCloser && Current.NestingLevel == 0)
+  if ((Previous.Type == TT_TemplateCloser ||
+       Previous.Type == TT_JavaAnnotation) &&
+      Current.NestingLevel == 0)
     State.Stack.back().BreakBeforeParameter = false;
   if (NextNonComment->is(tok::question) ||
       (PreviousNonComment && PreviousNonComment->is(tok::question)))
index 5eb6e10..bebd6cc 100644 (file)
@@ -1474,6 +1474,11 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
       Left.Type == TT_InheritanceColon)
     return 2;
 
+  if (Left.Type == TT_LeadingJavaAnnotation)
+    return 1;
+  if (Style.Language == FormatStyle::LK_Java && Right.TokenText == "implements")
+    return 2;
+
   if (Right.isMemberAccess()) {
     if (Left.is(tok::r_paren) && Left.MatchingParen &&
         Left.MatchingParen->ParameterCount > 0)
@@ -1481,9 +1486,6 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
     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
index 96dda9b..60111b1 100644 (file)
@@ -77,6 +77,11 @@ TEST_F(FormatTestJava, ClassDeclarations) {
                "    implements SomeInterface,\n"
                "               AnotherInterface {}",
                getStyleWithColumns(40));
+  verifyFormat("@SomeAnnotation()\n"
+               "abstract class aaaaaaaaaaaa extends bbbbbbbbbbbbbbb\n"
+               "    implements cccccccccccc {\n"
+               "}",
+               getStyleWithColumns(76));
 }
 
 TEST_F(FormatTestJava, EnumDeclarations) {