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

I want to create a live template for Timer logger similar to the default live logm template. It uses a Groovy script to collect method parameters and separate them with commas. For instance:

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 collected using 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 that Timber methods require a 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.

+6
source share
1 answer

Try to create a live template with a cursor in% types and fill them after using this template

+3
source

Source: https://habr.com/ru/post/1012687/


All Articles