Code Snippets
Code Snippets
Code Snippets are small chunks of commonly used source code that you can insert into your source files. For example, you can use a snippet to insert a "for" or "while" loop. Snippets are intended for boiler plate text.
The Snippets panel contains all your code snippets and helps to organize them. The Snippets panel lets you create, edit, delete, and insert code snippets. You can also import and export snippets.
Snippets can also be programming language-specific, or common to all languages, or common to certain sets of languages. For example, you can have a snippet for C/C++ and another with the same name just for
Java. See “Snippet Window” on page 330.
Inserting Snippets
To insert a snippet, you can do any of the following:
•
From the Snippet panel, double-click on a snippet or type the name of snippet and press Enter. You can activate the Snippet panel first so you can type into it by using the Activate Snippet Window command (Ctrl+Alt+S).
•
From the Edit menu, select Insert Snippet > "snippet name".
•
Use the Complete Snippet command (Ctrl+E) to pop up a list of snippets.
•
Start typing a snippet name and the auto-completion list will show the snippet. Note that having snippet names in the auto-completion list is optional.
Text Variables
Snippets may contain text variables (sometimes referred to as placeholders). A text variable is a special identifier that is replaced with a particular value when the snippet is inserted. For example,
$date$
is a text variable that gets replaced with the current date when you insert a snippet containing that text variable.
A text variable starts and ends with a dollar sign ($). For example
$this$.
When you select anywhere within it, the whole thing gets selected. Arrow keys move over text variables in one press. To select inside one, hold down Ctrl and click with the mouse in it.
You can also force Source Insight to process text variables in any selected text by using Expand Text Vari-
ables
. See “Expand Text Variables” on page 197.
Note:
Text variables can also be used in Clips (see Clips).
96 Source Insight 4 User Guide
Code Snippets
Name
$current_function$
$current_namespace$
$current_package$
$current_symbol$
$current_type$
$current_word$
$date$
$dollar$
$enclosing_symbol$
$file$
$file_in_project$
$file_no_path$
$next_function$
$next_namespace$
$next_package$
$next_symbol$
$next_type$
$prev_function$
$prev_namespace$
$prev_package$
$prev_symbol$
$prev_type$
$return_type$
$selection$
$time$
$username$
$UTC_date$
$UTC_time$
$UTC_year$
$year$
Predefined Text Variables
There are many predefined text variables that are automatically replaced with values when a snippet is inserted.
Table 3.5: Predefined Text Variables
Value
name of the current enclosing function name of the current enclosing namespace name of the current enclosing package name of symbol declared or enclosing the cursor position name of the current enclosing struct, class or data type the current word at the cursor position before the snippet is inserted the current local date the dollar sign ($) name of the function, class, or symbol that encloses the cursor current file name with path current file name relative to project source directory (root) current file name without path name of the function declared after the current line name of the namespace declared after the current line name of package declared after the current line name of the symbol declared after the current line name of the type declared after the current line name of the function declared before the current line name of the namespace declared before the current line name of the package declared before the current line name of the symbol declared before the current line name of the type declared before the current line return type of the current function the currently selected text before the snippet is inserted the current local time the system user name that is logged in the current UTC date the current UTC time the current UTC year the current local year
Placeholder Text Variables
You can also use your own text variables to represent placeholders that behave similar to form fields after the snippet is inserted.
Source Insight 4 User Guide 97
Code Snippets
For example, this snippet insert a "for" statement and has placeholders for the iterator variable, the limit value, and the final cursor position.
for ($i$ = 0; $i$ < $lim$; ++$i$)
{
$end$
}
When this snippet is inserted, the first text variable is automatically selected. In this example, that would be the
$i$
variable. You can simply type the name of the variable to replace
$i$
.
Then, you can use the Select Next Snippet Placeholder command (Ctrl+Shift+;) to select the next placeholder. Alternatively, you can use any other navigation command to move the cursor.
Automatic Text Variable Replacements
When you edit a placeholder, all instances of the same text variable placeholder get replaced automatically with what you typed. For example, if
$myvar$
appears in 4 places, and you select it and type over it, then all 4 places are replaced with what you typed. This is useful for snippets that refer to the same identifier in multiple places.
In the above example, let's say you type ioffset
to replace the value of
$i$
. The other instances of
$i$
will get replaced with ioffset
.
To select the next text variable in a file, use the Select Next Snippet Placeholder command (CTRL+SHIFT+;).
This selects the next text variable placeholder in the file. If you have the "Smart Tab" feature enabled, then you can just type Tab in many cases to move to the next placeholder.
In the above example, the
$lim$
placeholder will get selected. Now you can type the limit value of this for loop.
Pressing Ctrl+Shift+; again will select the
$end$
placeholder. Now you can edit the body of the for loop.
Snippet Options
You can control what file types can use snippets. To enable or disable snippet use for each file type, Select
Options > File Type Options
and select or de-select the "Enable code snippets" option. If the code snippets are disabled, then code snippets will not be proposed in the auto-completion list while you edit, and text variables will not have any special behavior in those files.
You can control automatic text variable replacements on a per-snippet basis. In the Snippet panel, select a snippet and click the Edit Snippet button (Ctrl+E). Select or de-select the "Enable text variables when inserting" option as desired.
You can also enable or disable text variables for all snippets in the Snippet Window Options of the Snippet panel. Select or de-select the "Enable text variables when inserting" option as desired.
See “Snippet Window Options” on page 331.
Project vs Global Snippets
Snippets can be project-specific, or common to all projects. The Storage Location of a snippet specifies if the snippet is stored with the project, or global. Global means it is common to all projects. The Snippets panel lists all your snippets, both project and global.
Global snippets which apply to all projects are stored in a file named global.snippets.xml
in the Source
Insight 4.0 documents folder inside the Snippets folder. For example:
C:\Users\John\Documents\Source Insight 4.0\Snippets\global.snippets.xml
Project-specific snippets are stored in a file named
<project-name>.snippets.xml
in your project data directory. For example in a project named "myproject" :
C:\Users\John\Documents\Source Insight 4.0\Projects\myproject\myproject.snippets.xml
For more information: See “Snippet Window” on page 330.
98 Source Insight 4 User Guide