Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live Template not working in Kotlin

Hope you have worked with Live Templates which is given by Android by default.

Like:

Generate debug log statement: "logd"+TAB Generate error log statement: "loge"+TAB Generate info log statement: "logi"+TAB Generate TAG declaration: "logt"+TAB Generate parameter logging: "logm"+TAB Generate method return log: "logr"+TAB 

enter image description here

Which it is not available in KOTLIN?

Is it not available in Android Studio 3.0 Canary Version?

like image 371
Pratik Butani Avatar asked Jun 06 '17 04:06

Pratik Butani


People also ask

How to use live template?

Live templates 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 import a live template into IntelliJ?

Import live template configurationChoose File | Manage IDE Settings | Import Settings from the menu. Specify the path to the archive with the exported live template configuration. In the Import Settings dialog, select the Live templates checkbox and click OK.

How do I create a live template in IntelliJ?

Press Ctrl+Alt+S to open the IDE settings and select Editor | Live Templates. Select the template group where you want to create a new live template (for example, other). If you do not select a template group, the live template will be added to the user group. and select Live Template.


2 Answers

Here is the step-by-step guide:

Firstly, Copy and paste AndroidLog templates to Kotlin (Just select them and use CMD+C, CMD+V (or Ctrl+C, Ctrl+V)
Secondly, You have to adjust them manually:

  1. logd (loge, logv and others) Select the logd item and press "Edit variables" enter image description here

Change expression to: kotlinMethodName() enter image description here

Also, remove ; from the end of the template, as you don't need it in Kotlin.

Now your method name will be shown correctly

  1. logt

This one is a bit trickier.
Solution 1: TAG = class name.

Template text :

private val TAG = "$className$" 

Edit variables -> Expression:

groovyScript("_1.take(Math.min(23, _1.length()));", kotlinClassName()) 

Solution 2: TAG = file name (can be used inside Companion)

Template text :

private const val TAG = "$className$" 

or:

companion object {     private const val TAG = "$className$" } 

Edit variables -> Expression:

groovyScript("_1.take(Math.min(23, _1.length()));", fileNameWithoutExtension()) 
like image 125
Leonid Ustenko Avatar answered Sep 20 '22 05:09

Leonid Ustenko


Yet not added log template in Kotlin Live templates section in Android Studio.

Settings -> Editor -> Live Templates -> Kotlin for kotlin templates.

kotlin templates

Settings -> Editor -> Live Templates -> AndroidLog for AndroidLog templates

AndroidLog  templates.

So you can't get the same AndroidLog templates in Kotlin code.

So now Question is How to use same Log Functions using templates in Kotlin?

Ans: You can add same Log templates (AndroidLog Templates) in Kotlin Templates section in Android studio as below example.

Kotlin Templates section

Then It will be available in your Kotlin code!

Kotlin loge

I hope in this way you can get an advantage of Log functions templates in Kotlin.

like image 43
pRaNaY Avatar answered Sep 22 '22 05:09

pRaNaY