SSR: javadoc matching fix and tests
authorBas Leijdekkers <basleijdekkers@gmail.com>
Wed, 16 Nov 2016 13:42:40 +0000 (14:42 +0100)
committerBas Leijdekkers <basleijdekkers@gmail.com>
Wed, 16 Nov 2016 20:21:45 +0000 (21:21 +0100)
java/structuralsearch-java/src/com/intellij/structuralsearch/impl/matcher/JavaMatchingVisitor.java
platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralReplaceTest.java
platform/structuralsearch/testSource/com/intellij/structuralsearch/StructuralSearchTest.java

index c2606724dc6e1e6b2987c4a4bb8f3537702f06b4..fd7228b3c07cfebb3896355ad4b655be2d2a5476 100644 (file)
@@ -1670,23 +1670,16 @@ public class JavaMatchingVisitor extends JavaElementVisitor {
 
   @Override
   public void visitClass(PsiClass clazz) {
+    PsiClass clazz2 = (PsiClass)myMatchingVisitor.getElement();
     if (clazz.hasTypeParameters()) {
-      myMatchingVisitor
-        .setResult(
-          myMatchingVisitor.match(clazz.getTypeParameterList(), ((PsiClass)myMatchingVisitor.getElement()).getTypeParameterList()));
-
+      myMatchingVisitor.setResult(myMatchingVisitor.match(clazz.getTypeParameterList(), clazz2.getTypeParameterList()));
       if (!myMatchingVisitor.getResult()) return;
     }
 
-    PsiClass clazz2;
-
-    if (myMatchingVisitor.getElement() instanceof PsiDeclarationStatement &&
-        myMatchingVisitor.getElement().getFirstChild() instanceof PsiClass
-      ) {
-      clazz2 = (PsiClass)myMatchingVisitor.getElement().getFirstChild();
-    }
-    else {
-      clazz2 = (PsiClass)myMatchingVisitor.getElement();
+    final PsiDocComment comment = clazz.getDocComment();
+    if (comment != null) {
+      myMatchingVisitor.setResult(myMatchingVisitor.match(comment, clazz2));
+      if (!myMatchingVisitor.getResult()) return;
     }
 
     final boolean isTypedVar = myMatchingVisitor.getMatchContext().getPattern().isTypedVar(clazz.getNameIdentifier());
index eb2c53a60396f7ba1a29852524761a3490db77c9..47db6d0d1ae00c4dad3e0b589e3486867b37afc6 100644 (file)
@@ -911,7 +911,9 @@ public class StructuralReplaceTest extends StructuralReplaceTestCase {
     String s12 = "public class $a$ {\n" +
                  "  $Other$\n" +
                  "}";
-    String expectedResult4 = "/** @example */\n" +
+    String expectedResult4 = "    /**\n" +
+                             "     * @example\n" +
+                             "     */\n" +
                              "    public class A {\n" +
                              "        class C {\n" +
                              "        }\n\n" +
index 3cf9672b81bac6505cb6efedd9da97fdb31d787d..073c3c14909d9f9797b205a1e90098cd981f6008 100644 (file)
@@ -154,17 +154,6 @@ public class StructuralSearchTest extends StructuralSearchTestCase {
                                     "  /** @serializable */ private int value2; " +
                                     "}" +
                                     "class G { /** @param a*/ void a() {} }";
-  private static final String s57_2 = "/** @author Maxim */ class C { " +
-                                      "} " +
-                                      "class D {" +
-                                      "/** @serializable */ private int value; " +
-                                      "/** @since 1.4 */ void a() {} "+
-                                      "}" +
-                                      "class F { " +
-                                      "/** @since 1.4 */ void a() {} "+
-                                      "/** @serializable */ private int value2; " +
-                                      "}" +
-                                      "class G { /** @param a*/ void a() {} }";
 
   private static final String s59 = "interface A { void B(); }";
 
@@ -1094,11 +1083,11 @@ public class StructuralSearchTest extends StructuralSearchTestCase {
 
   public void testSearchJavaDoc() {
     final String s58 = "/** @'T '_T2 */ class '_ { }";
-    assertEquals("java doc comment in class", 1, findMatchesCount(s57,s58));
-    assertEquals("java doc comment in class in file", 1, findMatchesCount(s57_2,s58,true));
+    assertEquals("java doc comment in class in file", 1, findMatchesCount(s57,s58,true));
 
     assertEquals("javadoc comment for field", 2, findMatchesCount(s57, "class '_ { /** @serializable '_* */ '_ '_; }"));
     assertEquals("javadoc comment for method", 2, findMatchesCount(s57, "class '_ { /** @'T 1.4 */ '_ '_() {} }"));
+    assertEquals("javadoc comment for method 2", 2, findMatchesCount(s57, "/** @'T 1.4 */ '_t '_m();"));
     assertEquals("just javadoc comment search", 4, findMatchesCount(s57, "/** @'T '_T2 */"));
 
     final String s84 = "    /**\n" +
@@ -1116,8 +1105,16 @@ public class StructuralSearchTest extends StructuralSearchTestCase {
     assertEquals("optional tag value match", 6, findMatchesCount(s57, "/** @'T '_T2? */"));
     assertEquals("multiple tags match +", 2, findMatchesCount(s75, " /** @'_tag+ '_value+ */"));
     assertEquals("multiple tags match *", 3, findMatchesCount(s75, " /** @'_tag* '_value* */"));
-    assertEquals("multiple tags match ?", 3, findMatchesCount(s75, " /** @'_tag? '_value? */ class 't {}"));
-    assertEquals("no infinite loop on javadoc matching", 1, findMatchesCount(s57, "/** 'Text */ class '_ { }"));
+    assertEquals("multiple tags match ?", 3, findMatchesCount(s75, " /** @'_tag? '_value? */ class 't {}", true));
+    assertEquals("no infinite loop on javadoc matching", 1, findMatchesCount(s57, "/** 'Text */ class '_ { }", true));
+
+    final String source = "class outer {\n" +
+                          "  /** bla */\n" +
+                          "  class One {}\n" +
+                          "  class Two {}\n" +
+                          "}";
+    final String pattern = "class '_A { /** '_Text */class 'B {}}";
+    assertEquals("match inner class with javadoc", 1, findMatchesCount(source, pattern));
   }
 
   public void testNamedPatterns() {