如何将变量传递给Intellij IDEA实时模板groovy脚本中的groovy代码? [英] How to pass variable to groovy code in Intellij IDEA live templates groovy script?
问题描述
在我的 Intellij
IDEA实时模板中有一个 groovyScript
,如下所示:
I have a groovyScript
in my Intellij
IDEA live template, like this :
groovyScript("D:/test.groovy","", v1)
在我的 D:/test.groovy
我有这样的代码:
on my D:/test.groovy
i have a code like this:
if ( v1 == 'abc') {
'abc'
}
现在我想将v1变量传递给test.groovy,任何人都可以帮助我如何做到这一点?
Now I want to pass v1 variable into test.groovy ,can any one help me how can I do this?
推荐答案
为了举例说明,我制作了一个活动模板,用当前类和当前方法打印注释。
For exemplification purposes I made a live template which is printing a comment with the current class and current method.
是我的实时模板的定义:
This is how my live template is defined:
这是我编辑 variableResolvedWithGroovyScript 变量的方式:
And here is how I edited the variableResolvedWithGroovyScript variable:
给定变量的表达式具有以下值:
The Expression for the given variable has the follwing value:
groovyScript("return \"// Current Class:\" + _1 + \". Current Method:\"+ _2 ", className(),methodName())
在这种情况下,_1(它在groovy脚本中的作用类似于变量)取第一个参数的值作为类名,而_2取值为
第二个参数的值,该参数是方法名。如果需要另一个参数,_3将在groovy脚本中用于引用给定的参数。
As you can see, in this case the _1(which acts like a variable in the groovy script) takes the value of the first parameter which is the class name, and the _2 takes the value of the second parameter which is the method name. If another parameter is needed the _3 will be used in the groovy script to reference the given parameter.
这篇关于如何将变量传递给Intellij IDEA实时模板groovy脚本中的groovy代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!