How to debug groovy script using in live template?
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?
Please sign in to leave a comment.
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.
Try changing the script to
I'd also recommend escaping back slashes in the script path:
thanks for your answer@Elena Pogorelova,I can not reply directly,so i pasted your answer link:
I got a new error using script in your anwer for the method "sum":
by the way,is there a way to debug the script directly?
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())
Here is my own live template config file:
although no errors aftering using your script,the result is not correct.The test method and results as follow:
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?
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
Thank you so much,you saved me.I will try it later by another IDE.