Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idea Live Template to Log Method Args

I would like to be able to create a live template in Jetbrain's Idea that will log the method's arguments. Let's call it "larg". It would work like:

public void get(String one, String two) {
    larg<tab>

to create

public void get(String one, String two) {
    log.info("get: one = " + one + " two = " + two);

I'm fine with getting the method name in, but have not figured out how to pull in the method arguments. Any ideas?

like image 430
Tim Avatar asked Sep 17 '09 18:09

Tim


People also ask

What is a live template?

Use live templates to insert common constructs into your code, such as loops, conditions, various declarations, or print statements. To expand a code snippet, type the corresponding template abbreviation and press Tab . Keep pressing Tab to jump from one variable in the template to the next one.

How do I use code templates in IntelliJ?

Press Ctrl+Alt+S to open the IDE settings and select Editor | File and Code Templates. and specify the template name, file extension, name of the resulting file, and body of the template. Apply the changes and close the dialog.


1 Answers

I'm 4 years late, but the predefined template soutp pretty much does this using a groovyscript variable.

Here's the groovy script that does what you're looking for

groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())
like image 145
Rob Avatar answered Oct 21 '22 05:10

Rob