Code snippets

Code snippets are saved pieces of code that make it easier for you to implement complicated or recurring code. The Code Snippets window is used to manage code snippets. In addition, you can insert snippets into templates and functions from this window.

By default, the Code Snippets window is hidden on the right hand side of the Tricentis TDM Studio window and only visible as a tab. To display the window, you can hover the mouse over the tab or you can select View->Code Snippets from the menu.

Code snippets are divided into two main categories:

  • Predefined Code Snippets: These are the code snippets that were copied into the Tricentis TDM Studio installation directory during the installation of the product. Predefined code snippets cannot be deleted or edited. You can only change their order in the group.

  • My Code Snippets: This is where the code snippets that you define are saved. These code snippets can be edited, renamed, moved and deleted.

Code snippets can only be used for functions (.cs files) and templates (.tpl files). To create a new code snippet or to apply an existing one, you have to open the respective file in an edit window.

You create a new code snippet by highlighting the text that you want to use for the snippet in an Editor window and then selecting New Code Snippet from the Editor's context menu. This opens the Code Snippet Properties window in which you can add additional information about the code snippet. Some of the fields in this dialog are optional, but you must specify a group under which the code snippet will appear in the tree structure of the Code Snippets window.

Managing code snippets

Code snippets and code snippet groups can be copied, moved or deleted using the mouse or the context menu for the Code Snippets window. You can also copy and paste code snippets or even entire code snippet groups via the clipboard.

Tricentis TDM Studio remembers the order of the groups and of the code snippets in the groups from one session to the next.

The context menu also contains options for viewing and for editing an individual code snippet.

Using code snippets

Code snippets can be inserted into an Editor window in two different ways:

  • By double-clicking on the code snippet. The code snippet appears at the cursor position in the Editor window. If a section of text was selected in the Editor, this is replaced by the snippet.

  • By dragging and dropping the code snippet into the Editor window. The code is inserted where you release the mouse button. Any selected text in the Editor is not affected by this operation.

Code snippets can not be inserted everywhere. For example, there are code snippets that contain TDL code for templates. These snippets can only be inserted into templates and into project and task variables. Code snippets that contain function code (C#) can only be inserted in a Function Editor window.

Context menu

The context menu for the Code Snippets window contains the following menu options:

Option

Description

Copy

Copies the selected node (a code snippet or a code snippet group) to the clipboard.

Paste

Pastes the code snippet/group from the clipboard into the tree structure.

Delete

Deletes the selected code snippet or code snippet group. This action is permanent and can not be reversed. You therefore have to confirm the deletion before it is made final.

Rename...

If a group or code snippet is selected, you can rename it.

Move up

Moves the selected element one step upwards. You cannot use this option to move a code snippet out of a group.

Move down

Moves the selected element one step downwards. You cannot use this option to move a code snippet out of a group.

Properties

Opens the Code Snippet Properties dialog that displays the properties of the selected snippet. The properties cannot be edited at predefined elements.