As professional developers we are constantly looking for productive tips to save our precious time. IntelliJ IDEA comes with many tricks and shortcuts. One of them are Live Templates.
Live templates are predefined code fragments, which allow us to code faster and finally to become more productive. We can create our custom live templates and edit existing.
They are stored in the following location:
- Windows:
<your home directory>\.<product name><version number>\config\templates - Linux:
~\.<product name><version number>\config\templates - MacOS:
~/Library/Preferences/<product name><version number>/templates
To create new live templates we need to launch Settings and open Live Templates page. Then we can select template group or create new one. I chose second option and called my template group: “Android”.
Next add new template and fill fields as below. I decided for following naming convention: first letter – “l” (from Log), second letter – “d,e,i,w” (from Log type), third letter if exists – “t” (from TAG, if it is defined).
- Abbreviation: ld
- Desription: Sends DEBUG log message
- Template text:
1Log.d("$TAG$", "$MSG$"); - Applicable in Java: statement
I also added another template for logs with defined TAG as static (abbreviation with “t” suffix, e.g. ldt, let):
- Abbreviation: ldt
- Desription: Sends DEBUG log message
- Template text:
1Log.d(TAG, "$MSG$"); - Applicable in Java: statement
Same convention I used for ERROR logs:
- Abbreviation: le
- Desription: Sends ERROR log message
- Template text:
1Log.e("$TAG$", "$MSG$"); - Applicable in Java: statement
and with static TAG:
- Abbreviation: let
- Desription: Sends ERROR log message
- Template text:
1Log.e(TAG, "$MSG$"); - Applicable in Java: statement
Finally it should look as below:
In code it works perfectly:
I am also attaching Android.xml file from Live Templates directory so you can faster add templates I created above. I was a bit lazy, but you can add relevant templates for other log types (e.g. info, verbose, warn).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?xml version="1.0" encoding="UTF-8"?> <templateSet group="Android"> <template name="ld" value="Log.d("$TAG$", "$MSG$");" description="Sends DEBUG log message" toReformat="false" toShortenFQNames="true"> <variable name="TAG" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="MSG" expression="" defaultValue="" alwaysStopAt="true" /> <context> <option name="JAVA_CODE" value="false" /> <option name="JAVA_STATEMENT" value="true" /> <option name="JAVA_EXPRESSION" value="false" /> <option name="JAVA_DECLARATION" value="false" /> <option name="JAVA_COMMENT" value="false" /> <option name="JAVA_STRING" value="false" /> <option name="COMPLETION" value="false" /> </context> </template> <template name="ldt" value="Log.d(TAG, "$MSG$");" description="Sends DEBUG log message with TAG" toReformat="false" toShortenFQNames="true"> <variable name="MSG" expression="" defaultValue="" alwaysStopAt="true" /> <context> <option name="JAVA_CODE" value="false" /> <option name="JAVA_STATEMENT" value="true" /> <option name="JAVA_EXPRESSION" value="false" /> <option name="JAVA_DECLARATION" value="false" /> <option name="JAVA_COMMENT" value="false" /> <option name="JAVA_STRING" value="false" /> <option name="COMPLETION" value="false" /> </context> </template> <template name="le" value="Log.e("$TAG$", "$MSG$");" description="Sends ERROR log message" toReformat="false" toShortenFQNames="true"> <variable name="TAG" expression="" defaultValue="" alwaysStopAt="true" /> <variable name="MSG" expression="" defaultValue="" alwaysStopAt="true" /> <context> <option name="JAVA_CODE" value="false" /> <option name="JAVA_STATEMENT" value="true" /> <option name="JAVA_EXPRESSION" value="false" /> <option name="JAVA_DECLARATION" value="false" /> <option name="JAVA_COMMENT" value="false" /> <option name="JAVA_STRING" value="false" /> <option name="COMPLETION" value="false" /> </context> </template> <template name="let" value="Log.e(TAG, "$MSG$");" description="Sends ERROR log message with TAG" toReformat="false" toShortenFQNames="true"> <variable name="MSG" expression="" defaultValue="" alwaysStopAt="true" /> <context> <option name="JAVA_CODE" value="false" /> <option name="JAVA_STATEMENT" value="true" /> <option name="JAVA_EXPRESSION" value="false" /> <option name="JAVA_DECLARATION" value="false" /> <option name="JAVA_COMMENT" value="false" /> <option name="JAVA_STRING" value="false" /> <option name="COMPLETION" value="false" /> </context> </template> </templateSet> |
More about Live Templates you can find here.