thakis created this revision.
thakis added a reviewer: djasper.

The existing code kept the space if it was there for identifiers, and it didn't 
handle `this`. After this patch, for java `this` is handled in addition to 
identifiers, and existing space is always stripped between identifier and `::`.


https://reviews.llvm.org/D52842

Files:
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestJava.cpp


Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2774,6 +2774,9 @@
   if (!Style.SpaceBeforeAssignmentOperators &&
       Right.getPrecedence() == prec::Assignment)
     return false;
+  if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
+      (Left.is(tok::identifier) || Left.is(tok::kw_this)))
+    return false;
   if (Right.is(tok::coloncolon) && Left.is(tok::identifier))
     // Generally don't remove existing spaces between an identifier and "::".
     // The identifier might actually be a macro name such as ALWAYS_INLINE. If
Index: unittests/Format/FormatTestJava.cpp
===================================================================
--- unittests/Format/FormatTestJava.cpp
+++ unittests/Format/FormatTestJava.cpp
@@ -443,6 +443,19 @@
                getStyleWithColumns(40));
 }
 
+TEST_F(FormatTestJava, MethodReference) {
+  EXPECT_EQ("private void foo() {\n"
+            "  f(this::methodReference);\n"
+            "  f(C.super::methodReference);\n"
+            "  Consumer<String> c = System.out::println;\n"
+            "}",
+            format("private void foo() {\n"
+                   "  f(this ::methodReference);\n"
+                   "  f(C.super ::methodReference);\n"
+                   "  Consumer<String> c = System.out ::println;\n"
+                   "}"));
+}
+
 TEST_F(FormatTestJava, CppKeywords) {
   verifyFormat("public void union(Type a, Type b);");
   verifyFormat("public void struct(Object o);");


Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2774,6 +2774,9 @@
   if (!Style.SpaceBeforeAssignmentOperators &&
       Right.getPrecedence() == prec::Assignment)
     return false;
+  if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) &&
+      (Left.is(tok::identifier) || Left.is(tok::kw_this)))
+    return false;
   if (Right.is(tok::coloncolon) && Left.is(tok::identifier))
     // Generally don't remove existing spaces between an identifier and "::".
     // The identifier might actually be a macro name such as ALWAYS_INLINE. If
Index: unittests/Format/FormatTestJava.cpp
===================================================================
--- unittests/Format/FormatTestJava.cpp
+++ unittests/Format/FormatTestJava.cpp
@@ -443,6 +443,19 @@
                getStyleWithColumns(40));
 }
 
+TEST_F(FormatTestJava, MethodReference) {
+  EXPECT_EQ("private void foo() {\n"
+            "  f(this::methodReference);\n"
+            "  f(C.super::methodReference);\n"
+            "  Consumer<String> c = System.out::println;\n"
+            "}",
+            format("private void foo() {\n"
+                   "  f(this ::methodReference);\n"
+                   "  f(C.super ::methodReference);\n"
+                   "  Consumer<String> c = System.out ::println;\n"
+                   "}"));
+}
+
 TEST_F(FormatTestJava, CppKeywords) {
   verifyFormat("public void union(Type a, Type b);");
   verifyFormat("public void struct(Object o);");
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to