Skip to content

Live template for CREATOR

Bradley Campbell edited this page Nov 27, 2016 · 6 revisions

Important: all information in this on this page is only relevant to PaperParcel 1.x. For information on the latest version of PaperParcel, visit http://grandstaish.github.io/paperparcel/

While it's nice that you don't have to write the implementation of the CREATOR field, it is still a little bit of boilerplate that you need to add it yourself. This is the perfect case for a Live Template.

Steps:

  1. Open Preferences... -> Editor -> Live Templates

  2. Click '+' under 'Android'

  3. Under 'Abbreviation', type the keyword you want to use to create the CREATOR (e.g. 'creator')

  4. Under 'Description', type a message to help you remember what this command does (e.g. 'Create a PaperParcelable Creator instance')

  5. Type the following code into the template text:

    companion object {
       @JvmField val CREATOR = PaperParcelable.Creator($CLASS_NAME$::class.java)
    }
  6. Define the 'Applicable contexts' to 'Kotlin -> Class'

  7. Click 'Edit Variables'

  8. Under 'Expression' for CLASS_NAME, type kotlinClassName()

  9. Apply changes

Now you can simply type your keyword in your data class, press the Tab key, and you'll get an auto-complete box for class names so you can quickly type in the class name.

Clone this wiki locally