r221981 - clang-format: [Java] Fix line break behavior of class declarations.

Daniel Jasper djasper at google.com
Fri Nov 14 02:15:56 PST 2014


Author: djasper
Date: Fri Nov 14 04:15:56 2014
New Revision: 221981

URL: http://llvm.org/viewvc/llvm-project?rev=221981&view=rev
Log:
clang-format: [Java] Fix line break behavior of class declarations.

Change breaking preferences:
1. Break before "extends"
2. Break before "implements"
3. Break within the implements list.

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

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=221981&r1=221980&r2=221981&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Nov 14 04:15:56 2014
@@ -1439,6 +1439,18 @@ unsigned TokenAnnotator::splitPenalty(co
 
   if (Left.is(tok::semi))
     return 0;
+
+  if (Style.Language == FormatStyle::LK_Java) {
+    if (Left.Type == TT_LeadingJavaAnnotation)
+      return 1;
+    if (Right.is(Keywords.kw_extends))
+      return 1;
+    if (Right.is(Keywords.kw_implements))
+      return 2;
+    if (Left.is(tok::comma) && Left.NestingLevel == 0)
+      return 3;
+  }
+
   if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next &&
                               Right.Next->Type == TT_DictLiteral))
     return 1;
@@ -1448,6 +1460,7 @@ unsigned TokenAnnotator::splitPenalty(co
     if (Right.Type != TT_ObjCMethodExpr && Right.Type != TT_LambdaLSquare)
       return 500;
   }
+
   if (Right.Type == TT_StartOfName ||
       Right.Type == TT_FunctionDeclarationName || Right.is(tok::kw_operator)) {
     if (Line.First->is(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
@@ -1472,12 +1485,6 @@ unsigned TokenAnnotator::splitPenalty(co
       Left.Type == TT_InheritanceColon)
     return 2;
 
-  if (Left.Type == TT_LeadingJavaAnnotation)
-    return 1;
-  if (Style.Language == FormatStyle::LK_Java &&
-      Right.is(Keywords.kw_implements))
-    return 2;
-
   if (Right.isMemberAccess()) {
     if (Left.is(tok::r_paren) && Left.MatchingParen &&
         Left.MatchingParen->ParameterCount > 0)

Modified: cfe/trunk/unittests/Format/FormatTestJava.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJava.cpp?rev=221981&r1=221980&r2=221981&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJava.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJava.cpp Fri Nov 14 04:15:56 2014
@@ -69,8 +69,11 @@ TEST_F(FormatTestJava, ClassDeclarations
                "}");
   verifyFormat("public class A extends B.C {}");
 
+  verifyFormat("abstract class SomeClass\n"
+               "    extends SomeOtherClass implements SomeInterface {}",
+               getStyleWithColumns(60));
   verifyFormat("abstract class SomeClass extends SomeOtherClass\n"
-               "    implements SomeInterface {}",
+               "    implements SomeInterfaceeeeeeeeeeeee {}",
                getStyleWithColumns(60));
   verifyFormat("abstract class SomeClass\n"
                "    extends SomeOtherClass\n"
@@ -81,14 +84,17 @@ TEST_F(FormatTestJava, ClassDeclarations
                "    implements SomeInterface,\n"
                "               AnotherInterface {}",
                getStyleWithColumns(40));
+  verifyFormat("abstract class SomeClass\n"
+               "    implements SomeInterface, AnotherInterface {}",
+               getStyleWithColumns(60));
   verifyFormat("@SomeAnnotation()\n"
-               "abstract class aaaaaaaaaaaa extends bbbbbbbbbbbbbbb\n"
-               "    implements cccccccccccc {\n"
+               "abstract class aaaaaaaaaaaa\n"
+               "    extends bbbbbbbbbbbbbbb implements cccccccccccc {\n"
                "}",
                getStyleWithColumns(76));
   verifyFormat("@SomeAnnotation()\n"
-               "abstract class aaaaaaaaa<a> extends bbbbbbbbbbbb<b>\n"
-               "    implements cccccccccccc {\n"
+               "abstract class aaaaaaaaa<a>\n"
+               "    extends bbbbbbbbbbbb<b> implements cccccccccccc {\n"
                "}",
                getStyleWithColumns(76));
   verifyFormat("interface SomeInterface<A> extends Foo, Bar {\n"





More information about the cfe-commits mailing list