Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 1000
Stanislav Shamilov Avatar asked Nov 27 '16 15:11

Stanislav Shamilov


People also ask

How do I show parameters in IntelliJ?

View parameter hints in the editorOpen the Settings/Preferences dialog ( Ctrl+Alt+S ) and go to Editor | Inlay Hints | <required language>. Select Parameter hints from the list, make sure the Show parameter hints checkbox is selected, and then specify the context where you want parameter hints shown.

How do I list all methods in IntelliJ?

By default, IntelliJ IDEA shows all classes, methods, and other elements of the current file. To toggle the elements you want to show, click the corresponding buttons on the Structure tool window toolbar. to show class fields. to have protected class members shown in the tree.

Where is method declaration in IntelliJ?

If the current method or class declaration is not visible, you can view it in the tooltip by pressing Alt+Q .


1 Answers

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

like image 55
mr. Nutscracker Avatar answered Sep 20 '22 10:09

mr. Nutscracker