How to debug groovy script using in live template?

Answered

I use live template variables in my groovy script,but i got some errors.i can not get any ideas in google.Could anyone give me some help?

Thanks so much

--------------------------------------------------------------

update:

i am new to groovy,i want to generate parameters comments like "@param [paramemter name] [parameter type] " for a method by live template.The predefined function "methodParameters()" can not do this,so i want run the custom grooy script  by predefined function "groovyScript".

The custom script named "test.groovy"  as follow:

def methodParameters=_1
def methodParameterTypes=_2
def result='';
def params=methodParameters.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();
def type=methodParameterTypes.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();
for(i = 0; i < params.size(); i++) {
result+='* @param '+ params[i] + ' ' + type[i] + ((i < params.size() - 1) ? '\\n ' : '')
};
return result

and I call this this script by inline function "groovyScript" like this:

groovyScript("D:\project\groovyDemo\src\test.groovy", methodParameters(),methodParameterTypes())

but I got the error message as follow:

No signature of method: java.util.ArrayList.replaceAll() is applicable for argument types: (java.lang.String, java.lang.String) values: [[\\[|\\]|\\s], ]
Possible solutions: replaceAll(java.util.function.UnaryOperator)

i can not find any ideas to debug this groovy script using in live template of Idea.Can anyone give me some advice?

1
7 comments

Hi - you can refer to this help page for general information on live template variables and a list of functions used to define them.

If it doesn't help, please share the actual script and the error that you are getting.

1

Try changing the script to

def methodParameters="${_1}"
def methodParameterTypes="${_2}"
def result='';
def params=methodParameters.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();
def type=methodParameterTypes.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();
for(i = 0; i < params.size(); i++) {
result+='* @param '+ params[i] + ' ' + type[i] + ((i < params.size() - 1) ? '\\n ' : '')
};
return result

I'd also recommend escaping back slashes in the script path:

groovyScript("D:\\project\\groovyDemo\\src\\test.groovy", methodParameters(),methodParameterTypes())
0

thanks for your answer@Elena Pogorelova,I can not reply directly,so i pasted your answer link:

https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010804299/comments/360003220499 

 I got a new error using script in your anwer for the method "sum":

/**
* @param [a null\n * @param b] null
* @return int
* @date 2021/6/8 22:57
* @since 0.0.1
*/
private static int sum(int a, int b) {
return a + b;
}

by the way,is there a way to debug the script directly?

0

Not sure where null comes from... Could you share your live template (<IDEA config dir>\templates\user.xml or whatever it's called)?

As for the output format, I'm not sure how to fix it, IDEA dumps the resultant array as is when loading a script by file path for some reason... As a workaround, I can only suggest embedding a script into groovyScript() call. Looks ugly, but works as expected:

groovyScript("def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();def type=\"${_2}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();for(i = 0; i < params.size(); i++) {result+='* @param ' + params[i] + ' ' + type[i] + ((i < params.size() - 1) ? '\\n ' : '')}; return result", methodParameters() ,methodParameterTypes())

0

Here is my own live template config file:

<templateSet group="personalTemplate">
<template name="**m" value="**&#10; * @author Monica&#10; $params$&#10; * @return $returns$ &#10; * @date $date$ $time$&#10; * @since 0.0.1&#10; */$END$" shortcut="ENTER" description="method doc" toReformat="false" toShortenFQNames="true">
<variable name="params" expression="groovyScript(&quot;def result=''; def params=\&quot;${_1}\&quot;.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();def type=\&quot;${_2}\&quot;.replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList();for(i = 0; i &lt; params.size(); i++) {result+='* @param ' + params[i] + ' ' + type[i] + ((i &lt; params.size() - 1) ? '\\n ' : '')}; return result&quot;, methodParameters() ,methodParameterTypes())" defaultValue="" alwaysStopAt="false" />
<variable name="returns" expression="methodReturnType()" defaultValue="" alwaysStopAt="false" />
<variable name="date" expression="date()" defaultValue="" alwaysStopAt="false" />
<variable name="time" expression="time()" defaultValue="" alwaysStopAt="false" />
<context>
<option name="JAVA_CODE" value="true" />
</context>
</template>
</templateSet>

although no errors aftering using your script,the result is not correct.The test method and results as follow:

/**
* @author Monica
* @param a null
* @param b null
* @return int
* @date 2021/6/9 20:08
* @since 0.0.1
*/
private static int sum(int a, int b) {
return a + b;
}

And could you tell me how you fix errors when scripts produce them?I asked forJET BRAINS support for help,but he told me I can not debug scripts directly,I should simulating the scripts to fix errors.How to simulate?Just  using hot key to insert templates as the way I usually use?

0

Your template works fine for me:

 

There is no way to debug the scripts, you can only modify it and check the result by applying a template

1

Thank you so much,you saved me.I will try it later by another IDE.

0

Please sign in to leave a comment.