Groovy script AST transforms

Answered

Hello

I have my own AST transformations in groovy. I use intellij and want the compiler to pick up my AST transformations as part of compiling/validating the groovy script

One example is this. I have an annotation @NoCode which I will use to ensure that a function has no code in it. so

@NoCode
def func1() {} // is valid

@NoCode
def func2() {func1();} // must be invalid, therefore expecting to see red squiggly lines

My AST transforms work and I am able to get the compile error I use when I see code. How do I now integrate this with intellij? I tried using a config.groovy with this transformation in it and setting this config.groovy file in the configscript option for the groovy compiler. That did not work.

Any help is appreciated

My config.groovy is this

import com.me.runtime.NoCode

def analysis = new ASTTransformationCustomizer(NoCode)
configuration.addCompilationCustomizers(analysis)
configuration.setScriptBaseClass("com.mybase.RuntimeScript")
2
5 comments

Highlighting should be provided by providing Annotator http://www.jetbrains.org/intellij/sdk/docs/reference_guide/custom_language_support/syntax_highlighting_and_error_highlighting.html (or inspection if ability to switch on/off + configuration is needed).

 

For reference, org.jetbrains.plugins.groovy.transformations.AstTransformationSupport extension point allows the IDE to understand actual transformations.

0
Avatar
Permanently deleted user

Thank you for your prompt response. I am new to writing plugins so please bear with me.

From your answer and some googling, I see that ASTTransformationSupport must be implemented for every ASTTransformation I write for groovy. The question I have though is this

a) I specify my AST transformation through a config.groovy script (as shown above) and never directly on the script itself. This config.groovy is referenced in the Groovy Compiler settings page for intellij. Is that the right spot for this? and will that automatically trigger custom ASTTransformationSupport plugins?

b) If the AST transformations plugin is implemented, will that feed into the debugger as well? because the IDE now knows the extra code that was inserted?

c) Is there a help page to help me get started with ASTTransformationSupport? I am looking for basic stuff, like getting started with plugin writing and moving on to ASTTransformation

0

a) org.jetbrains.plugins.groovy.transformations.AstTransformationSupport extension point is part of Groovy Plugin's API you can extend in your custom IDE plugin to enable IDE support at design time for the transformations you provide at runtime. So this is unrelated to config.groovy.

b) asking colleague

c) Plugin writer's documentation is available here http://www.jetbrains.org/intellij/sdk/docs/welcome.html

0

b) If the AST transformations plugin is implemented, will that feed into the debugger as well? because the IDE now knows the extra code that was inserted?

No. The `AstTransformationSupport` extension point allows to teach IntelliJ that some method or field is present on some class.
For example: giving info that `foo` method is available on `Bar` class will make all `foo` references in Groovy/Java/Kotlin code resolvable, so they will not be highlighted as errors. The extension doesn't work with method bodies, and there is no place to even a place breakpoint, because there are no method bodies in the code. 

0
Avatar
Permanently deleted user

Hello Daniil,

Thank you for your response. Let me rephrase my example to see what I can achieve with the transformation support plugin

@AddExtraParam
def func1() {
someRandomFunction()
}

def func2() {
func1()
}

is converted to

@AddExtraParam
def func1(MyCustomInjectedParam _custom) {
injectCodeToSomethingWith(_custom)
someRandomFunction()
}

def func2() {
func2(new MyCustomInjectedParam())
}

This will compile fine with my ASTTransformation for AddExtraParam. But now if I set a breakpoint in intellij inside func1, it will not hit because the signature has changed. However if I place my breakpoint inside func2, it will hit and everything will work as expected as far as debugging goes

The question I have is this

a) If I write an ASTTransformation plugin to tell the IDE to add a function defined as def func1(MyCustomInjectedParam _custom) in addition to the original function, will that dupe the debugger to respect my breakpoint? I will obviously have to copy all code in func1 to the modified param func1

b) Will the presence of multiple functions mess with the debug experience? 

c) Is there a different approach to this, ideally I just want to tell IDEA that my breakpoint is at a different spot.

 

0

Please sign in to leave a comment.