Custom Groovy DSL support
Join the DZone community and get the full member experience.
Join For FreeThis time I have a little surprise up in my sleeve. An experimental feature which we would like to get some feedback on from you. Look carefully at the IntelliJ IDEA Community Edition screenshot below.
Â
What you see is me editing Groovy code with some sort of currency DSL in it. The code completion dialog is visible, showing me the options ... WAIT a moment! The dialog is proposing currencies as valid properties to numbers! Currencies like eur or usd are being suggested! And once typed, valid currencies are no longer underlined as unresolvable, while invalid currency symbols will still get their underline.
What? Have we taught IntelliJ IDEA the world's currencies? No way, it's just me leveraging one of the many new Groovy features available in Maia, the next version of IntelliJ IDEA, available currently under EAP.
IntelliJ IDEA 9 will allow you to describe your custom DSLs with a Groovy script. The script having the .gdsl extension then needs to be put on project's classpath (by you or a library) so that IDEA can see it. The following simple script did the whole magic for the currencies.
def ctx1 = context(ctype: "java.lang.Number")
contributor(ctx1) {
property name: "eur", type: "test.Money"
property name: "usd", type: "test.Money"
property name: "chf", type: "test.Money"
property name: "rur", type: "test.Money"
}
IDEA picks up the file and starts recognizing the mentioned properties in your code right after you save the gdsl file.
Methods
It's all not only about properties. Methods can be added as well. For example, a quite handy enhancement of the ReentrantLock class with a withLock() method to safely lock and unlock the reentrant lock before and after use, which can be defined like this:
ReentrantLock.metaClass.withLock = {nestedCode ->
delegate.lock()
try {
nestedCode()
} finally { delegate.unlock() }
}
will be recognized by IDEA, if you add an extra .gdsl script file to your project.
def ctx2 = context(ctype: "java.util.concurrent.locks.ReentrantLock")
contributor(ctx2) {
method name: 'withLock', type: 'void', params: [closure: { } ]
}
Now the withLock() method is recognized and properly auto-completed for you.
Generic methods
Since the .gdsl files are normal Groovy scripts, using iterations you can quickly build up whole families of dynamic methods to feed IDEA with.
def ctx3 = context(ctype: "demo.introduction.domain.Company")
contributor(ctx3) {
['Senior', 'Expert', 'Junior'].each {
method name: "findAll${it}Employees", type: 'java.util.Collection'
}
}
Now you get support for dynamic methods with quite generic names as well. And you can easily spot a typo, for example. Can you see the misspelled method name?
Libraries may also come with their .gdsl files bundled and so you get DSL code-assistance out-of-the-box.
Conclusion
This whole feature is still experimental and may change before the final IntelliJ IDEA 9 release, but we wanted to let you know to stimulate feedback early enough. Please, read more at the JetBrains wiki or on mrhaki's blog. Whatever ideas you have about the feature, please, let us know, so we could tune it exactly for your needs.
Enjoy Groovy coding.
Opinions expressed by DZone contributors are their own.
Comments