file structure filtering tests
authorKonstantin Bulenkov <kb@jetbrains.com>
Thu, 2 Feb 2012 08:20:58 +0000 (09:20 +0100)
committerKonstantin Bulenkov <kb@jetbrains.com>
Thu, 2 Feb 2012 08:22:19 +0000 (09:22 +0100)
14 files changed:
java/java-tests/testData/fileStructure/filtering/AnonymousType.java [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/AnonymousType.tree [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/Camel.java [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/Camel.tree [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/Camel2.java [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/Camel2.tree [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/ReturnValue.java [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/ReturnValue.tree [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/Simple.java [new file with mode: 0644]
java/java-tests/testData/fileStructure/filtering/Simple.tree [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/ide/fileStructure/JavaFileStructureFilteringTest.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/ide/fileStructure/JavaFileStructureTestCase.java
platform/lang-impl/src/com/intellij/ide/util/FileStructurePopup.java
platform/testFramework/src/com/intellij/testFramework/FileStructureTestBase.java

diff --git a/java/java-tests/testData/fileStructure/filtering/AnonymousType.java b/java/java-tests/testData/fileStructure/filtering/AnonymousType.java
new file mode 100644 (file)
index 0000000..21731da
--- /dev/null
@@ -0,0 +1,8 @@
+import java.awt.*;
+
+class AnonymousType<caret> {
+  int num;
+  void foo() {
+    new java.awt.Point(){};
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/fileStructure/filtering/AnonymousType.tree b/java/java-tests/testData/fileStructure/filtering/AnonymousType.tree
new file mode 100644 (file)
index 0000000..6c7561f
--- /dev/null
@@ -0,0 +1,4 @@
+-AnonymousType.java
+ -AnonymousType
+  -foo():void
+   [$1]
\ No newline at end of file
diff --git a/java/java-tests/testData/fileStructure/filtering/Camel.java b/java/java-tests/testData/fileStructure/filtering/Camel.java
new file mode 100644 (file)
index 0000000..c0794f7
--- /dev/null
@@ -0,0 +1,7 @@
+class Camel<caret> {
+  void doSomething(){}
+  void doSomethingWith(){}
+  void doSomethingWithMyHead(){}
+  void getSomeHead(){}
+  void getSomeArtHeader(){}
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/fileStructure/filtering/Camel.tree b/java/java-tests/testData/fileStructure/filtering/Camel.tree
new file mode 100644 (file)
index 0000000..0abeed9
--- /dev/null
@@ -0,0 +1,3 @@
+-Camel.java
+ -Camel
+  [getSomeHead():void]
\ No newline at end of file
diff --git a/java/java-tests/testData/fileStructure/filtering/Camel2.java b/java/java-tests/testData/fileStructure/filtering/Camel2.java
new file mode 100644 (file)
index 0000000..a30da13
--- /dev/null
@@ -0,0 +1,7 @@
+class Camel2<caret> {
+  void doSomething(){}
+  void doSomethingWith(){}
+  void doSomethingWithMyHead(){}
+  void doSomeHead(){}
+  void doSomeArtHeader(){}
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/fileStructure/filtering/Camel2.tree b/java/java-tests/testData/fileStructure/filtering/Camel2.tree
new file mode 100644 (file)
index 0000000..f909604
--- /dev/null
@@ -0,0 +1,5 @@
+-Camel2.java
+ -Camel2
+  doSomeArtHeader():void
+  [doSomeHead():void]
+  doSomethingWithMyHead():void
\ No newline at end of file
diff --git a/java/java-tests/testData/fileStructure/filtering/ReturnValue.java b/java/java-tests/testData/fileStructure/filtering/ReturnValue.java
new file mode 100644 (file)
index 0000000..d99fd72
--- /dev/null
@@ -0,0 +1,10 @@
+import java.awt.*;
+
+class ReturnValue {
+  void aaa(){<caret>}
+  Point getLocationOnScreen(){}
+  Point getPoint(){}
+  void fff2(){}
+  void fff3(){}
+  void fff4(){}
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/fileStructure/filtering/ReturnValue.tree b/java/java-tests/testData/fileStructure/filtering/ReturnValue.tree
new file mode 100644 (file)
index 0000000..f8bfa09
--- /dev/null
@@ -0,0 +1,4 @@
+-ReturnValue.java
+ -ReturnValue
+  getLocationOnScreen():Point
+  [getPoint():Point]
\ No newline at end of file
diff --git a/java/java-tests/testData/fileStructure/filtering/Simple.java b/java/java-tests/testData/fileStructure/filtering/Simple.java
new file mode 100644 (file)
index 0000000..24a62e9
--- /dev/null
@@ -0,0 +1,8 @@
+class Simple<caret> {
+  void aaa(){}
+  void bbb(){}
+  void fff(){}
+  void fff2(){}
+  void fff3(){}
+  void fff4(){}
+}
diff --git a/java/java-tests/testData/fileStructure/filtering/Simple.tree b/java/java-tests/testData/fileStructure/filtering/Simple.tree
new file mode 100644 (file)
index 0000000..c2c6413
--- /dev/null
@@ -0,0 +1,6 @@
+-Simple.java
+ -Simple
+  [fff():void]
+  fff2():void
+  fff3():void
+  fff4():void
diff --git a/java/java-tests/testSrc/com/intellij/ide/fileStructure/JavaFileStructureFilteringTest.java b/java/java-tests/testSrc/com/intellij/ide/fileStructure/JavaFileStructureFilteringTest.java
new file mode 100644 (file)
index 0000000..848828f
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2000-2012 JetBrains s.r.o.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.intellij.ide.fileStructure;
+
+/**
+ * @author Konstantin Bulenkov
+ */
+public class JavaFileStructureFilteringTest extends JavaFileStructureTestCase {
+  @Override
+  protected String getTestDataFolderName() {
+    return "filtering";
+  }
+
+  public void testSimple()throws Exception{checkTree("f");}
+  public void testReturnValue()throws Exception{checkTree("point");}
+  public void testAnonymousType()throws Exception{checkTree("point");}
+  public void testCamel()throws Exception{checkTree("sohe");}
+  public void testCamel2()throws Exception{checkTree("soHe");}
+
+}
index c3559b0370f35d5cf6a3b30c353ab3772738fa45..f5c6da4031ccefbc2723cbd8bc7ea037703f6de0 100644 (file)
@@ -20,6 +20,7 @@ import com.intellij.ide.structureView.impl.java.JavaAnonymousClassesNodeProvider
 import com.intellij.ide.util.FileStructurePopup;
 import com.intellij.ide.util.PropertiesComponent;
 import com.intellij.testFramework.FileStructureTestBase;
+import com.intellij.testFramework.IdeaTestCase;
 
 /**
  * @author Konstantin Bulenkov
@@ -27,6 +28,10 @@ import com.intellij.testFramework.FileStructureTestBase;
 public abstract class JavaFileStructureTestCase extends FileStructureTestBase {
   private boolean myShowAnonymousByDefault;
 
+  protected JavaFileStructureTestCase() {
+    IdeaTestCase.initPlatformPrefix();
+  }
+
   protected abstract String getTestDataFolderName();
 
   @Override
index 3238ffc635d07d9398ac531389c0f761c2a1c6e3..58ecff594a9c2fddee592110ed70219091b11783 100644 (file)
@@ -179,132 +179,7 @@ public class FileStructurePopup implements Disposable {
     myTree.setRootVisible(false);
     myTree.setShowsRootHandles(true);
 
-    mySpeedSearch = new TreeSpeedSearch(myTree, new Convertor<TreePath, String>() {
-      @Nullable
-      public String convert(TreePath path) {
-        final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
-        final Object userObject = node.getUserObject();
-        if (userObject instanceof FilteringTreeStructure.FilteringNode) {
-          return getText(((FilteringTreeStructure.FilteringNode)userObject).getDelegate());
-        }
-        return "";
-      }
-    }, true) {
-      @Override
-      protected Point getComponentLocationOnScreen() {
-        return myPopup.getContent().getLocationOnScreen();
-      }
-
-      @Override
-      protected Rectangle getComponentVisibleRect() {
-        return myPopup.getContent().getVisibleRect();
-      }
-
-      @Override
-      protected Object findElement(String s) {
-        List<ObjectWithWeight> elements = new ArrayList<ObjectWithWeight>();
-        s = s.trim();
-        final ListIterator<Object> it = getElementIterator(0);
-        while (it.hasNext()) {
-          final ObjectWithWeight o = new ObjectWithWeight(it.next(), s, getComparator());
-          if (!o.weights.isEmpty()) {
-            elements.add(o);
-          }
-        }
-        ObjectWithWeight cur = null;
-        ArrayList<ObjectWithWeight> current = new ArrayList<ObjectWithWeight>();
-        for (ObjectWithWeight element : elements) {
-          if (cur == null) {
-            cur = element;
-            current.add(cur);
-            continue;
-          }
-
-          final int i = element.compareWith(cur);
-          if (i == 0) {
-            current.add(element);
-          } else if (i < 0) {
-            cur = element;
-            current.clear();
-            current.add(cur);
-          }
-        }
-
-        return current.isEmpty() ? null : findClosestTo(myInitialPsiElement, current);
-      }
-
-      @Nullable
-      private Object findClosestTo(PsiElement path, ArrayList<ObjectWithWeight> paths) {
-        if (path == null || myInitialPsiElement == null) {
-          return paths.get(0).node;
-        }
-        final Set<PsiElement> parents = getAllParents(myInitialPsiElement);
-        ArrayList<TreePath> cur = new ArrayList<TreePath>();
-        int max = -1;
-        for (ObjectWithWeight p : paths) {
-          final Object last = ((TreePath)p.node).getLastPathComponent();
-          final List<PsiElement> elements = new ArrayList<PsiElement>();
-          final Object object = ((DefaultMutableTreeNode)last).getUserObject();
-          if (object instanceof FilteringTreeStructure.FilteringNode) {
-            FilteringTreeStructure.FilteringNode node = (FilteringTreeStructure.FilteringNode)object;
-            while (node != null) {
-              elements.add(getPsi(node));
-              node = node.getParentNode();
-            }
-            final int size = ContainerUtil.intersection(parents, elements).size();
-            if (size > max) {
-              max = size;
-              cur.clear();
-              cur.add((TreePath)p.node);
-            } else if (size == max) {
-              cur.add((TreePath)p.node);
-            }
-          }
-        }
-
-        Collections.sort(cur, new Comparator<TreePath>() {
-          @Override
-          public int compare(TreePath o1, TreePath o2) {
-            return o2.getPathCount() - o1.getPathCount();
-          }
-        });
-        return cur.isEmpty() ? null : cur.get(0);
-      }
-
-      class ObjectWithWeight {
-        final Object node;
-        final List<TextRange> weights = new ArrayList<TextRange>();
-
-        ObjectWithWeight(Object element, String pattern, SpeedSearchComparator comparator) {
-          this.node = element;
-          final String text = getElementText(element);
-          if (text != null) {
-            final Iterable<TextRange> ranges = comparator.matchingFragments(pattern, text);
-            if (ranges != null) {
-              for (TextRange range : ranges) {
-                weights.add(range);
-              }
-            }
-          }
-          Collections.sort(weights, TEXT_RANGE_COMPARATOR);
-        }
-        
-        int compareWith(ObjectWithWeight obj) {
-          final List<TextRange> w = obj.weights;
-          for (int i = 0; i < weights.size(); i++) {
-            if (i >= w.size()) return 1;
-            final int result = TEXT_RANGE_COMPARATOR.compare(weights.get(i), w.get(i));
-            if (result != 0) {
-              return result;
-            }
-          }
-          
-          return 0;
-        }
-        
-      }
-      
-    };
+    mySpeedSearch = new MyTreeSpeedSearch();
     mySpeedSearch.setComparator(new SpeedSearchComparator(false, true));
 
     final FileStructurePopupFilter filter = new FileStructurePopupFilter();
@@ -327,8 +202,8 @@ public class FileStructurePopup implements Disposable {
       }
     };
 
-    myAbstractTreeBuilder.getUi().getUpdater().setDelay(1);
-
+    myAbstractTreeBuilder.getUi().getUpdater().setPassThroughMode(true);
+    myInitialPsiElement = getCurrentElement(getPsiFile(myProject));
     //myAbstractTreeBuilder.setCanYieldUpdate(true);
     Disposer.register(this, myAbstractTreeBuilder);
   }
@@ -399,7 +274,6 @@ public class FileStructurePopup implements Disposable {
         myAbstractTreeBuilder.queueUpdate().doWhenDone(new Runnable() {
           @Override
           public void run() {
-            myInitialPsiElement = getCurrentElement(getPsiFile(myProject));
             selectPsiElement(myInitialPsiElement);
             treeHasBuilt.setDone();
             //long t = System.currentTimeMillis() - time;
@@ -856,4 +730,136 @@ public class FileStructurePopup implements Disposable {
     return mySpeedSearch != null && !StringUtil.isEmpty(mySpeedSearch.getEnteredPrefix())
                     ? mySpeedSearch.getEnteredPrefix() : null;
   }
+
+  public class MyTreeSpeedSearch extends TreeSpeedSearch {
+    public MyTreeSpeedSearch() {
+      super(FileStructurePopup.this.myTree, new Convertor<TreePath, String>() {
+        @Nullable
+        public String convert(TreePath path) {
+          final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
+          final Object userObject = node.getUserObject();
+          if (userObject instanceof FilteringTreeStructure.FilteringNode) {
+            return FileStructurePopup.getText(((FilteringTreeStructure.FilteringNode)userObject).getDelegate());
+          }
+          return "";
+        }
+      }, true);
+    }
+
+    @Override
+    protected Point getComponentLocationOnScreen() {
+      return myPopup.getContent().getLocationOnScreen();
+    }
+
+    @Override
+    protected Rectangle getComponentVisibleRect() {
+      return myPopup.getContent().getVisibleRect();
+    }
+
+    @Override
+    public Object findElement(String s) {
+      List<ObjectWithWeight> elements = new ArrayList<ObjectWithWeight>();
+      s = s.trim();
+      final ListIterator<Object> it = getElementIterator(0);
+      while (it.hasNext()) {
+        final ObjectWithWeight o = new ObjectWithWeight(it.next(), s, getComparator());
+        if (!o.weights.isEmpty()) {
+          elements.add(o);
+        }
+      }
+      ObjectWithWeight cur = null;
+      ArrayList<ObjectWithWeight> current = new ArrayList<ObjectWithWeight>();
+      for (ObjectWithWeight element : elements) {
+        if (cur == null) {
+          cur = element;
+          current.add(cur);
+          continue;
+        }
+
+        final int i = element.compareWith(cur);
+        if (i == 0) {
+          current.add(element);
+        } else if (i < 0) {
+          cur = element;
+          current.clear();
+          current.add(cur);
+        }
+      }
+
+      return current.isEmpty() ? null : findClosestTo(myInitialPsiElement, current);
+    }
+
+    @Nullable
+    private Object findClosestTo(PsiElement path, ArrayList<ObjectWithWeight> paths) {
+      if (path == null || myInitialPsiElement == null) {
+        return paths.get(0).node;
+      }
+      final Set<PsiElement> parents = getAllParents(myInitialPsiElement);
+      ArrayList<ObjectWithWeight> cur = new ArrayList<ObjectWithWeight>();
+      int max = -1;
+      for (ObjectWithWeight p : paths) {
+        final Object last = ((TreePath)p.node).getLastPathComponent();
+        final List<PsiElement> elements = new ArrayList<PsiElement>();
+        final Object object = ((DefaultMutableTreeNode)last).getUserObject();
+        if (object instanceof FilteringTreeStructure.FilteringNode) {
+          FilteringTreeStructure.FilteringNode node = (FilteringTreeStructure.FilteringNode)object;
+          while (node != null) {
+            elements.add(getPsi(node));
+            node = node.getParentNode();
+          }
+          final int size = ContainerUtil.intersection(parents, elements).size();
+          if (size > max) {
+            max = size;
+            cur.clear();
+            cur.add(p);
+          } else if (size == max) {
+            cur.add(p);
+          }
+        }
+      }
+
+      Collections.sort(cur, new Comparator<ObjectWithWeight>() {
+        @Override
+        public int compare(ObjectWithWeight o1, ObjectWithWeight o2) {
+          final int i = o1.compareWith(o2);
+          return i != 0 ? i 
+                        : ((TreePath)o2.node).getPathCount() - ((TreePath)o1.node).getPathCount();
+        }
+      });
+      return cur.isEmpty() ? null : cur.get(0).node;
+    }
+
+    class ObjectWithWeight {
+      final Object node;
+      final List<TextRange> weights = new ArrayList<TextRange>();
+
+      ObjectWithWeight(Object element, String pattern, SpeedSearchComparator comparator) {
+        this.node = element;
+        final String text = getElementText(element);
+        if (text != null) {
+          final Iterable<TextRange> ranges = comparator.matchingFragments(pattern, text);
+          if (ranges != null) {
+            for (TextRange range : ranges) {
+              weights.add(range);
+            }
+          }
+        }
+        Collections.sort(weights, TEXT_RANGE_COMPARATOR);
+      }
+
+      int compareWith(ObjectWithWeight obj) {
+        final List<TextRange> w = obj.weights;
+        for (int i = 0; i < weights.size(); i++) {
+          if (i >= w.size()) return 1;
+          final int result = TEXT_RANGE_COMPARATOR.compare(weights.get(i), w.get(i));
+          if (result != 0) {
+            return result;
+          }
+        }
+
+        return 0;
+      }
+
+    }
+  }
 }
index 99c205834e9489db560920056cf08b2fc73b47e9..269ed4ca8546266a6bb910872708693f000552a3 100644 (file)
@@ -22,7 +22,6 @@ import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.io.FileUtil;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.testFramework.fixtures.CodeInsightFixtureTestCase;
-import com.intellij.ui.TreeSpeedSearch;
 import com.intellij.ui.treeStructure.Tree;
 import com.intellij.ui.treeStructure.filtered.FilteringTreeBuilder;
 import com.intellij.ui.treeStructure.filtered.FilteringTreeStructure;
@@ -30,6 +29,7 @@ import com.intellij.util.ui.tree.TreeUtil;
 import junit.framework.Assert;
 import org.junit.Before;
 
+import javax.swing.tree.TreePath;
 import java.io.File;
 
 /**
@@ -68,6 +68,14 @@ public abstract class FileStructureTestBase extends CodeInsightFixtureTestCase {
     return getFileName("tree");
   }
 
+  protected void checkTree(String filter) throws Exception {
+    myPopup.setSearchFilterForTests(filter);
+    getBuilder().refilter(null, false, true);
+    getBuilder().queueUpdate();
+    TreeUtil.selectPath(getTree(), (TreePath)getSpeedSearch().findElement(filter));
+    checkTree();
+  }
+
   protected void checkTree() throws Exception {
     final String expected = FileUtil.loadFile(new File(getTestDataPath() + "/" + getTreeFileName()), true);
     Assert.assertEquals(expected.trim(), PlatformTestUtil.print(getTree(), true).trim());
@@ -97,8 +105,8 @@ public abstract class FileStructureTestBase extends CodeInsightFixtureTestCase {
     return myPopup.getTreeBuilder();
   }
 
-  protected TreeSpeedSearch getSpeedSearch() {
-    return myPopup.getSpeedSearch();
+  protected FileStructurePopup.MyTreeSpeedSearch getSpeedSearch() {
+    return (FileStructurePopup.MyTreeSpeedSearch)myPopup.getSpeedSearch();
   }