IDEA9 的超酷新DSL支持

原帖:http://groovy.dzone.com/articles/custom-groovy-dsl-support

 

 

作者想秀一下IDEA9的新功能,并取得一些反馈。

上图就是在IDEA9中写groovy代码时写下数字加点的代码提示,竟然是币种提示。这是怎么出来的呢?

 

只要在项目classpath下加入后缀为gdsl的groovy文件,就可以实现这种效果。比如把以下代码存入一个.gdsl:

 

 

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"
}

 

就实现了上面数字后的币种代码提示。

 

方法提示

 

除了上面例子里的property提示,还可以加入方法提示。

 

比如我们给ReentrantLock注入一个withLock的方法,该方法接受一个闭包,在运行闭包内代码前加锁,运行完后解锁。

 

 

ReentrantLock.metaClass.withLock = {nestedCode ->
    delegate.lock()
    try {
        nestedCode()
    } finally { delegate.unlock() }
}

 在.gdsl文佳内加入以下代码

 

def ctx2 = context(ctype: "java.util.concurrent.locks.ReentrantLock")

contributor(ctx2) {
    method name: 'withLock', type: 'void', params: [closure: { } ]
}

 在IDEA9中就会出现代码提示和自动补全:

 

这个新功能还在试验中,想了解更多的话请去 JetBrains wiki 以及 mrhaki's blog. 如果大家有什么想法的,联系作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值