Skip to content

Instantly share code, notes, and snippets.

@jonfhancock
Last active December 10, 2020 01:35
Show Gist options
  • Save jonfhancock/5c2d52e60f3038fe900b331fd474dea9 to your computer and use it in GitHub Desktop.
Save jonfhancock/5c2d52e60f3038fe900b331fd474dea9 to your computer and use it in GitHub Desktop.
Android Studio live templates. Apply them following directions here: https://www.jetbrains.com/help/idea/sharing-live-templates.html
<templateSet group="KotlinShared">
<template name="krequire" value="requireNotNull($VAR$) {&#10; &quot;Required value '$VAR$' in $CLASS_NAME$.$FUNCTION_NAME$() was null.&quot;&#10;}&#10;$END$" description="Require Not Null with a helpful print statement" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="kotlinVariable()" defaultValue="" alwaysStopAt="true" />
<variable name="CLASS_NAME" expression="kotlinClassName()" defaultValue="" alwaysStopAt="false" />
<variable name="FUNCTION_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<context>
<option name="KOTLIN" value="true" />
</context>
</template>
</templateSet>
<templateSet group="TimberLogKotlin">
<template name="tlogd" value="timber.log.Timber.d(&quot;$KOTLIN_FUNCTION_NAME$: $content$&quot;)" description="Timber.d(String)" toReformat="true" toShortenFQNames="true">
<variable name="KOTLIN_FUNCTION_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="tloge" value="timber.log.Timber.e($exception$, &quot;$KOTLIN_FUNCTION_NAME$: $content$&quot;)" description="Timber.e(Exception, String)" toReformat="true" toShortenFQNames="true">
<variable name="exception" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="KOTLIN_FUNCTION_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="tlogi" value="timber.log.Timber.i(&quot;$KOTLIN_FUNCTION_NAME$: $content$&quot;)" description="Timber.i(String)" toReformat="true" toShortenFQNames="true">
<variable name="KOTLIN_FUNCTION_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="tlogm" value="timber.log.Timber.d($content$)" description="Log method name and its arguments" toReformat="true" toShortenFQNames="true">
<variable name="content" expression="groovyScript(&quot;def paramNames = _2.collect {it + ' = [%s]'}.join(', '); def paramValues = _2.collect {it}.join(', '); return '\&quot;' + _1 + '() called' + (paramNames.empty ? '' : ' with: ' + paramNames) + '\&quot;'+(paramValues.empty ? '' : ', ' + paramValues) &quot;, kotlinFunctionName(), functionParameters())" defaultValue="" alwaysStopAt="false" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="tlogr" value="timber.log.Timber.d(&quot;$KOTLIN_FUNCTION_NAME$() returned: %s&quot;, $result$)" description="Log result of this method" toReformat="true" toShortenFQNames="true">
<variable name="KOTLIN_FUNCTION_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="result" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="tlogw" value="timber.log.Timber.w($exception$, &quot;$KOTLIN_FUNCTION_NAME$: $content$&quot;)" description="Timber.w(Exception, String)" toReformat="true" toShortenFQNames="true">
<variable name="exception" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="KOTLIN_FUNCTION_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="twtf" value="timber.log.Timber.wtf($exception$, &quot;$KOTLIN_FUNCTION_NAME$: $content$&quot;)" description="Timber.wtf(Exception, String)" toReformat="true" toShortenFQNames="true">
<variable name="exception" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="KOTLIN_FUNCTION_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
</templateSet>
@jonfhancock
Copy link
Author

The first one is intended to address the problem expressed here: https://twitter.com/AdamMc331/status/1331662669712158723?s=20

When used inside a function in a class, the live template will prefill the class and function names.
Demo 1

When used in a top level function, the cursor is placed so that you can type something for the class name (or not).
When used in a class, but outside a function (e.g. property assignment), the template will place the cursor so that you can type something for the function name (or not).
Demo 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment