clang-format: [Java] Improve formatting of generics.
authorDaniel Jasper <djasper@google.com>
Fri, 14 Nov 2014 08:22:46 +0000 (08:22 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 14 Nov 2014 08:22:46 +0000 (08:22 +0000)
Before:
  Function < F, ? extends T > function;

After:
  Function<F, ? extends T> function;

llvm-svn: 221976

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

index 74adaf7..740adb2 100644 (file)
@@ -415,6 +415,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) {
     GoogleStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
     GoogleStyle.ColumnLimit = 100;
     GoogleStyle.SpaceAfterCStyleCast = true;
+    GoogleStyle.SpacesBeforeTrailingComments = 1;
   } else if (Language == FormatStyle::LK_JavaScript) {
     GoogleStyle.BreakBeforeTernaryOperators = false;
     GoogleStyle.MaxEmptyLinesToKeep = 3;
index 8ec8f58..f926beb 100644 (file)
@@ -63,11 +63,13 @@ private:
         next();
         return true;
       }
-      if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
-                                tok::colon))
-        return false;
       if (CurrentToken->is(tok::question) &&
-          Style.Language != FormatStyle::LK_Java)
+          Style.Language == FormatStyle::LK_Java) {
+        next();
+        continue;
+      }
+      if (CurrentToken->isOneOf(tok::r_paren, tok::r_square, tok::r_brace,
+                                tok::colon, tok::question))
         return false;
       // If a && or || is found and interpreted as a binary operator, this set
       // of angles is likely part of something like "a < b && c > d". If the
@@ -367,10 +369,6 @@ private:
   }
 
   bool parseConditional() {
-    if (Style.Language == FormatStyle::LK_Java &&
-        CurrentToken->isOneOf(tok::comma, tok::greater))
-      return true;  // This is a generic "?".
-
     while (CurrentToken) {
       if (CurrentToken->is(tok::colon)) {
         CurrentToken->Type = TT_ConditionalExpr;
index 0ee398a..650c0af 100644 (file)
@@ -203,6 +203,7 @@ TEST_F(FormatTestJava, Generics) {
 
   verifyFormat("public static <R> ArrayList<R> get() {\n}");
   verifyFormat("<T extends B> T getInstance(Class<T> type);");
+  verifyFormat("Function<F, ? extends T> function;");
 }
 
 TEST_F(FormatTestJava, StringConcatenation) {