mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
currentType pointcut
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package org.jetbrains.plugins.groovy.dsl;
|
||||
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.plugins.groovy.dsl.toplevel.ClassContextFilter;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public abstract class DslPointcut<T> {
|
||||
|
||||
abstract boolean matches(T t, ProcessingContext context);
|
||||
|
||||
abstract boolean operatesOn(Class c);
|
||||
|
||||
public static DslPointcut<GroovyClassDescriptor> currentType(final Object arg) {
|
||||
return new DslPointcut<GroovyClassDescriptor>() {
|
||||
@Override
|
||||
boolean matches(GroovyClassDescriptor groovyClassDescriptor, ProcessingContext context) {
|
||||
return ClassContextFilter.subtypeOf((String)arg).isApplicable(groovyClassDescriptor, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean operatesOn(Class c) {
|
||||
return GroovyClassDescriptor.class == c;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static <T> DslPointcut<T> unknownPointcut() {
|
||||
return new DslPointcut<T>() {
|
||||
@Override
|
||||
boolean matches(T t, ProcessingContext context) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean operatesOn(Class c) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public class GroovyDslExecutor {
|
||||
def enhancer = new GdslMetaClassProperties(this)
|
||||
|
||||
mc.methodMissing = { String name, Object args ->
|
||||
return new Context([:])
|
||||
return DslPointcut.unknownPointcut()
|
||||
}
|
||||
|
||||
// Fill script with necessary properties
|
||||
@@ -52,6 +52,12 @@ public class GroovyDslExecutor {
|
||||
|
||||
|
||||
def contribute = {cts, Closure toDo ->
|
||||
if (cts instanceof DslPointcut) {
|
||||
assert cts.operatesOn(GroovyClassDescriptor) : "A non top-level pointcut passed to contributor"
|
||||
addClassEnhancer([new PointcutContextFilter(cts)], toDo)
|
||||
return
|
||||
}
|
||||
|
||||
if (cts instanceof Map) {
|
||||
cts = new Context(cts)
|
||||
}
|
||||
@@ -72,6 +78,8 @@ public class GroovyDslExecutor {
|
||||
StringUtil.compareVersionNumbers(ideaVersion, ver) >= 0
|
||||
}
|
||||
|
||||
mc.currentType = { arg -> DslPointcut.currentType(arg) }
|
||||
|
||||
mc.initialize()
|
||||
script.metaClass = mc
|
||||
script.run()
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.jetbrains.plugins.groovy.dsl;
|
||||
|
||||
import com.intellij.util.ProcessingContext;
|
||||
import org.jetbrains.plugins.groovy.dsl.toplevel.ContextFilter;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class PointcutContextFilter implements ContextFilter {
|
||||
private final DslPointcut<GroovyClassDescriptor> myPointcut;
|
||||
|
||||
public PointcutContextFilter(DslPointcut<GroovyClassDescriptor> pointcut) {
|
||||
myPointcut = pointcut;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(GroovyClassDescriptor descriptor, ProcessingContext ctx) {
|
||||
return myPointcut.matches(descriptor, ctx);
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,20 @@ class DsldTest extends LightGroovyTestCase {
|
||||
}
|
||||
|
||||
public void testUnknownPointcut() {
|
||||
def file = myFixture.addFileToProject('a.gdsl', "contribute(asdfsadf()) { property name:'foo', type:'String' }")
|
||||
checkHighlighting "contribute(asdfsadf()) { property name:'foo', type:'String' }",
|
||||
'println foo.substring(2) + <warning>bar</warning>'
|
||||
}
|
||||
|
||||
public void testCurrentType() {
|
||||
checkHighlighting 'contribute(currentType("java.lang.String")) { property name:"foo" }',
|
||||
'println "".foo + [].<warning>foo</warning>'
|
||||
}
|
||||
|
||||
private def checkHighlighting(String dsl, String code) {
|
||||
def file = myFixture.addFileToProject('a.gdsl', dsl)
|
||||
GroovyDslFileIndex.activateUntilModification(file.virtualFile)
|
||||
|
||||
checkHighlighting('println foo.substring(2) + <warning>bar</warning>')
|
||||
checkHighlighting(code)
|
||||
}
|
||||
|
||||
private def checkHighlighting(String text) {
|
||||
|
||||
Reference in New Issue
Block a user