如何用于IntelliJ Live模板内部? [英] How to use for inside IntelliJ Live Template?
问题描述
我正在尝试编写一个连接字符串的简单Groovy脚本.该Groovy脚本旨在用作IntelliJ Live模板中的变量并生成一些代码.
I am trying to write a simple Groovy script which concatenates strings. This Groovy script is intended to be used as a variable inside an IntelliJ Live Template and generate some code.
当我将 $ variableResolvedWithGroovyScript $ 设置为以下值时:
When I set the $variableResolvedWithGroovyScript$ to have the following value:
groovyScript("def elements=[\"firstElem\",\"secondElem\",\"thirdElem\"];
String result=\"\";
for (String element : elements) {
result=result+element;
}
return result;"
, clipboard())
我遇到以下错误:
startup failed:
Script1.groovy: 1: expecting EOF, found 'return' @ line 1, column 183.
lt+element; } return res
^
1 error
但是,如果我尝试删除括号( { 和 } ),则无需任何操作问题:
However, if I try and remove the for braces({ and }) it works without any problems:
groovyScript("def elements=[\"firstElem\",\"secondElem\",\"thirdElem\"];
String result=\"\";
for (String element : elements)
result=result+element;
return result;"
, clipboard())
如果我使用上述脚本并在Groovy Console中运行它,则可以正常工作,因此我认为这是Live Template问题.我试图以与引号转义的方式相同的方式来使花括号转义,但是没有任何运气.
If I take above script and run it inside a Groovy Console it works without problems, so I assume this is a Live Template issue. I tried to escape the braces in the same way the quotation marks are escaped, but without any luck.
如何在实时模板中编写for(包含多个指令)?
How can I write a for(containing more than one instruction) inside of a Live Template ?
推荐答案
我设法通过从磁盘加载Groovy脚本来使其工作.因此,不是 $ variableResolvedWithGroovyScript $ 的值,是:
I managed to make it work by loading the Groovy script from the disk. So instead of the $variableResolvedWithGroovyScript$'s value, which was:
groovyScript("def elements=[\"firstElem\",\"secondElem\",\"thirdElem\"];
String result=\"\";
for (String element : elements) {
result=result+element;
}
return result;"
, clipboard())
我只是简单地使用:
groovyScript("d:\\Some\\Path\\Concatenate.groovy" , clipboard())
d:\ Some \ Path \ Concatenate.groovy使用花括号,我还可以定义新方法,等等.
The d:\Some\Path\Concatenate.groovy worked with braces and I was also able to define new methods and so on.
这篇关于如何用于IntelliJ Live模板内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!