AppCode 2019.2 Help

Creating live templates

The following example procedure illustrates how to create a template for a TODO comment with the current date and user name:

  1. In the Preferences dialog (⌘,), go to Editor | Live Templates.

  2. Select the template group where you want to create a new live template. If you do not select a template group, the live template is added to the user group.

  3. Click Add The Add icon and select Live Template.

  4. Specify the following values:

    Field

    Description

    Example value

    Abbreviation

    Specify the characters to expand the template

    todo

    Description

    Specify an optional description to identify what the template is for

    Insert TODO comment with the current date and username

    Template text

    Specify the body of the template with variables

    //TODO $DATE$ $USER$: $END$
  5. Click Define at the bottom to select the applicable contexts (for example, Objective-C if it should apply to Objective-C source files).

    Define context for a new live template

  6. Click Edit Variables and select the date() function for $DATE$ and user() for $USER$.

    Edit template variables
    Also select Skip if defined for both variables and click OK. If necessary, change the order of variables in the list using the Up button andthe Down button. This order determines the order in which the cursor will jump between the variables in the expanded template. For example, if you move the $todo$ variable down, the cursor will jump to its input field after the $project$ input field is completed. Click OK to close the dialog.

  7. Click OK to apply the changes.

Depending on the current system date and username, the newly created todo template will expand to a line which is similar to the following:

//TODO 2019-07-02 jsmith:

Create a surround live template

To create a surround template, you need to use the $SELECTION$ predefined variable that indicates the code fragment to be wrapped.

  1. In the Preferences dialog (⌘,), go to Editor | Live Templates.

  2. Select the template group where you want to create a new live template.

  3. Click Add The Add icon and select Live Template.

  4. Specify the following values:

    field

    Description

    Example value

    Abbreviation

    Specify the characters to expand the template

    ifel

    Description

    Specify an optional description to identify what the template is for

    Surround with if/else

    Template text

    Specify the body of the template with variables

    if $SELECTION$ { $IF_EXPR$ }else{ $ELSE_EXPR$ } $END$
  5. Click Define at the bottom to select the applicable contexts.

  6. Click OK to apply the changes.

  7. To surround your code with the newly created template, press⌥⌘J, and select the template from the list:

If you do not see the added template in the list that opens on pressing⌥⌘J, make sure that you have correctly defined your context.

Create a new template from a fragment of code

  1. In the editor, select the text fragment to create a live template from.

  2. Select Tools | Save as Live Template from the main menu. The list of the live templates opens. In this list, the newly created template has been added to the user group.

  3. Specify an abbreviation for the template, an optional description (to identify what the template is for) and modify the template body. If the template has variables defined, click Edit Variables to configure them.

  4. Click OK to apply the changes.

Copy an existing template

If you want to reuse the same template in multiple groups, or you want to create a new template based on another one, you can duplicate an existing template.

  1. On the Editor | Live Templates page of the Preferences dialog (⌘,), select the template which you want to copy.

  2. Click Duplicate The Duplicate button on the toolbar. A new template item is added to the same group as the original, and selected.

  3. Specify a new abbreviation for the template, an optional description (to identify what the template is for), and modify the template body if necessary. If the template has variables defined, click Edit Variables to configure them.

  4. Click OK to apply the changes.

Last modified: 10 October 2019