GDSL Awesomeness – AutoComplete Script for grails-Mail-Plugin “sendMail” closure
Hi,
I have been using the Grails mail plugin for a while now, this plugin injects a method named “sendMail” to all controllers and services.
Our aim will be to provide auto-completes for this method and the closure inside.
The GDSL Script is as follows:
[java]
import com.intellij.patterns.PsiJavaPatterns
import com.intellij.patterns.PlatformPatterns
//Let Us first Contribute the "sendMail" method.
def ctx = context(
ctype: PsiJavaPatterns.psiClass().withName(PlatformPatterns.string().matches(/.*/))
)
contributor(ctx) {
def path = ""
try {
path = psiClass.containingFile.originalFile.virtualFile.path
} catch (Exception e) {
}
//Check for controllers and Services
if (path =~ ".*/*grails-app/controllers/.*" || path =~ ".*/*grails-app/services/.*") {
method(name: ‘sendMail’, type:’void’, params: [closure: {}])
}
}
//Now we will contribute methods to the closure passed to "sendMail"
def ctx2 = context(scope: closureScope())
contributor(ctx2) {
def call = enclosingCall("sendMail")
if (call) {
method name: ‘to’, type:’void’, params: [recepients: "java.lang.String…"]
method name: ‘cc’, type:’void’, params: [recepients: "java.lang.String…"]
method name: ‘bcc’, type:’void’, params: [recepients: "java.lang.String…"]
method name: ‘body’, type:’void’, params: [body: "java.lang.String"]
method name: ‘html’, type:’void’, params: [html: "java.lang.String"]
//And so on all methods defined here
}
}
[/java]
Image illustrating auto-complete
Read Further in the “GDSL AWESOMENESS” Series
- GDSL Awesomeness – Introduction to GDSL in IntelliJ Idea
- GDSL Awesomeness – Understanding Context And Contributors in Detail
- GDSL Awesomeness – Defining dynamic property in a class
- GDSL Awesomeness – Defining dynamic method in a class
- GDSL Awesomeness – Adding Hinting for missingMethod implementation
- GDSL Awesomeness – Setting a global context
- GDSL Awesomeness – Delegating Closure Calls
- GDSL Awesomeness – Defining methods in a closure passed to method of particular name
- GDSL Awesomeness – Defining Properties in a closure passed to method of particular name
- GDSL Awesomeness – Contributing to classes which match a path/name pattern
- GDSL Awesomeness – contributing methods with current class return types or parameters
- GDSL Awesomeness – AutoComplete Script for grails-Mail-Plugin “sendMail” closure
- GDSL Awesomeness – Getting Code of a method or Field in GDSL Script
- GDSL Awesomeness – Advanced Example showing domain properties implementation
Hope It Helped.
Kushal Likhi