Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Write less with Intellij Live Templates
Mahendran
Mahendran

Posted on • Originally published atmahendranv.github.io on

     

Write less with Intellij Live Templates

LiveTemplate is a feature in Intellij based IDEs where you can expand a code snippet by typing an abbreviation and the IDE guide your cursor through the parts that need your attention (a variable name).

Intellij has a complete set of live templates at your disposal. Few of them you might’ve been using it in day-to-day basis. Check this list blow.

  1. sout
  2. logi
  3. soutf
  4. soutm
  5. appNs
  6. psvm and goes on…

If you haven’t used them before, for items 1-4 open a kotlin file ⇨ type the abbreviation and press tab to expand.

Within five minutes, you can also write a template.


Know your template

Template is special kind of macro that allows you to enter your input in between / place cursor for you. It is located in Preferences ⇨ Editor ⇨ Live templates section. Let’s have a look atsoutf and analyze what cooks the live template.


// soutf : Prints current class and function name to System.outprintln("$CLASS$.$METHOD$")
Enter fullscreen modeExit fullscreen mode

It’s just a one-liner where class and method wrapped inside$. IDE reads thesee variables and fill in when expanding it.


Cursor context

One major thing to design a better template isCursor context. It tells the IDE, when is a Live template is applicable. Expanding a kotlin code inside axml file is totally useless.

Forsoutf, the context is set to work inside Kotlin files, and it can expand inside a function. Try to expand it outside a function. Check the below templates to know various contexts.

abbreviationscope
maintop-level
soutstatement
fun0class
ifnexpression

The Best way understand the scope is to expand all these templates in their destined place in code. That’s all about the context, on to the fun part where we edit thesout template.


Placing cursor in template

The sout template is great, it autofills classname and function name for us. But it places cursor outside the quotes after expanding it. Let’s fix it. Add an $END$ to the template and try it now.

println("$CLASS$.$METHOD$ $END$")
Enter fullscreen modeExit fullscreen mode

That’s a bit about scopes and cursor. Let’s create our live template for LiveData.


What is LiveData?

An android class that connects UI to the Presenter/VieweModel.

If you’re not familiar with LiveData, think of it as an Observable. Any consumer class can attach to it and get updates when the underlying value change. It has two variants, LiveData(observe only model) and MutableLiveData(Superset of LiveData — where you can publish values).

Let me draw out the target code and inline why each line is needed.

// A mutable backing property which is set to private.privateval_email=MutableLiveData<String>()// Frontface to field, the observers will be able to read// but, cannot modifyvalemailLiveData:LiveData<String>=_email// Syntactic sugar — to validate the data in later part// of the application — use thisprivatevalemail:String?get()=emailLiveData.value// A setter method that updates our mutable property// write code to trigger validation in here. This is why// we don't expose the mutable version of livedata outside.funupdateEmail(value:String?){_email.value=value// start your validation flow here}
Enter fullscreen modeExit fullscreen mode

Scanning though the live template window in IDE, you might’ve already figured out how to duplicate/create new live template. Ours will be calledld. I’ll use the same name going further. Notice thatld is scoped toKotlin — Class.

Live Template for LiveData


Placeholders

Insout, we didn’t need user input on anything as cursor context can give us the function and class names. That’s not the case withld (LD), here we need two inputs from the user. Property name$PROP_NAME$ and Type$PROP_TYPE$ — rest can be filled by live template.

private val _$PROP_NAME$ = MutableLiveData<$PROP_TYPE$>()val $PROP_NAME$LiveData : LiveData<$PROP_TYPE$> = _$PROP_NAME$private val $PROP_NAME$: $PROP_TYPE$?        get() = $PROP_NAME$LiveData.valuefun update$PROP_NAME_IN_FUNCTION$(value: $PROP_TYPE$?) {  _$PROP_NAME$.value = value}
Enter fullscreen modeExit fullscreen mode

By mentioning these placeholders in different places, we tell the IDE to fill-in the variable wherever we it inside the template. So, when you type email for thePROP_NAME, it is filled in for LiveData, MutableLiveData, in the field so on. Same forPROP_TYPE.

We have another placeholder calledPROP_NAME_IN_FUNCTION, which is a capitalized version of ourPROP_NAME. So, that’s about template content, now we have to tell the IDE, what each placeholder is.

Click onEdit Variables and input the values as in below table.

NameExpression
PROP_NAME
PROP_TYPEclassName()
PROP_NAME_IN_FUNCTIONcapitalize(PROP_NAME)

The property name is left to our choice, there is not much IDE can do about it. When filling in PROP_TYPE, IDE will suggest classes. AndPROP_NAME_IN_FUNCTION is a derived property that capitalizes thePROP_NAME(email ⇨ Email). Now we have a live template setup for future use. Create as many as you want and focus on what matters the most.

Happy coding ⌨️.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Android / Kotlin / Python / KMP
  • Location
    India
  • Work
    Senior android developer at Omnissa
  • Joined

More fromMahendran

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp