Better DSL Support for Groovy-Eclipse

36
© 2011 SpringSource, A division of VMware. All rights reserved What’s new in Groovy & Grails Support? DSL Support in Groovy-Eclipse Andrew Eisenberg, SpringSource Tools Team

description

Here are the slides from my presentation at GR8Conf Europe 2011. I showed Groovy-Eclipse's new DSLD feature that improved DSL support in the editor.

Transcript of Better DSL Support for Groovy-Eclipse

Page 1: Better DSL Support for Groovy-Eclipse

© 2011 SpringSource, A division of VMware. All rights reserved

What’s new in Groovy & Grails Support? DSL Support in Groovy-Eclipse Andrew Eisenberg, SpringSource Tools Team

Page 2: Better DSL Support for Groovy-Eclipse

© 2011 SpringSource, A division of VMware. All rights reserved

What’s new in Groovy & Grails Support?

Part 1

Page 3: Better DSL Support for Groovy-Eclipse

3 3

New Groovy-Eclipse Support in 2.5.0

  DSL Descriptors (discussed later)  Groovy 1.8   Parameter guessing content assist   Script outline view   Distinguish read vs. write access in search   Conditional breakpoints in Groovy files (STS only)

Page 4: Better DSL Support for Groovy-Eclipse

4 4

New Grails tooling support in STS 2.7.0.M1

  Service field content assist  Groovy search results in GSPs

Page 5: Better DSL Support for Groovy-Eclipse

© 2011 SpringSource, A division of VMware. All rights reserved

Gradle support in STS

Part 1.5

Page 6: Better DSL Support for Groovy-Eclipse

6 6

First cut at Gradle support now available in STS

  Demoed this morning in the Gradle talk   Yay!

Page 7: Better DSL Support for Groovy-Eclipse

© 2011 SpringSource, A division of VMware. All rights reserved

DSL Support in Groovy-Eclipse

Part 2

Page 8: Better DSL Support for Groovy-Eclipse

8 8

The Problem

Page 9: Better DSL Support for Groovy-Eclipse

9 9

A DSL for distance calculations

3.m + 2.yd + 2.mi - 1.km

In the Groovy editor:

Uh oh!

Can we do better???

Page 10: Better DSL Support for Groovy-Eclipse

10 10

The Solution: DSL Descriptors (DSLDs)

Page 11: Better DSL Support for Groovy-Eclipse

11 CONFIDENTIAL

DSL Descriptors

  Teach the IDE about DSLs through scripting

Page 12: Better DSL Support for Groovy-Eclipse

12 12

DSL Descriptors

  In English: •  “Any subtype of Number should have the following properties: m, yd, mi, km”

  In DSLD: •  “Any subtype of Number”:

currentType( subType( Number ) )!

•  “…the following properties…”: [ “m”, “yd”, “cm”, “mi”, “km” ].each {!

property name:it, type:"Distance”!

}!

3.m + 2.yd + 2.mi - 1.km

Page 13: Better DSL Support for Groovy-Eclipse

13 13

Let’s see that

  Ex 1: Distances

Page 14: Better DSL Support for Groovy-Eclipse

14 14

Anatomy of a DSLD file

Page 15: Better DSL Support for Groovy-Eclipse

15 15

DSLD and the Inferencing Engine

  Hooks into Groovy-Eclipse’s type inferencing engine •  Visit each expression AST node

• Determine type using previous expression • Move to next expression

  DSLD operates on Groovy AST Expression nodes •  Exposes Groovy AST nodes and uses Groovy API

  In the background, while typing (reconciling) org.codehaus.groovy.ast.expr.Expression!

Page 16: Better DSL Support for Groovy-Eclipse

16 16

Pointcuts and Contribution blocks

  Pointcuts: • Where to do it.

• What is the current expression? • Declaring type?

•  Enclosing class?

  Contribution blocks: • What to do

•  “Add” method

•  “Add” property •  (not at runtime, in the editor only)

class Other {! def x!}!class Foo {! def method() {! new Other().x! }!}!

Page 17: Better DSL Support for Groovy-Eclipse

17 17

What goes in a Contribution Block?

  property : “adds” a property •  arguments

•  name: “blarb” •  type: “java.lang.String” •  declaringType :”com.foo.Frumble” •  isStatic: false •  doc: “Some html” •  provider: “My DSL”

 method : “adds” a method •  all arguments above, and

•  params: [firstName:“java.lang.String”, lastName:“java.lang.String”] •  useNamedArgs: true

  name is required, others optional

(…).accept {! property name: “myName”! method name: “getMyName”!}!

Page 18: Better DSL Support for Groovy-Eclipse

18 18

Pointcuts

 currentType() : Matches on the current declaring type  enclosingClass() : Matches on the enclosing class  currentType(“com.bar.Foo”)  methods(“run”)  annotatedBy(“org.junit.runner.RunWith”)

 enclosingClass( annotatedBy(“org.junit.runner.RunWith”) ) & currentType(methods(“run”) )

Page 19: Better DSL Support for Groovy-Eclipse

19 19

Where does this pointcut match? What does it add?

(enclosingClass(annotatedBy(“org.junit.runner.RunWith”)) & currentType(methods(“run”))).accept { property name:”blarb” }!

@RunWith(Sumthin) !class Foo {! def someTest() {! print “Hello”! def x = new MyRunner()! x.blarb! }!}!

class MyRunner { def run() {…} }!

enclosed by @RunWith

has run method

blarb

Looking for an expression that:

Page 20: Better DSL Support for Groovy-Eclipse

20 20

Wait…isn’t this Aspect-Oriented Programming?

  Pointcut •  Intentionally borrowed from AOP

  AspectJ: pointcuts and advice •  operates on Java instructions at runtime

  DSLD: pointcuts and contribution blocks •  operates on AST org.codehaus.groovy.ast.expr.*!

  Join Point Model •  Join points (e.g., instructions, expressions)

• Mechanism for quantifying join points (e.g., pointcuts)

• Means of affect at a join point (e.g., advice, contribution blocks)

class Foo {! def x = {! def i = 0!

i++! }!}!

Page 21: Better DSL Support for Groovy-Eclipse

21 21

Other things to help with DSLDs

Page 22: Better DSL Support for Groovy-Eclipse

22 22

New DSLD Wizard

  File New Groovy DSL Descriptor

Page 23: Better DSL Support for Groovy-Eclipse

23 23

DSLD Preferences Page

Page 24: Better DSL Support for Groovy-Eclipse

24 24

Groovy Event Console

  Keep this open while implementing DSLDs: •  Shows exceptions

•  Pointcuts • Matches

  Find the Console here:

Page 25: Better DSL Support for Groovy-Eclipse

25 25

Groovy AST Viewer

  Exploration of AST of current Groovy file

Page 26: Better DSL Support for Groovy-Eclipse

26 26

Examples

Page 27: Better DSL Support for Groovy-Eclipse

27 27

Basic Script Ex 2: Using the DSLD wizard

Page 28: Better DSL Support for Groovy-Eclipse

28 28

Meta DSL Ex 3: DSLD script for editing DSLDs

Page 29: Better DSL Support for Groovy-Eclipse

29 29

AST Transforms Ex 3: Standard AST Transforms

Page 30: Better DSL Support for Groovy-Eclipse

30 30

SwingBuilder Ex 4: Long script, but simple to understand

Page 31: Better DSL Support for Groovy-Eclipse

31 31

Grails Constraints DSL Ex 5: encapsulate Grails domain knowledge in a script

Page 32: Better DSL Support for Groovy-Eclipse

32 32

Criteria Queries Ex 6: simple script, large effect

Page 33: Better DSL Support for Groovy-Eclipse

33 33

Griffon Ex 7: from last night

Page 34: Better DSL Support for Groovy-Eclipse

34 34

What’s next?

Page 35: Better DSL Support for Groovy-Eclipse

35 35

DSLD is not complete

 Guided by user feedback   Collaboration with library developers

• Grails, Gaelyk, Griffon, etc.

  Release standard DSLDs (AST Transforms, Builders, etc.) • With Groovy-Eclipse? • With Groovy core?

 More pointcuts • What do users need that isn’t implemented? •  regex(), instanceof(), superType(), enclosingEnum(), etc.

 What about IntelliJ’s GDSL?

Page 36: Better DSL Support for Groovy-Eclipse

36 36

Thanks!

 More information: • Google: groovy eclipse dsld

  Full documentation on Codehaus.org: •  http://docs.codehaus.org/display/GROOVY/DSL+Descriptors+for+Groovy-Eclipse

  Install from update site: •  http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.6/

 Mailing list: •  [email protected]

 Or chat with me whenever