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.
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.
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.
If the current method or class declaration is not visible, you can view it in the tooltip by pressing Alt+Q .
Try creating live template with cursor at % types, and fill them after using this template
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With