You can watch this video tutorial for more information:
Visual Studio currently offers support for simple template based snippets. This is a great feature for simple snippets of code, however these templates do not contain any advanced logic or access to rich meta-data. For simple snippets such as creating a simple property shell without any logic, we still recommend using the Visual Studio snippet. |
The first step in creating an ActiveSnippet is by simply creating a CodeSmith Generator Template. In this example, we'll create a CodeSmith Generator Template that doubles as an ActiveSnippet and is able to generate properties for a given TableSchema in C#.
This template can be found in Template Explorer under the ActiveSnippets\CSharp or ActiveSnippets\VisualBasic folders. |
For more information on creating a new template please take a look at the following tutorial. |
The required output needs to look like this for every column in a table.
private int _orderId; public int OrderId { get { return _orderId; } set { _orderId = value; } } |
When first creating a template, think about the requirements that we defined above and focus and write the template in small steps. This keeps you from getting overwhelmed by writing everything at once.Next, try and think about how you would write the template by outlining some steps in pseudocode. This will make writing the template much easier. Here are a few steps that we used to create this template:
Finally, ensure that the template compiles and runs from Template Editor or Template Explorer.
The next step is to launch Visual Studio, and explore the options available for using ActiveSnippets within Visual Studio. This will help you get a feel for the integration capabilities for using your ActiveSnippets.
To access various ActiveSnippet features, you can use keyboard shortcuts or use the ActiveSnippet menu items. These are located in the Generator submenu on the right hand side of the Visual Studio menu bar. Once the Generator menu is expanded, you will see the menu items for ActiveSnippet's as shown above. The command keyboard shortcut is located to the right of every menu option that has one configured.
The ActiveSnippet Configuration can be accessed by selecting the ActiveSnippet Configuration menu item located in the Visual Studio Generator sub-menu. The ActiveSnippet Configuration dialog allows you to Add, Remove, Edit or view all ActiveSnippets, Once you've created a template for usage as an ActiveSnippet, you must add the ActiveSnippet which maps to a CodeSmith Generator Template.
ActiveSnippets must be configured inside Visual Studio in order to be used. |
Find detailed information on Configuring an ActiveSnippet.
ActiveSnippet usage information can be obtained through the CodeSmith Generator Output Window. CodeSmith attempts to find the ActiveSnippet usage information using the context of the current line with focus. To display the output usage for configured ActiveSnippets you can select the Output ActiveSnippet Usage menu item or press Ctrl+E, Ctrl+R.
Calling an ActiveSnippet is easy. Once configured, you simply have to enter the alias or name along with any argument parameters. Once you have defined the active snippet you want to expand, you just need to select the Expand ActiveSnippet menu item or press CTRL-E, CTRL-E.
If you are unsure about an ActiveSnippets usage is, you can select the Output ActiveSnippet Usage menu item or press Ctrl+E, Ctrl+R. |
By default the shortcut for Expanding an ActiveSnippet is CTRL-E, CTRL-E. |
In the example below we will execute an ActiveSnippet with the name tp and pass it one argument parameter.
tp Petshop.dbo.Orders |
to execute this active snippet we will select the Expand ActiveSnippet menu item or press CTRL-E, CTRL-E. The below screenshot shows the code which was generated by this ActiveSnippet.