Special Code Template Syntax
You can use code templates to speed up the entry of commonly used
sequences of reserved words and common code patterns. For example,
if you enter forc and press the Tab key, it expands into the
following:
for (Iterator it = collection.iterator(); it.hasNext();) {
Object elem = (Object) it.next();
}
When you create code templates, there are several constructs
that you can use to customize the way the code template behaves.
You can look at the default IDE code
templates in the Options window, under the Tools
menu, for examples from which you can learn.
In general, a code template parameter can be specified by its name and a
set of optional hints. Hints serve as guidance when
computing the values assigned by the infrastructure to the parameters on template expansion.
Syntax for parameter definition is as follows:
${param_name hint=value hint=value ...}
However, boolean hints that can be written without the value part:
${param_name hint} translates to ${param_name hint=true}
Some parameter names are reserved by the code template infrastructure:
- ${cursor} defines the position where
the caret will be located after the editing of the code template values finishes.
- ${selection} defines the position for pasting the content of
the editor selection, which is used by the 'selection templates'
that appear as hints whenever the user selects text in the editor.
Similarly, some of the hint names are reserved by the code template infrastructure:
- ${param_name default="value"} defines the parameter's default value.
- ${param_name editable=false} can be used to disable user's editing of the parameter.
- ${param_name instanceof="java.util.Collection"} requires the parameter value to be an instance of the given type.
- ${param_name array} requires the parameter value to be of an array type (including arrays of primitive data types).
- ${param_name iterable} requires the parameter value to be of an array type or an instance of "java.lang.Iterable". Can
be used in 'for-each' cycles.
- ${param type="java.util.Iterator"} requires the parameter value to be the given type. The infrastructure will try to use
short name Iterator and import java.util.Iterator if possible.
- ${param_name iterableElementType} requires the parameter value to be the type of the iterable element. Can be used in
'for-each' cycles.
- ${param_name leftSideType} requires the parameter value to be the type of the expression on the assignment's left side.
- ${param_name rightSideType} requires the parameter value to be the type of the expression on the assignment's right side.
- ${param_name cast} defines that the parameter value would be a type cast if necessary.
- ${param_name newVarName} defines that the parameter value should be a 'fresh' unused variable name in the given context.
Legal Notices