Templates
Templates provide a convenient method to insert a pre-defined block of text into a program.
The following table describes the template options on the Preferences → IDL → Templates preferences page:
|
Option
|
Description
|
|---|---|
| Template List |
This list displays the templates currently loaded in the IDL Workbench. When you select a template in the list, the template contents are displayed in the Preview box. If the value in the Auto Insert column is "on", the template is automatically inserted if there is an exact content assist match. |
| New/Edit |
Creates a new template, or edits a template selected from the list. Refer to Creating or Modifying a Template for more information. |
| Remove |
Removes a selected template from the list. |
| Restore Removed |
Restores all the current templates removed from the list. |
| Revert to Default |
If you altered a template, this option restores the original version of the selected template. |
| Import |
Imports an external .xml file containing IDL Workbench templates. A template file can be created using the Export button. |
| Export |
Exports one or more selected templates into an external .xml file. These files can later be reimported with the Import button. |
| Preview |
The Preview box shows the contents of a selected template. |
| Restore Defaults |
Restores the Templates page to its initial configuration. |
| Apply |
Applies all the current preference changes made to the Templates page. |
Creating or Modifying a Template
In addition to using preexisting templates, you can create your own code templates and modify them later.
To create a new template or modify an existing template:
- On the Preferences → IDL → Templates page, click New or select a template from the Template list and click Edit.
- Enter a template name in the Name box, or modify the existing name.
- Select Automatically insert to ensure that templates that exactly match a content assist request are automatically inserted into the editor.
- Describe the template in the Description box.
- Enter or modify the template text in the Pattern box.
- Click Insert Variable to insert a predefined variable into the template.
- Click OK.
The New Template or Edit Template dialog appears.
Note
The Automatically insert option is only useful for templates that do not include a line or word selection.
You can insert variables into your template, which allow you to customize a template upon insertion. Variable names follow the form:
${variable_name}
Once a template is inserted in the editor, the variable sections of the template are active, and the variable text can be changed.
Refer to Predefined Variables for a description of the predefined template variables.
If you changed the name of the existing template, you are presented with the option of either renaming the template, or creating another template with the new name.
Predefined Variables
The following table describes the predefined variables available when constructing a template.
Inserting a Template into an Editor
If a template contains a word selection or a line selection variable, you must select the appropriate text prior to inserting the template. Selecting text prior to invoking the content assist menu also means that you must explicitly choose the template from the list (automatic template insertion is not an option).
To insert a template into your code that does not include a line or word selection:
- Type the name (or part of a name) of a template in the editor.
- Press Ctrl-Space to display the list of content assist items.
- Select a template from the list to display the template's content.
- Double-click a template in the list (or press Enter) to insert the template into the editor.
- If the template contains user-defined variables, the keyboard focus is on the first variable. Edit the variable value, and press Tab to highlight the next variable (if one exists).
- After the last variable, if the template contains a
${cursor}variable,pressing Tab places the keyboard focus at the${cursor}position. Enter additional code at this position.
Templates have the
icon.
If the template was configured to automatically insert itself, and if the name of the template does not match any other content assist item, the template is inserted automatically into the editor.
To insert a template into your code that includes a line or word selection:
- In the editor, select the desired text.
- Press Ctrl-Space to display the list of content assist items.
- Select a template from the list to display the template's content.
- Double-click a template in the list (or press Enter) to insert the template into the editor.
- If the template contains user-defined variables, the keyboard focus is on the first variable. Edit the variable value, and press Tab to highlight the next variable (if one exists).
- After the last variable, if the template contains a
${cursor}variable,pressing Tab places the keyboard focus at the${cursor}position. Enter additional code at this position.
Templates have the
icon.
Inserting a Template into the Command Line View
A template that consists of a single line can be inserted into the Command Line view as well as into an editor.
For example, suppose you find yourself wanting to select a data file with the extension .dat from a specific directory quite often. You could define a one-line template similar to the following, giving it a short name like df:
Then when you want to select a data file, simply type df at in the Command Line view and press Ctrl-Space. Assuming no other template begins with the characters df, the template is inserted into the Command Line with the initial file highlighted, allowing you to quickly change the variable name. Pressing Return brings up the file selection dialog in the specified directory, with the file list filtered for .dat files.