Using live templates in IntelliJ

If you need to write repeated text in IntelliJ then you can use its live templating function to help you. Suppose I’m writing a Liquibase script that will be composed of many similar changesets:

<changeSet id="MapSalvageCategoryAToMIAFTR" author="proctorh">
    <preConditions onFail="MARK_RAN">
        <and>
            <sqlCheck expectedResult="0">SELECT COUNT(*) FROM EXTERNAL_SYSTEM_MAPPED_PROP WHERE REF_DATA_TABLE =
                'rd_salvage_category' AND REF_DATA_CODE='CATEGORYA' AND EXTERNAL_SYSTEM_ID = (SELECT ID FROM
                RD_EXTERNAL_SYSTEM WHERE CODE='MIAFTR')
            </sqlCheck>
        </and>
    </preConditions>
    <sql>INSERT INTO EXTERNAL_SYSTEM_MAPPED_PROP       (EXTERNAL_SYSTEM_ID,REF_DATA_TABLE,REF_DATA_CODE,EXT_SYS_PROPERTY_VALUE) (SELECT
        ID,'RD_SALVAGE_CATEGORY','CATEGORYA','A' FROM RD_EXTERNAL_SYSTEM WHERE CODE='MIAFTR')
    </sql>
</changeSet>

I want to repeat these inserts, but with different values for the ref data code, and what it is being mapped to.

  • Select the code and go to Tools -> Save as live template.
  • Choose the abbreviation for the template.
  • Edit the template to insert variables where required.
<changeSet id="MapSalvageCategoryAToMIAFTR" author="proctorh">
    <preConditions onFail="MARK_RAN">
        <and>
            <sqlCheck expectedResult="0">SELECT COUNT(*) FROM EXTERNAL_SYSTEM_MAPPED_PROP WHERE REF_DATA_TABLE =
                'rd_salvage_category' AND REF_DATA_CODE='$refdata$' AND EXTERNAL_SYSTEM_ID = (SELECT ID FROM
                RD_EXTERNAL_SYSTEM WHERE CODE='MIAFTR')
            </sqlCheck>
        </and>
    </preConditions>
    <sql>INSERT INTO EXTERNAL_SYSTEM_MAPPED_PROP
        (EXTERNAL_SYSTEM_ID,REF_DATA_TABLE,REF_DATA_CODE,EXT_SYS_PROPERTY_VALUE) (SELECT
        ID,'RD_SALVAGE_CATEGORY','$refdata$','$value$' FROM RD_EXTERNAL_SYSTEM WHERE CODE='MIAFTR')
    </sql>
</changeSet>

Now you can repeat the code block by doing:

  • Ctrl / Command + J to bring up the Insert template menu. The carat will be positioned on the first variable. Type the variable value, then press return to go to the next variable.

This entry was posted in IntelliJ and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

HTML tags are not allowed.

517,762 Spambots Blocked by Simple Comments