Václav is a programming enthusiast who's constantly seeking ways to make development more effective and enjoyable. He's particularly interested in server-side Java technologies, distributed systems, concurrency, agile methodologies, modern programming languages and DSLs. He works for JetBrains as a senior software developer and a technology evangelist. He is also a board member of the JetBrains Academy. On the side, he's leading the GPars project, an opensource concurrency library, and investigates the domains of neural networks, evolutionary programming and data mining. You can check out his blog or follow him on twitter. [dzone] Václav is a DZone Zone Leader and has posted 45 posts at DZone. View Full User Profile

Custom Groovy DSL support

11.11.2009
| 13365 views |
  • submit to reddit

This 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.

 

Published at DZone with permission of its author, Václav Pech.

Comments

Kode Ninja replied on Thu, 2009/11/12 - 12:46am

Very, very cool :). Vaclav, this looks really interesting. I wish something like this could be done in Eclipse/NetBeans too.
Good to know that this feature'll be available in the community edition too!

Gr8 work. IDEA rocks, as always!
-Kodeninja

Dierk Koenig replied on Thu, 2009/11/12 - 10:36am

Awesome fricken cool !!!!!

 Please make sure that this experimental feature makes it into the finals!

 You can charge me twice the regular price for that one! - Really, no joke.

 cheers

Dierk

Leonard Axelsson replied on Fri, 2009/11/13 - 3:02am

That is really nice, well done! Looking forward to giving it a try.
To quote Dierk: "Please make sure that this experimental feature makes it into the finals!"

/Leo

Chao Liu replied on Fri, 2009/11/13 - 7:49am

Just try it out. this is really coooooooooooool!

Burgen Slift replied on Mon, 2010/11/15 - 4:14pm

ve been playing with it for a bit now, and I did have a question about how plugin dependencies are added to the plugin iml file. Feels like a bit of a scam. The first plugin I added seemed to show up there wihtout problems, but others didn't pickup; is it a manual process?

Hushcat Hunny replied on Fri, 2011/08/26 - 7:47pm

I am happy when reading your blog with updated information! thanks alot and hope that you will post more site that are related to this site. Rebar

Aster Beatrix replied on Sat, 2012/02/25 - 10:03am

Its my very first experience to get info regarding this specific DSL support. I am yet not able to find more detailed info via internet regarding this specific content. So i would love to ask for more from your side. On the other side, i am also working for ferrari most expensive ferrari

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.