- Shortcuts and settings
- Template types
- Adding custom templates
- Surround With templates
- Live Templates scripting
Shortcuts and settings
Surround with Live Template
Insert Live Template
Expand Live Template
Save as a Live Template
In fact, Live Templates are similar to Xcode snippets with one important exception: by using simple predefined variables and constructions, you can trigger various automated actions for template placeholders, such as type suggesting, copying the entered value, automatic copying of entered expression in the snippet, and others.
There are two types of live templates:
Parameterized templates, which contain plain text and/or variables that enable user input. You can invoke the full list of such templates depending on the code context via ⌘J, or by typing the template abbreviation and using Tab to make it do coding for you:
Surround With templates, which allow you to surround selected text with some expression (⌥⌘T):
Adding custom templates
Let’s check how can we create a custom
sout template which will print values in the following form in Swift:
print (“expression = \(expression)”)
Go to. Here you’ll find a list of predefined Live Templates grouped by language. Select the Swift group and click the Add (+) button or press ⌘N:
Enter an abbreviation (the short name by which we are going to invoke this template) and a description:
Select the context where this template should be used:
- Let’s add the template text. For our template we need to specify the following: Note that you can simply select the text in the editor, invoke , and quickly get a template stub with the selected text.
$EXPR$is a template variable. Template variables in live templates are what enables user input. After a template is expanded, variables appear in the editor as input fields. Variables are declared as
$VARIABLE_NAME$, and here you can specify any VARIABLE_NAME you want. They are defined by expressions which can also accept default values.
Two predefined live template variables are supported:
$END$(position of the cursor after template is expanded and all values are entered).
$SELECTION$(for surround templates this is the code fragment to be wrapped).
You can edit each variable by selecting Edit variables:
Finally, our template will insert the snippet, accept value for the expression, and duplicate it in the interpolated string. To duplicate the variable value into another input field, just specify its name omitting
$signs in the Expression field. After pressing ⏎, it will move the cursor to the end of line as specified by the
$END$variable: Note, that you can jump between template inputs in the same way as you do between method parameters, by using ⇥/⇧⇥.
Surround With templates
Let’s see how we can create a similar template for surrounding our code block with
Here we need to add the following text and use the
$IF_EXPR$and other variables allow us to specify conditional statements. Let’s save the template and see it in action:
Live Templates scripting
For each variable specified in the template, you can use a number of built-in functions to script the created live templates. This is how we can use them:
To apply the built-in function, select it in the Expression field - see example in
alloctemplate for Objective-C where
className()function allows to guess the class name needed based on variable type:
You can also apply the built-in function to any variable value in your template. For example, we can force the camelCase based on type for Swift using the copy of type name with
camelCase()function applied: Here we use modified
lettemplate: with the following variables:
Finally, to specify the default value for any variable, just enter it in the Default value field with quotes: