Can I use GDSL with Groovy AST transforms, that have closures as parameters?

已回答

So I have a question about gdsl files, and if I can use them to add IDE support for a Groovy AST Transform, made for a Grails plugin, that takes up to three closures as parameters? The closures are eventually delegated to a Grails service, which is generate by the plugins install. What I'm looking to get out of this is that the IDE won't mark calls to the Grails service, in the closures as unresolved. If it could allow for code completion of the various methods from the Grails service that would be a bonus.

The plugin:
https://github.com/virtualdogbert/Enforcer/

The AST Transform:

https://github.com/virtualdogbert/Enforcer/blob/master/src/main/groovy/com/virtualdogbert/ast/Enforce.groovy
https://github.com/virtualdogbert/Enforcer/blob/master/src/main/groovy/com/virtualdogbert/ast/EnforceASTTransformation.groovy

If this can be done, my next question is how? I've looked through documentation and examples, but haven't found any that targets a Groovy AST Transform, with closure parameters. I've also tried something based off:
http://www.tothenew.com/blog/gdsl-awesomeness-delegating-closure-calls/

but it didn't work.

1
正式评论

Hi 

You need closure scope restricted to closures within annotation. 

contributor(context(scope: closureScope(annotationName: "com.virtualdogbert.ast.Enforce"))) {
delegatesTo(findClass(DELEGATE_FQN))
}

I've not found `enforserService` in your repo, you just substitute DELEGATE_FQN with its fully qualified name.

That worked thanks. Now I just have to make a few changes, so that the service is always installed to the same location(rather than user chosen), and I can do a new release. Thanks again.

0

Yes, you can use the Groovy Dynamic Language Support (GDSL) with Groovy AST transforms that have closures as parameters.

When using GDSL with Groovy AST transforms, you can define type information and method signatures for your code, including closures that are used as parameters. This can help with code completion and other features in your IDE.

To define the type information for a closure parameter in your AST transform, you can use the @ClosureParams annotation. For example:

import groovy.transform.CompileStatic
import org.codehaus.groovy.transform.stc.ClosureParams

@CompileStatic
class MyTransform {
    @ClosureParams(value = [String.class, int.class], options = "strict")
    static void myMethod(Closure closure) {
        // ...
    }
}

In this example, the @ClosureParams annotation is used to specify that the closure parameter of the myMethod method takes two parameters: a String and an int. The options parameter is set to "strict" to ensure that the closure can only be called with exactly two arguments of the specified types.

Once you have defined the type information for your closure parameter in your AST transform, you can use GDSL to provide code completion and other features for closures that match the specified signature.

https://semidotinfotech.com/

0

You can define closures as parameters in your GDSL script definition by using the closure technique. Consider the AST transformation that adds the following method to the class, for instance:

@Builder
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
class AddMethodTransform implements ASTTransformation {
    void visit(ASTNode[] nodes, SourceUnit sourceUnit) {
        nodes.each { node ->
            if (node instanceof ClassNode) {
                def classNode = node
                classNode.addMethod(new MethodNode('myMethod', Modifier.PUBLIC, ClassHelper.OBJECT_TYPE, [], [], new ReturnStatement(new ConstantExpression('Hello, World!'))))
            }
        }
    }
}


You can create a closure argument using the closure method to create a GDSL script that uses this transform:

contributor(context(scope: closure({ node ->
    if (node instanceof ClassNode) {
        def classNode = node
        proposeMethod(classNode, 'myMethod', {
            parameters {}
            returnType 'java.lang.Object'
            body { "return 'Hello, World!'" }
        })
    }
})))

In this script, a closure parameter that accepts a node argument is defined using the closure technique. The closure then determines whether the node is an instance of ClassNode, and if it is, it uses the proposeMethod method to suggest a new method.

Keep in mind that you can use the same syntax and techniques in the closure parameter as in standard GDSL scripts. The closure parameter's additional node argument, which you can use to access the AST node being converted, is the only distinction.

Overall, Groovy AST transforms that take closures as parameters can be used with GDSL by setting closure parameters in your GDSL script and interacting with the AST nodes that are being transformed using those parameters.

https://www.exatosoftware.com/

0

As of my last update in September 2021, the combination of Groovy AST (Abstract Syntax Tree) transforms and GDSL (Groovy-Eclipse DSL) is possible, and it provides powerful capabilities for metaprogramming in Groovy.

Groovy AST transforms allow you to modify or enhance the Abstract Syntax Tree of your Groovy code at compile-time. This enables you to apply custom annotations and perform various transformations to your code before it gets compiled into bytecode.

On the other hand, GDSL is a DSL (Domain-Specific Language) used in the Eclipse IDE for code assist and navigation. It provides IDE features like code completion, code navigation, and tooltips based on the DSL definitions you provide.

By combining Groovy AST transforms with GDSL, you can improve your development experience by providing enhanced code assist and navigation features specific to your AST-transformed code.

To set up GDSL for your AST-transformed code in Eclipse, you generally need to define the GDSL script, which describes the DSL for your code. This script should be placed in the 'dsl' folder in your Eclipse project.

However, keep in mind that the capabilities and integration between Groovy AST transforms and GDSL may have evolved beyond my last update. I recommend checking the latest documentation, official websites development, and community forums for any updates or changes related to Groovy, AST transforms, and GDSL support in your current environment.

0

The post by Daniil Ovchinnikov is the correct answer using GDSL, all others should be ignored, and I wouldn't click any links from their post. Especially the last one which looks like it was produced by Chat GPT. Intellij is the one that uses GDSL and I'm pretty sure Eclipse uses DSLD which is described on GitHub:
https://github.com/groovy/groovy-eclipse/wiki/DSL-Descriptors

0

请先登录再写评论。