如何通过变量IntelliJ IDEA的动态模板Groovy代码Groovy脚本?

问题描述 投票:6回答:4

我有一个groovyScriptIntellij IDEA现场模板,就像这样:

groovyScript("D:/test.groovy","", v1)

D:/test.groovy我有这样的代码:

if ( v1 == 'abc') {
    'abc'
}

现在我想V1变量传递到test.groovy,任何一个可以帮助我,我该怎么办呢?

intellij-idea groovy live-templates
4个回答
4
投票

为了举例说明目的,我做它打印与当前类和当前方法的评论的活样板。

这是我的生活模板是如何定义的:

enter image description here

这里是我如何编辑的variableResolvedWithGroovyScript变量:

enter image description here

对于给定的变量表达式有以下值:

groovyScript("return \"// Current Class:\" + _1 + \". Current Method:\"+ _2 ", className(),methodName())

正如可以看到,在这种情况下,_1(其作用类似于在常规脚本变量)开,其是类名的第一个参数的值,并且_2需要其是方法名的第二个参数的值。如果需要其他参数_3将在Groovy脚本被用来引用给定的参数。


2
投票

到groovyScript宏的参数被绑定到名为_1脚本变量,_2等,这也是在groovyScript帮助在Edit Template Variables Dialog / Live Template Variables描述。


0
投票

我找到了解决办法。

我需要计算使用真人模特的限定类名的CRC32

我用这样的:

groovyScript("   
def crc = new java.util.zip.CRC32().with { update _1.bytes; value }; 
return Long.toHexString(crc);  
", qualifiedClassName())

enter image description here

那么结果是

enter image description here


0
投票

基于该documentation,你的变量可以作为_1_2等。请注意,变量不美元符号传递(因而只有V1,而不是$ V1 $)

所以,你的测试脚本应该像

if ( _2 == 'abc') {
    'abc'
}
© www.soinside.com 2019 - 2024. All rights reserved.