IntelliPrompt code snippets provide a way to insert pre-defined fragments of text into the editor. Each code snippet can declare multiple fields of text, and when a code snippet template session is activated in SyntaxEditor, the text is inserted, and the end user can Tab between the fields to edit their values.
Multiple fields can be declared with the same ID. In this scenario, only one of the fields with that ID in a template session will be editable. When that field's value is changed by the end user, the dependent fields (the other non-editable fields with the same ID) will have their value updated as well. This improves end user typing efficiency.
Tip
The XML code snippet file format used is the same as Visual Studio's. This allows for any Visual Studio compatible code snippet to be used in SyntaxEditor.
Opening Selection Sessions
Functionality is included that displays a helpful popup where the end user can type the title of an accessible code snippet to activate it. A completion list that shows the code snippets and child folders at the current folder level is also displayed below the popup and fully supports partial matches and Tab-based selection. The code snippets used to populate the selection session come from the ICode
When a folder is selected in the completion list, the scope of the selection popup changes to that folder and the user can access the code snippets and child folders within it. Pressing Bkspace or clicking the parent folder's link, backs up to the parent folder level.
Programmatic Session Creation
A code snippet selection session can be opened via two different method calls. Both require that a ICode
Both of these helper methods wrap a call to the ICode
Opening Template Sessions
Code snippets are generally inserted into SyntaxEditor and activated into a template session by one of these methods:
- Selection made via a selection session.
- End user types a code snippet's registered shortcut (defined in the source XML file) into SyntaxEditor and presses the Tab key.
- Programmatic template session creation.
Pressing Tab After a Code Snippet Shortcut
Each code snippet has a shortcut that can be typed into the editor. When the Tab key is pressed after the shortcut, an ICode
This mechanism depends on a ICode
Programmatic Session Creation
Assuming that an ICodesnippet
variable via some means, a session can be programmatically opened like this:
Template Sessions at Run-Time
When a template session is opened, the code snippet's text is inserted into the document. Some code snippets indicate a location within their text where the current view's selected text should be inserted. In this scenario, the selected text prior to the session being opened is appropriately indented and injected within the code snippet text.
If one or more fields were declared in the code snippet, then fields will display in the editor. Fields are highlighted replacement regions that 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.
Editable fields are drawn with a background behind them. 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 upon it automatically update. The dependent fields are drawn with a dotted line around them.
Once all the fields have been updated, pressing Enter while in a field completes the code snippet and closes the session. At this point, if the code snippet designated a target offset for the caret to end up at, the caret will move to that offset.
Template Session Event Sink
Any object that implements the ICode
These methods on the event sink receive the event notifications:
Member | Description |
---|---|
Notify |
Notifies when a new declaration in the session's code snippet is activated. |
Notify |
Notifies when a declaration in the session's code snippet is deactivated. |
Notify |
Notifies when the text value of the active declaration in the session's code snippet is changed. |
Notify |
Notifies after a session is closed. |
Notify |
Notifies after a session is opened. |
Notify |
Notifies before a session is opened. |
See the Event Sinks topic for more information on event sinks in general.
Code Snippets
Code snippets are represented by the ICode
Here are the important members of the Code
Member | Description |
---|---|
Author Property | Gets or sets the name of the code snippet author. |
Code |
Gets or sets the character used to describe literals and objects in the code. |
Code |
Gets or sets a CodeSnippetKind indicating the kind of code contained by the snippet. |
Code |
Gets or sets the language in which the snippet's code is written. |
Code |
Gets or sets the code that is inserted when the code snippet is activated. |
Declarations Property | Gets the collection of declarations. |
Description Property | Gets or sets descriptive information about the code snippet contents. |
Help |
Gets or sets a URL that provides more information about the code snippet. |
Imported |
Gets the collection of imported namespaces. |
Keywords Property | Gets the collection of keywords that describe the code snippet. |
References Property | Gets the collection of assembly references. |
Shortcut Property | Gets or sets the shortcut text used to insert the code snippet. |
Snippet |
Gets the types of actions that can be performed by the code snippet. |
Tag Property | Gets or sets the object that contains user-defined data about the object. |
Title Property | Gets or sets the code snippet title. |
Code Snippet Serialization
ICode
Please see the Visual Studio documentation for more information on the XML schema for code snippet files.
Loading a Code Snippet
Code snippets can be loaded from a Stream
via the Code
The return value is an enumerable of ICode
Saving a Code Snippet
Code snippets can be saved to a Stream
via the Code
A parameter array of ICode
Code Snippet Folders and Metadata
The ICode
A folder has a Items collection, where each item is an object of type ICode
Nested folders can be created as well. Each folder has a Folders collection that lists the child folders of the current folder.
A helper Find
Loading a Code Snippet Folder
An ICode
To create and initialize an ICode
This method will recursively examine folders and load any code snippets located that are for the designated language.
Code Snippet Providers
The ICode
Its Rootfalse
for case-insensitive languages.
This code registers a provider on a language using the folder from the previous section's sample:
This code is also required to be called once per application instance since it registers the classification types and related styles for any fields that will be visible during a template session. If it is not called, then field backgrounds and borders might not display.
Once the provider has been registered on a language, it will watch for Tab key presses. If a shortcut for a known code snippet appears before the caret, then that code snippet will be activated into a new template session.
Customizing Code Snippet Providers
Some languages may wish to only allow shortcuts to trigger code snippet template sessions when in certain portions of code. For instance, a language generally doesn't want a code snippet shortcut to trigger when the shortcut is in a comment or a string literal.
This can be achieved by creating a class that inherits CodeTextSnapshotRange.Deleted
. Otherwise, return the result from the base method call.
Code Snippets in Completion Lists
Languages that wish to include code snippet support may wish to list code snippet metadata items in the Ctrl+Space completion lists. The .NET Languages Add-on languages include this feature.
When a code snippet is selected in the completion list, its shortcut is inserted into the editor. Then if the user presses the Tab key next, and assuming a code snippet provider is registered on the language (see above), the related code snippet is inserted.