RightEdge - The Ultimate Backtesting and Trading System Development Platform

Code Snippets

A code snippet is a fragment of code that you can store for reuse within the code editor at a later time.  Microsoft introduced an open standard for code snippets starting with Visual Studio 2005.  The editor honors this format.  RightEdge ships with a number of code snippets that are contained in the AppData\Snippets directory.  There are code snippets for both C# and Visual Basic.


When a code snippet is activated, it inserts its text into the editor.  If there are fields declared in the code snippet, they are highlighted within the inserted code.  This easily facilitates naming items that are typically variable but are required to complete the code snippet.  To move around these fields, hit the Tab key.  To indicate that all fields have been named, hit the Enter key.  If more than one field use the same declaration, then the secondary fields are flagged as dependent on the primary field for the declaration.  They automatically update whenever the text in the primary field is updated.


Using Code Snippets


To use a code snippet, begin typing the shortcut within the editor window.  For example, to create a class definition, start by typing the word 'class' onto an empty line followed by the tab key.  The code will be auto-generated and allow you to define your class name.  Code snippets may also be invoked via the Insert Snippet command on the Edit menu or by right clicking in the editor window and selecting Insert Snippet.




The up and down arrow keys also allow for selection of items in the list.  The Tab and Enter keys pick the selected folder or code snippet, while the Escape key cancels the popup.  If a folder is picked, the folder is traversed into and its contents are displayed in the list.  Code snippets are grouped in the list through a directory structure on disk.  The name shown in the list for each snippet indicates the shortcut name that will invoke this snippet when typed into the editor.


In the sample below, prop followed by the Tab key were entered into the editor window to invoke a property snippet.




If a code snippet requires additional items, these fields are highlighted and represent the regions of text that need to be edited to complete the code snippet.  While the caret is in a field, the Tab key can be used to navigate to the next field. Likewise, Shift+Tab navigates to the previous field.


The active field is drawn with a line around it.  Sometimes a field refers to a declaration that is used in multiple places within the code snippet.  In that case, only the first field is designated as editable.  When its value changes, the other fields that are dependent on it automatically update.  The dependent fields are drawn with a dotted line around them.


Creating Code Snippets


As mentioned, RightEdge ships with a number of code snippets that are ready to use.  In some cases it may be desirable to create your own code snippets.  The snippets follow the format put forth by Microsoft.


A code snippet is simple an XML-formatted file on the file system that contains a .snippet extension.  These are placed in RightEdge's AppData\Snippets folder underneath the corresponding folder (C# and VB.NET).  The code snippets that ship with RightEdge can be examined and changed with any text editor.  The full schema definition can be found in the Visual Studio documentation.


The example below is a quick analysis of the basic nodes within a code snippet file.  This snippet is the contents of the current close snippet.


<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

 <CodeSnippet Format="1.0.0">


                 <Title>Current Close</Title>


                 <Description>Code snippet for current close value</Description>

                 <Author>Yye Software</Author>



                 <Code Language="csharp"><![CDATA[Close.CurrentValue]]>






Each code snippet starts with a <CodeSnippets> tag and the <CodeSnippet> itself is contained within.  The first four lines and the last three lines of the file should never need modification.


The <Header> blocks contain information about the code snippet.  The title of the code snippet is a descriptive name.  The shortcut is used to allow the developer to quickly invoke this code snippet.


The <Snippet> tag itself contains the meat of the snippet definition.  The language is specified in the <Code> tag followed by a CDATA element that contains the actual code that is inserted.  This simple example takes no parameters.


Once you have a firm understanding of this example, consider opening some of the more complicated snippets, especially those taking parameters to realize the full power of code snippets.


Note: The editor window loads the code snippets when it is opened.  If you modify or create a code snippet, simply close all editor window instances and load them again.