Home > Code Generation > Creating CodeRush code templates overview

Creating CodeRush code templates overview

April 26th, 2012

Creating CodeRush code templates is easy. They are being created on the Templates options page in the Options Dialog. The Templates options page is an excellent way to explore and learn how templates are built. When a template expands, and you want to see how it is built, just bring up the Template options page and the last template expanded will be displayed. I recommend maximizing the Options dialog and collapse the options tree view on the left by clicking those tiny blue rectangles that appear when you hover over the area between the tree view and options page area:

Creating CodeRush Templates #1

Templates are organized into categories. There are dozens of built-in categories in the CodeRush templates library, so it is recommended to create your own root folder for newly created templates. If you create your own folder, you can easily share it among other developers or other computers after its export. Categories can also be enabled or disabled by clicking the checkbox to the left of the category name.

Before you start creating templates, make sure you have chosen the correct language on the bottom of the Options dialog. Remember, that templates are language specific – so it is important to choose the correct language. Then, let’s create our root category, where all new templates will be stored. To create a new root category, right-click anywhere on the templates categories list, and choose “New Root Category…”:

Creating CodeRush Templates #2

Type the name of the category and hit Enter or click OK:

Creating CodeRush Templates #3

Once the root category is created, we can add new templates in this category by clicking the corresponding button on the tool bar or right-clicking the category and choosing “New Template”:

Creating CodeRush Templates #4

Type the name of the template, preferably according to the Templates language or something unique:

Creating CodeRush Templates #5

Now, it’s time to write a template expansion. An expansion can contain the following:

  • Plain text. A plain text is expanded as is. The text typed in the Expansion area is inserted without modification.
  • String providersString providers are used like string functions returning some calculated or hard-coded string value like current date, current file name, new guid, etc.
  • Text commands. Text commands are like inline procedures that execute some code performing an action like moving the caret, dropping a marker, creating a new file, adding an assembly reference, opening a dialog – everything you want. The standard text commands library contain a lot of useful commands which can be expanded by creating your own text commands via the special DXCore component.

To learn more about creating your own templates, refer to the following articles:

  • Creating simple templates step by step, where the simple templates creation process is described. Simple templates have an easy text expansion containing simplest string providers and text commands.
  • Creating advanced code templates step by step, where complex templates are created. Such templates may have alternate expansions and use advanced text commands like ForEach, Get, Set, etc.
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Oct/02/2012
ID: C160

Similar Posts: