filters for groovy categories in Gdsl

This commit is contained in:
Maxim Medvedev
2010-08-08 17:15:03 +04:00
parent a5cbc1988b
commit d8763b414b
8 changed files with 138 additions and 58 deletions
@@ -19,16 +19,18 @@ package standardDsls
* @author Maxim.Medvedev
*/
gppCategory "groovy.util.Conversions"
gppCategory "groovy.util.Files"
gppCategory "groovy.util.Filters"
gppCategory "groovy.util.Iterations"
gppCategory "groovy.util.Mappers"
gppCategory "groovy.util.Sort"
gppCategory "groovy.util.Strings"
gppCategory "groovy.util.With"
gppCategory "groovy.util.concurrent.Atomics"
gppCategory "org.mbte.groovypp.runtime.ArraysMethods"
gppCategory "org.mbte.groovypp.runtime.DefaultGroovyPPMethods"
contributor([:]) {
gppCategory "groovy.util.Conversions"
gppCategory "groovy.util.Files"
gppCategory "groovy.util.Filters"
gppCategory "groovy.util.Iterations"
gppCategory "groovy.util.Mappers"
gppCategory "groovy.util.Sort"
gppCategory "groovy.util.Strings"
gppCategory "groovy.util.With"
gppCategory "groovy.util.concurrent.Atomics"
gppCategory "org.mbte.groovypp.runtime.ArraysMethods"
gppCategory "org.mbte.groovypp.runtime.DefaultGroovyPPMethods"
gppCategory "org.mbte.groovypp.runtime.DefaultGroovyPPStaticMethods", true
gppCategory "org.mbte.groovypp.runtime.DefaultGroovyPPStaticMethods", true
}
@@ -21,12 +21,12 @@ package standardDsls
* @author Maxim.Medvedev
*/
category "org.codehaus.groovy.runtime.DefaultGroovyMethods"
category "org.codehaus.groovy.runtime.DefaultGroovyStaticMethods", true
category "org.codehaus.groovy.runtime.DateGroovyMethods"
category "org.codehaus.groovy.runtime.EncodingGroovyMethods"
category "org.codehaus.groovy.runtime.SqlGroovyMethods"
category "org.codehaus.groovy.runtime.SwingGroovyMethods"
category "org.codehaus.groovy.runtime.XmlGroovyMethods"
contributor([:]) {
category "org.codehaus.groovy.runtime.DefaultGroovyMethods"
category "org.codehaus.groovy.runtime.DefaultGroovyStaticMethods", true
category "org.codehaus.groovy.runtime.DateGroovyMethods"
category "org.codehaus.groovy.runtime.EncodingGroovyMethods"
category "org.codehaus.groovy.runtime.SqlGroovyMethods"
category "org.codehaus.groovy.runtime.SwingGroovyMethods"
category "org.codehaus.groovy.runtime.XmlGroovyMethods"
}
+2
View File
@@ -61,6 +61,8 @@
<!-- GroovyDSL extensions -->
<gdslTopLevelProvider implementation="org.jetbrains.plugins.groovy.dsl.dsltop.GroovyDslDefaultMembers"/>
<gdslTopLevelProvider implementation="org.jetbrains.plugins.groovy.lang.resolve.GdkMethodDslProvider"/>
<gdslTopLevelProvider implementation="org.jetbrains.plugins.groovy.gpp.GppDslProvider"/>
<psiEnhancerCategory implementation="org.jetbrains.plugins.groovy.dsl.psi.PsiClassCategory"/>
<psiEnhancerCategory implementation="org.jetbrains.plugins.groovy.dsl.psi.PsiElementCategory"/>
<psiEnhancerCategory implementation="org.jetbrains.plugins.groovy.dsl.psi.PsiMethodCategory"/>
@@ -24,6 +24,7 @@ import com.intellij.psi.util.*;
import com.intellij.util.Function;
import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil;
import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil;
import java.util.ArrayList;
@@ -43,7 +44,7 @@ public class CategoryMethodProvider {
public static List<PsiMethod> provideMethods(@NotNull PsiType psiType,
final Project project,
String className,
GlobalSearchScope scope,
final GlobalSearchScope scope,
final Function<PsiMethod, PsiMethod> converter) {
final PsiClass categoryClass = JavaPsiFacade.getInstance(project).findClass(className, scope);
if (categoryClass == null) return Collections.emptyList();
@@ -52,10 +53,11 @@ public class CategoryMethodProvider {
@Override
public Result<MultiMap<String, PsiMethod>> compute() {
MultiMap<String, PsiMethod> map = new MultiMap<String, PsiMethod>();
PsiManager manager = PsiManager.getInstance(project);
for (PsiMethod m : categoryClass.getMethods()) {
final PsiParameter[] params = m.getParameterList().getParameters();
if (params.length == 0) continue;
final PsiType parameterType = params[0].getType();
final PsiType parameterType = TypesUtil.boxPrimitiveType(params[0].getType(), manager, scope);
PsiType targetType = TypeConversionUtil.erasure(parameterType);
map.putValue(targetType.getCanonicalText(), converter.fun(m));
}
@@ -46,40 +46,6 @@ class GdslMetaClassProperties {
}
}
Closure category = {Object[] params ->
if (params.length == 1) {
processCategoryMethods(params[0], new Function() {def fun(def param) {new GrGdkMethodImpl(param, false)}})
}
else if (params.length == 2) {
if (params[1] instanceof Boolean) {
processCategoryMethods(params[0], new Function() {def fun(def param) {new GrGdkMethodImpl(param, params[1])}})
}
else {
processCategoryMethods(params[0], params[1])
}
}
else throw new IllegalArgumentException("Incorrect aruments in method 'category': $params")
}
Closure gppCategory = {def className, def isStatic = false ->
def staticConverter = new Function() {
def fun(def m) {new GppGdkMethod(m, true)}
};
def nonStaticConverter = new Function() {
def fun(def m) {new GppGdkMethod(m, false)}
};
category className, isStatic ? staticConverter : nonStaticConverter
}
private def processCategoryMethods (def className, Function<PsiMethod, PsiMethod> converter) {
contributor(context()) {
if (!psiType) return;
List methods = CategoryMethodProvider.provideMethods(psiType, project, className, resolveScope, converter)
for (m in methods) add m
}
}
/**
* Auxiliary methods for context definition
*/
@@ -0,0 +1,45 @@
/*
* Copyright 2000-2010 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 org.jetbrains.plugins.groovy.gpp;
import com.intellij.psi.PsiMethod;
import com.intellij.util.Function;
import org.jetbrains.plugins.groovy.dsl.GdslMembersHolderConsumer;
import org.jetbrains.plugins.groovy.dsl.dsltop.GdslMembersProvider;
import org.jetbrains.plugins.groovy.lang.resolve.GdkMethodDslProvider;
/**
* @author Maxim.Medvedev
*/
@SuppressWarnings({"MethodMayBeStatic"})
public class GppDslProvider implements GdslMembersProvider {
public void gppCategory(String className, GdslMembersHolderConsumer consumer) {
gppCategory(className, false, consumer);
}
public void gppCategory(String className, boolean isStatic, GdslMembersHolderConsumer consumer) {
Function<PsiMethod, PsiMethod> staticConverter = new Function<PsiMethod, PsiMethod>() {
public PsiMethod fun(PsiMethod m) {
return new GppGdkMethod(m, true);
}
};
Function<PsiMethod, PsiMethod> nonStaticConverter = new Function<PsiMethod, PsiMethod>() {
public PsiMethod fun(PsiMethod m) {
return new GppGdkMethod(m, false);
}
};
GdkMethodDslProvider.processCategoryMethods(className, consumer, isStatic ? staticConverter : nonStaticConverter);
}
}
@@ -0,0 +1,63 @@
/*
* Copyright 2000-2010 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 org.jetbrains.plugins.groovy.lang.resolve;
import com.intellij.psi.PsiMethod;
import com.intellij.util.Function;
import org.jetbrains.plugins.groovy.dsl.GdslMembersHolderConsumer;
import org.jetbrains.plugins.groovy.dsl.dsltop.GdslMembersProvider;
import org.jetbrains.plugins.groovy.dsl.holders.DelegatedMembersHolder;
import org.jetbrains.plugins.groovy.dsl.toplevel.CategoryMethodProvider;
import org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrGdkMethodImpl;
import java.util.List;
/**
* @author Maxim.Medvedev
*/
@SuppressWarnings({"MethodMayBeStatic"})
public class GdkMethodDslProvider implements GdslMembersProvider {
public void category(String className, GdslMembersHolderConsumer consumer) {
processCategoryMethods(className, consumer, new Function<PsiMethod, PsiMethod>() {
public PsiMethod fun(PsiMethod m) {
return new GrGdkMethodImpl(m, false);
}
});
}
public void category(String className, final boolean isStatic, GdslMembersHolderConsumer consumer) {
processCategoryMethods(className, consumer, new Function<PsiMethod, PsiMethod>() {
public PsiMethod fun(PsiMethod param) {
return new GrGdkMethodImpl(param, isStatic);
}
});
}
public void category(String className, Function<PsiMethod, PsiMethod> converter, GdslMembersHolderConsumer consumer) {
processCategoryMethods(className, consumer, converter);
}
public static void processCategoryMethods(String className, GdslMembersHolderConsumer consumer, Function<PsiMethod, PsiMethod> converter) {
if (consumer.getPsiType() == null) return;
List<PsiMethod> methods =
CategoryMethodProvider.provideMethods(consumer.getPsiType(), consumer.getProject(), className, consumer.getResolveScope(), converter);
final DelegatedMembersHolder holder = new DelegatedMembersHolder();
for (PsiMethod m : methods) {
holder.addMember(m);
}
consumer.addMemberHolder(holder);
}
}
@@ -131,7 +131,7 @@ public class MyCategory {
}""")
def foo = category.getMethods()[0]
final PsiFile file = myFixture.addFileToProject(getTestName(false) + "Enhancer.gdsl", """
category 'MyCategory'""");
contributor([:]){category 'MyCategory'}""");
GroovyDslFileIndex.activateUntilModification(file.virtualFile)
myFixture.renameElement foo, "bar", false, false