reorganize gdsl filters: place light and written in java filters early
authorpeter <peter.gromov@jetbrains.com>
Sat, 27 Feb 2010 11:30:30 +0000 (11:30 +0000)
committerpeter <peter.gromov@jetbrains.com>
Sat, 27 Feb 2010 11:40:33 +0000 (11:40 +0000)
plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/toplevel/ClassContextFilter.java
plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/toplevel/Context.groovy
plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/toplevel/PlaceContextFilter.java [new file with mode: 0644]
plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/toplevel/scopes/Scope.groovy

index 565eeab3c9a7352bf57c491a9ea9fb7ad488a4db..462c2ab5296269bcb4fd640c6b24678d1a05cfcf 100644 (file)
@@ -18,7 +18,7 @@ public class ClassContextFilter implements ContextFilter {
   }
 
   public boolean isApplicable(PsiElement place, String fqName, ProcessingContext ctx) {
-    return myPattern.accepts(findPsiClass(place, fqName, ctx));
+    return myPattern.accepts(findPsiClass(place, fqName, ctx), ctx);
   }
 
   @Nullable
index cd8c900b6b7848208fc7a236dd8b3f03d910055f..d4e93e18bcdaa7d24016502ae132f78461c6daf7 100644 (file)
@@ -1,24 +1,21 @@
 package org.jetbrains.plugins.groovy.dsl.toplevel
 
-import com.intellij.openapi.util.Pair
 import com.intellij.openapi.util.text.StringUtil
+import com.intellij.patterns.PlatformPatterns
 import com.intellij.patterns.PsiElementPattern
 import com.intellij.patterns.PsiJavaPatterns
-
 import com.intellij.psi.PsiElement
-
 import com.intellij.psi.util.PsiTreeUtil
-import com.intellij.util.containers.ConcurrentHashSet
 import org.jetbrains.plugins.groovy.dsl.toplevel.scopes.ClassScope
 import org.jetbrains.plugins.groovy.dsl.toplevel.scopes.ClosureScope
 import org.jetbrains.plugins.groovy.dsl.toplevel.scopes.ScriptScope
-import org.jetbrains.plugins.groovy.lang.psi.GroovyFile
 import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression
 import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition
+import org.jetbrains.plugins.groovy.lang.psi.patterns.GroovyPatterns
 
 /**
  * @author ilyas
@@ -31,14 +28,6 @@ class Context {
     // Named parameter processing
     if (!args) return
 
-    // ctype : <ctype>
-    // Qualifier type to be augmented
-    if (args.ctype instanceof String) {
-      addFilter getClassTypeFilter(args.ctype)
-    } else if (args.ctype instanceof PsiElementPattern) {
-      addFilter new ClassContextFilter(args.ctype)
-    }
-
     // filetypes : [<file_ext>*]
     if (args.filetypes && args.filetypes instanceof List) {
       addFilter {PsiElement elem, fqn, ctx ->
@@ -55,6 +44,7 @@ class Context {
       }
     }
 
+    // filter by scope first, then by ctype
     // scope: <scope>
     switch (args.scope) {
       case null: break
@@ -62,15 +52,16 @@ class Context {
     // handling script scope
       case org.jetbrains.plugins.groovy.dsl.toplevel.scopes.ScriptScope:
         def scope = (ScriptScope) args.scope
-        addFilter {PsiElement elem, fqn, ctx ->
-          def parent = PsiTreeUtil.getParentOfType(elem, GrTypeDefinition.class, GroovyFile.class)
-          if (parent instanceof GroovyFile && ((GroovyFile) parent).isScript()) {
-            return ((GroovyFile) parent).getName().matches(scope.namePattern)
-          }
-          return false
-        }
+
+        //first, it should be inside groovy script
+        addFilter new PlaceContextFilter(PlatformPatterns.psiElement().inFile(GroovyPatterns.groovyScript()))
+
         // Name matcher
-        addFilter {PsiElement elem, fqn, ctx -> elem.containingFile.name.matches(scope.namePattern)}
+        def namePattern = scope.namePattern
+        if (namePattern) {
+          addFilter {PsiElement elem, fqn, ctx -> elem.containingFile.name.matches(namePattern)}
+        }
+
         // Process unqualified references only
         if (!args.ctype) {
           addFilter getClassTypeFilter(GroovyFileBase.SCRIPT_BASE_CLASS_NAME)
@@ -79,48 +70,54 @@ class Context {
         break
     // handling class scope
       case org.jetbrains.plugins.groovy.dsl.toplevel.scopes.ClassScope:
-        addFilter {GrReferenceExpression elem, fqn, ctx ->
-          final def classScope = (ClassScope) args.scope
-          if (!classScope.getName()) return false;
-          final GrTypeDefinition clazz = PsiTreeUtil.getParentOfType(elem, GrTypeDefinition)
-          if (clazz) {
-            final def qualName = clazz.getQualifiedName()
-            return clazz.getName().matches(classScope.getName()) ||
-                   qualName && qualName.matches(classScope.getName())
+        final def classScope = (ClassScope) args.scope
+        def namePattern = classScope.getName()
+        if (namePattern) {
+          addFilter {GrReferenceExpression elem, fqn, ctx ->
+            final GrTypeDefinition clazz = PsiTreeUtil.getParentOfType(elem, GrTypeDefinition)
+            if (clazz) {
+              final def qualName = clazz.getQualifiedName()
+              return clazz.getName().matches(namePattern) || qualName && qualName.matches(namePattern)
+            }
+            return false
           }
-          return false
-        }
-        if (!args.ctype) {
-          addFilter getClassTypeFilter("java.lang.Object")
         }
         break
 
     // handling closure scope
       case org.jetbrains.plugins.groovy.dsl.toplevel.scopes.ClosureScope:
-        // Enhance only unqualified expressions
-        if (!args.ctype) {
-          addFilter getClassTypeFilter("groovy.lang.Closure")
-        }
+        addFilter new PlaceContextFilter(PsiJavaPatterns.psiElement().inside(GrClosableBlock))
+
+        if (((ClosureScope) args.scope).isArg()) {
+          // Filter for call parameter
+          addFilter {GrReferenceExpression elem, fqn, ctx ->
+            def closParent = PsiTreeUtil.getParentOfType(elem, GrClosableBlock.class)
+            assert closParent != null
 
-        // Enhance closure contexts only
-        addFilter {GrReferenceExpression elem, fqn, ctx ->
-          def closParent = PsiTreeUtil.getParentOfType(elem, GrClosableBlock.class)
-          if (closParent == null) return false
-          def scope = (ClosureScope) args.scope
-          if (scope.isArg()) {
             def parent = closParent.getParent()
             if (parent instanceof GrArgumentList) {
-              return parent.getParent() instanceof GrMethodCallExpression
-            } else {
-              return parent instanceof GrMethodCallExpression
+              parent = parent.parent
             }
+            return parent instanceof GrMethodCallExpression
           }
-          return true
+        }
+
+        // Enhance only unqualified expressions
+        if (!args.ctype) {
+          addFilter getClassTypeFilter("groovy.lang.Closure")
         }
         break
 
       default: break
     }
+
+    // ctype : <ctype>
+    // Qualifier type to be augmented
+    if (args.ctype instanceof String) {
+      addFilter getClassTypeFilter(args.ctype)
+    } else if (args.ctype instanceof PsiElementPattern) {
+      addFilter new ClassContextFilter(args.ctype)
+    }
   }
 
   private ContextFilter getClassTypeFilter(String ctype) {
@@ -130,7 +127,7 @@ class Context {
   private def addFilter(Closure cl) {
     addFilter (cl as ContextFilter)
   }
-  private def addFilter(ContextFilter cl) {
+  private void addFilter(ContextFilter cl) {
     myFilters << cl
   }
 
diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/toplevel/PlaceContextFilter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/dsl/toplevel/PlaceContextFilter.java
new file mode 100644 (file)
index 0000000..c040f31
--- /dev/null
@@ -0,0 +1,21 @@
+package org.jetbrains.plugins.groovy.dsl.toplevel;
+
+import com.intellij.patterns.ElementPattern;
+import com.intellij.psi.PsiElement;
+import com.intellij.util.ProcessingContext;
+
+/**
+ * @author peter
+ */
+public class PlaceContextFilter implements ContextFilter {
+  private final ElementPattern<PsiElement> myPattern;
+
+  public PlaceContextFilter(ElementPattern<PsiElement> pattern) {
+    myPattern = pattern;
+  }
+
+  public boolean isApplicable(PsiElement place, String fqName, ProcessingContext ctx) {
+    return myPattern.accepts(place, ctx);
+  }
+
+}
\ No newline at end of file
index d86dfcc860feaa4f226017d8045537701e44a263..e2d814192436a450f52f6ef84cf5e5c10ea52876 100644 (file)
@@ -6,7 +6,7 @@ package org.jetbrains.plugins.groovy.dsl.toplevel.scopes
 abstract class Scope {}
 
 class ClassScope extends Scope {
-  private final myNamePattern
+  private final String myNamePattern
 
   ClassScope(Map args) {
     myNamePattern = args && args.name ? args.name : /.*/
@@ -39,7 +39,9 @@ class ScriptScope extends Scope {
   final String namePattern
 
   ScriptScope(Map args) {
-    namePattern = args && args.name ? args.name : /.*/
+    if (args && args.name) {
+      namePattern = args.name
+    }
   }
 
 }