Stas Shamilov Stas Shamilov - 6 days ago 6
Android Question

How to get method parameters types in live templates in Intellij IDEA?

I want to create a live template for Timber logger similarly to default live template logm . It uses a Groovy script to collect method parameters and separate them by commas. For example:

public int func(int a, float b, Object c, String d) {
logm
}


generate the following code:

public int func(int a, float b, Object c, String d) {
Log.d(TAG, "func() called with: a = [" + a + "], b = [" + b + "], c = [" + c + "], d = [" + d + "]");
}


Parameters are collecting by the following code:

def params = _2.collect {it + ' = [" + ' + it + ' + "]'}.join(', ');
return '"' + _1 + '() called' + (params.empty ? '' : ' with: ' + params) + '"'

//Where _1 and _2 - default IDEA methods, in this case
//_1 - methodName(), which eturns the name of the embracing method (where the template is expanded).
//_2 - methodParameters(), which returns the list of parameters of the embracing method (where the template is expanded).


The problem is the Timber methods require type format for parameters, for example:

int a = 5;
String s = new String("test");
boolean b = false;

Timber.d("%d, %s, %b", a, s, b);


Thus, i need to determine the types of method parameters.

Answer

Try creating live template with cursor at % types, and fill them after using this template

Comments