Telerik® JustCode™ by Progress

Telerik® JustCode™ comes with a set of predefined templates for the programming languages it supports. The real power of the code templates, though, stems from the ability to define your own code templates by going to JustCode -> Options -> Code Templates.

Any text you put in the code template editor is reproduced in the Visual Studio code editor when the template gets expanded unless it belongs to any of the following categories – template variable, template function or special character. Anything that belongs to these categories gets evaluated by Telerik® JustCode™ when the template is expanded and it gets replaced with the output of that evaluation.

Beware that $ character is special and it must to be escaped \$ in case you want to use it.

Defining File Templates

If you want your template to be expanded into a new file, place $NEW_FILE$ in the begging template's body.

Template Variables

The syntax for defining a template variable is:

Copy 
$number[=function]$

Where number is any natural number and function is any of the template functions in the Template Functions section.

So, the template variables are designated by non-repeating natural numbers, i.e. 1,2,3… Apart from serving as unique identifiers for the template variables these numbers also specify the order in which the user gets to pick values for the template variables. The lower numbered variables are visited before the higher numbered ones.

There is a very important consequence of the order in which the user gets to choose values for the template variables. Namely, $2$ can be a function of $1$ but $1$ cannot be a function of $2$ since the value of $2$ is not specified by the user at the time $1$ gets to be specified.

Here we have a basic template for an if statement:

Copy 
if ($1=SELECT_VARIABLE(bool)$)
{
|
}

Here we are defining a template variable that serves as a condition of the if statement and we are designating it with 1. When the template gets expanded that variable will get replaced by the return value of the function SELECT_VARIABLE if it returns a single item (i.e. there is only one boolean member in scope). If it returns more than one item (i.e. there is more than one boolean member in scope) the user will be given the chance to choose which one of the list of items returned will replace $1$.

We can further develop our template by referring to that variable again.

Copy 
if ($1=SELECT_VARIABLE(bool)$)
{
        $1$  = false;  //this will now output the value of $1$ variable inside the if block, and set its value to false
}

When we expand the template we get the following:

CopyC#
bool test = true; //declared in my code

//below is what my new if template generated for us
if ( test )
{
       test = false;
}

Example

A foreach template is a good example of template variable specification order.

Copy 
  foreach ( $3=GET_ELEMENT_TYPE($1$)$
          $2=SUGGEST_ELEMENT_NAME($1$)$ in
          $1=SELECT_VARIABLE([],System.Collections.ArrayList)$ )
{
|
}

The user gets to choose the value of $1$ first when the template is expanded. That way the choices the user can make for the values of $2$ and $3$ get evaluated based on the chosen value of $1$ and the user gets the respective values to choose from once he moves forward to specifying values for these variables.

Template Functions

SELECT

This allows you to give the template user a list of predefined constant values when the template is run. It works much like a combobox.

Example:

Copy 
$6=SELECT(choice one, choice two, choice three)$

SELECT_VARIABLE

Tries to find valid members of a given type(s) that are in scope. It will look for fields, and properties of the specified type. It will also look for methods that return the specified types, and require no parameters. If you do not pass in types for it to search for, then it will get all variables in scope.

Arguments: A comma separated list of variable types to search for.

Examples:

Copy 
SELECT_VARIABLE(System.Collections.Generic.List)

This will find all members of type “List” available in the current context.

You can also specify multiple valid types by separating them with a comma:

Copy 
SELECT_VARIABLE(int,uint)

SUGGEST_NAMESPACE

Returns suggestion for a namespace in accordance with the project's default namespace and the relative path to the file.

Example:

CopyC#
namespace $1=SUGGEST_NAMESPACE()$
{
...
}

SUGGEST_NAME

Returns suggestions for identifier name in accordance with the syntax rules of the programming language in use.

Example:

Copy 
bool $1=SUGGEST_NAME$ = false;
do {
...
...
}
while($1$)

SUGGEST_ELEMENT_NAME

This will try to come up with a good identifier name for an identifier that is a placeholder for the elements of a collection being iterated through (e.g. as in C# foreach syntax). The function argument should be evaluated to a collection type variable identifier or a list of such identifiers.

Arguments: Could be evaluated to a collection or a list of collections. At present, of all template functions only the SELECT_VARIABLE template function returns such a list.

Example:

Copy 
  foreach ( $3=GET_ELEMENT_TYPE($1$)$
          $2=SUGGEST_ELEMENT_NAME($1$)$ in
          $1=SELECT_VARIABLE([],System.Collections.ArrayList)$ )
{
|
}

$1$ will hold a list of all the arrays and IList implementers in scope.

Code example:

CopyC#
// Member In Scope:
List<string> myItems = new List<string>();

// Result:
foreach (string myItem in myItems)
{

}

Code example:

CopyC#
// Member In Scope:
List<string> myList = new List<string>();

// Result:
foreach (string e in myList)
{

}

SUGGEST_FRIENDLY_NAME

Suggest a collection of names that have been added by the user.

Arguments: A comma separated list of variables as a suggestion for the function name.

Example:

Copy 
public void $1=SUGGEST_FRIENDLY_NAME(MethodName,DoWork)$ ()
{
|
}

Code example:

CopyC#
// Result:
public void MethodName()
{

}

SUGGEST_INDEX_NAME

Tries to suggest a name for an index variable in a loop.

SUGGEST_EXCEPTION_NAME

Tries to suggest a name for an exception.

GET_FILE_NAME

Returns the name of the containing file without extension.

Example:

CopyC#
class Class1
{
  void Method1()
  {
    Console.WriteLine("Current file is: {0}", $1=GET_FILE_NAME()$);
  }
}

GET_TYPE_NAME

Returns the name of the containing type.

Example:

CopyC#
class Class1
{
  void Method1()
  {
    Console.WriteLine("Current type name is: {0}", $1=GET_TYPE_NAME()$);
  }
}

GET_MEMBER_NAME

Returns the name of the containing type member.

Example:

CopyC#
class Class1
{
  void Method1()
  {
    Console.WriteLine("Current method is: {0}", $1=GET_MEMBER_NAME()$);
  }
}

GET_ELEMENT_TYPE

Returns the type of the identifier given as an argument. Notice that this command works for collections only.

Arguments: Could be evaluated to an identifier.

Example:

Copy 
$1=SELECT_VARIABLE(System.Collections.Generic.List,System.Collections.Generic.IList)$
$2=GET_ELEMENT_TYPE($1$)$

This would output the type for the elements contained by an IList<> found in the current scope.

STATEMENT_SELECTION

A placeholder for any code the user might have highlighted prior to starting the template. You could see an example of this in all the “Surround with” templates.

Special Characters

| - marks the desired position of the cursor right after the user has committed all the changes on the template.

\ - escape character. Place it before | or $ or \ if they are not meant to be part of the template syntax, i.e. you want them literally reproduced when the template gets expanded.

Defining acronyms

Additionally when you create you own template, you can define an acronym for it. In the code template editor there is a text block you specify the acronym in. Once specified, the code template can easily be used by entering the acronym and pressing Shift+Space. You can also change the acronym for already defined template.

See Also