Ratpacked: Groovy DSL Code Completion In IntelliJ IDEA
Ratpack applications can be written in Java and Groovy. The Java API is already very clean and on top is a Groovy DSL to work with Ratpack. When we use Groovy we can use the DSL, which allows for more clean code. The Ratpack developers have used the @DelegateTo
annotation in the source code for the DSL definition. The annotation can be used to indicate which class or interface is used as delegate to execute the closure that is passed to the method. And this helps us a lot in the code editor of IntelliJ IDEA, because IDEA uses this information to give us code completion when we use the Groovy DSL in Ratpack. And that makes using the DSL very easy, because we rely on the IDE to give us the supported properties and methods and we make less mistakes.
Let's see this with an example in code. First we create a new Ratpack.groovy
file:
// File: src/ratpack/Ratpack.groovy
import static ratpack.groovy.Groovy.ratpack
ratpack {
handlers {
}
}
We want to add a method in the closure that is passed to the handlers
method. We first type ge and wait for the code completion to come up:
This is great! We get very informative information to complete our DSL. It is nice to see why this work in the source code of Ratpack for the handlers
method:
...
/**
* Registers the closure used to build the handler chain of the application.
*
* @param configurer The configuration closure, delegating to {@link GroovyChain}
*/
void handlers(@DelegatesTo(value = GroovyChain.class, strategy = Closure.DELEGATE_FIRST) Closure configurer);
...
The @DelegatesTo
annotation defines that the delegate of the closure code is the GroovyChain
class. IntelliJ IDEA now knows that GroovyChain
is used and gives us via the code completion the methods and properties of GroovyChain
.
Written with Ratpack 1.0.0.