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.
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())
I get below error:
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())
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.
How can I write a for(containing more than one instruction) inside of a Live Template ?
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())
I simply used:
groovyScript("d:\\Some\\Path\\Concatenate.groovy" , clipboard())
The d:\Some\Path\Concatenate.groovy worked with braces and I was also able to define new methods and so on.