AppCode 2019.3 Help

Live template variables

When you expand a live template abbreviation, its variables either appear as input fields where you can type values or are replaced with values. These may be default values that you can modify or values calculated using functions.

To declare variables within templates, use the following format: $<variable_name>$.

Define each variable using an expression and provide a default value for cases when the expression fails to evaluate.

This expression may contain the following constructs:

  • String constants in double quotes

  • Names of other variables defined in a live template

  • Predefined functions with possible arguments

Configure template variables

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

  2. Select a template where you want to configure variables.

  3. Specify variables in the template text and click Edit variables.

  4. In the Edit Template Variables dialog, you can do the following for each variable:

    • Change the variable name

    • Define the expression using predefined functions

    • Specify the default value for cases when the expression fails to evaluate

    • Specify whether you want to skip the variable when prompting the user for input if the expression evaluated successfully

Predefined template variables

AppCode supports the following predefined live template variables that cannot be modified:

  • $END$ indicates the position of the cursor when the code snippet is complete, and you can no longer press to jump to the next variable.

  • $SELECTION$ is used in surround templates and denotes the code fragment to be wrapped. After the template expands, it wraps the selected text as specified in the template. For example, if you select EXAMPLE in your code and invoke the "$SELECTION$" template via the assigned abbreviation or by pressing ⌥⌘T and selecting the desired template from the list, AppCode will wrap the selection in double quotes as follows: "EXAMPLE".

Predefined functions used in live template variables

The following functions can be used to define live template variables:

Function

Description

blockCommentEnd

Returns the characters that indicate the end of a block comment in the current language context.

camelCase(String)

Converts a string into camelCase. For example, camelCase(my-text-file), camelCase(my text file), and camelCase(my_text_file) all return myTextFile.

capitalize(String)

Capitalizes the first letter of the parameter.

capitalizeAndUnderscore(String)

Capitalizes all the letters of a CamelCase name passed as the parameter, and inserts an underscore between the parts. For example, capitalizeAndUnderscore(FooBar) returns FOO_BAR.

className()

Returns the name of the current class (the class where the template is expanded).

classNameComplete()

This expression substitutes for the class name completion at the variable position.

clipboard()

Returns the contents of the system clipboard.

collectSequences()

Returns the name of the sequence that is declared right before the palce where the template is expanded. For an example, see the for live template in the Swift group. This function is available for Swift only.

commentEnd()

Returns the characters that indicate the end of a comment in the current language context. The return value is empty if line comments are defined in the current language.

commentStart()

Returns the characters that indicate the start of a comment in the current language context. If line comments are defined in the current language, their start indicator is preferable.

complete()

Invokes code completion at the position of the variable.

completeSmart()

Invokes smart type completion at the position of the variable.

concat(expressions...)

Returns a concatenation of all the strings passed to the function as parameters.

date(sDate)

Returns the current system date in the specified format.

Without a parameter, the current date is returned in the default system format.

To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the function date("Y-MM-d, E, H:m") will return the date formatted as 2019 - 07 - 5, Fri, 12:56.

decapitalize(sName)

Replaces the first letter of the parameter with the corresponding lowercase letter.

enum(sCompletionString1,sCompletionString2,...)

Returns a list of comma-separated strings suggested for completion when the template is expanded.

escapeString(sEscapeString)

Escapes the string specified as the parameter.

executionContext()

Returns the name of the current method using the sel_getName(_cmd) function. For an example, see the logm live template in the Objective-C group. The function is available for Objective-C only.

expressionFormatCode()

Returns the string format specifier for the object which is passed as a parameter of this function. For an example, see the logv live template in the Objective-C group. The function is available for Objective-C only.

fastEnumeration()

Returns the names of collections and enumerations available in the current scope. For an example, see the each live template in the Objectibe-C group. The function is available for Objective-C only.

fileName()

Returns the name of the current file with its extension.

fileNameWithoutExtension()

Returns the name of the current file without its extension.

filePath()

Returns the absolute path to the current file.

fileRelativePath()

Returns the current file path relative to the current project. To check what the relative path is for a given file, right-click it and select Copy Reference, or press⌥⇧⌘C.

firstWord(sFirstWord)

Returns the first word of the string passed as the parameter.

guessElementType (<container>)

Makes a guess on the type of elements stored in a java.util.Collection. To make a guess, AppCode tries to find the places where the elements were added to or extracted from the container.

jsSuggestImportedEntityName()

Suggests the name for import statements of the type `import * as $ITEM$ from "$MODULE$"` or `import $ITEM$ from "$MODULE$"` based on the filename.

lineNumber()

Returns the current line number.

lowercaseAndDash(String)

Converts a camelCase string into lower case and inserts n-dashes as separators. For example, lowercaseAndDash(MyExampleName) returns my-example-name.

objectWithFunctions(sFunctionName1,sFunctionName2,...)

Returns the list of visible objects that contain member functions, listed as parameters.

regularExpression(String, Pattern, Replacement)

Find all occurrences of Pattern in a String and replace it with Replacement. You can specify the pattern as a regular expression to find everything that matches it in the string.

rightSideType()

Declares the left-side variable with a type of the right-side expression. It is used in the iterations group to have a single template for generating both raw-type and Generics Collections.

showParameterInfo()

Returns the parameter details when adding a parameter to a function or method.

snakeCase(String)

Converts a string into snake_case. For example, snakeCase(fooBar) returns foo_bar.

spaceSeparated(String)

Converts a string into lowercase and inserts spaces as separators. For example, spaceSeparated(fooBar) returns foo bar.

spacesToUnderscores(sParameterWithSpaces)

Replaces spaces with underscores in the string passed as the parameter. For example, spacesToUnderscores(foo bar) returns foo_bar.

substringBefore(String,Delimiter)

Removes the extension after the specified delimiter and returns only the filename. This is helpful for test file names (for example, substringBefore($FileName$,".") returns component-test in component-test.js).

suggestIndexName()

Suggests the name of an index variable from most commonly used ones: i, j, k, and so on (first one that is not used in the current scope).

suggestVariableName()

Suggests the name for a variable based on the variable type and its initializer expression, according to your code style settings that refer to the variable naming rules.
For example, if it is a variable that holds an element within an iteration, AppCode makes a guess on the most reasonable name, taking into account the name of the container being iterated.

time(sSystemTime)

Returns the current system time in the specified format.

Without a parameter, the current time is returned in the default system format.

To use a different format, provide a parameter according to the SimpleDateFormat specification. For example, the function time("H:m z") will return the timestamp formatted as 13:10 UTC.

underscoresToCamelCase(String)

Replaces underscores with camelCase letters in the string passed as the parameter. For example, underscoresToCamelCase(foo_bar) returns fooBar.

underscoresToSpaces(sParameterWithUnderscores)

Replaces underscores with spaces in the string passed as the parameter. For example, underscoresToSpaces(foo_bar) returns foo bar.

user()

Returns the name of the current user.

variableForIteration()

Returns a variable that is an instance of the object used as a range expression in a loop.

variableForRangeBasedIteration()

Returns a variables that is an instance of the object that contains begin and end functions.

variableOfType(<type>)

Suggests all variables that may be assigned to the type passed as the parameter, for example, variableOfType("NSString"). If you pass an empty string "" as the parameter, the function suggests all variables regardless of their types. For an example, see the logv live template in the Objective-C group. The function is available for Objective-C only.

Last modified: 31 March 2020