terça-feira, 21 de fevereiro de 2006

Understanding Delphi's codetemplate

For those who don’t know code templates we can say that they are models of codes made to improve the process of development time.

When you are writing your code just press the Ctrl+J to show the code templates list or type the code template name and press Space, the Delphi IDE will write the template code to be edited. Code templates can be made to appear in Delphi’s code completition.

So let’s create our code template.

1º - Go to View -> Templates, it will open a lateral bar with all of Delphi installed templates.
2º - Click at the new button, located at the top of the templates names.

A new document called template1.xml will be created with the follow tags:

<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns=" http://schemas.borland.com/Delphi/2005/codetemplates " version="1.0.0">
<template name="" invoke="manual">
<description>
</description>
<author>
</author>
<code language=""><![CDATA[]]> </code>
</template>
</codetemplate>

The first and the second line it’s just xml markup tags and it doesn’t need to be edited.
The template tag has a property called name, here you have to specify the name of our template (The name that will be displayed in IDE) and the property invoke that is used for:
Invoke = “auto”: The IDE will automatic load the template when it’s been typed.
Invoke = “manual”: The IDE will load the template after you typed and press the Tab Key.
Invoke = “none”: The template will be load just after press Ctrl+J and selecting from the list or pressing Ctrl+Space and selecting from the list.
The descripition tagis used to describe what this template is used for, like showed:
<description> This is a test template </description>
The autor tag is used for identify the templates autor ;) like showed:
<autor> Diego M. Garcia </autor>

Before we go to the code tag we have to know another tag called point that is used to identify the points of the code that should be navigable through the Tab Key.
The point tag should always have its name seted to be used in the code tag, so write your point tag like below:
<point name=”test”></point>

Now we should expecifie the text that will be displayed by the IDE inside the editor, to do that, add a tag named text inside the point tag like bellow:
<point name=”test”>
<text> TextToBeChanged </text>
</point>

It’s possible to add the tag called hint to display a hint when the mouse cursor is over the text.
Ex.:

<point name=”test”>
<text> TextToBeChanged </text>
<hint> This is a test Hint Text </hint>
</point>
Ps.: The code template can have more than one point tag.
We still have one tag to see before go to the code tag, that tag is the script tag that is used to call different types of services through IDE like declare a variable and other ones, to know a little more about it take a look at the code templates that come with Delphi. The script tag have a property called language that must be specified or Delphi wouldn’t know. In our case it would be Delphi, the script tag can have two properties to specify when the script will be triggered, the first is onenter=”true” that calls the script when the code template is loaded and the second is onleave=”true” that is called when the user end edit the code template.
Ex.:
<script language=” Delphi” onenter=”false” onleave=”true”>
</script>
The DeclareVariable command can be used in the script tag and it should reference an point tag that has the variable name definition in its text tag like below:
<point name="InternalVarName">
<text>
TypeAnIntegerValue</text>
<hint>
Variable to be used. </hint>
</point>

<script language="Delphi" onenter="false" onleave="true">
DeclareVariable(InternalVarName);
</script>

Ps.: The characters “|” (Pipes) are used before and after the name of the point tag, they are delimiters.

Until now I haven’t found a way to especify the type of an variable, because the editor automatic assumes the type passed to it.
The code tag write the code to the IDE editor our code template, it has the property language that works like the one in the script tag and the property delimiter that is used to identify the point tag names, in our case it will be the “|” pipe character, take a look above:
<code language=”Delphi” delimiter=””>
<![CDATA[]]>
</code>
Take a look at the automaticaly generated tag called CDATA, pay attention, the code to be displayed must be inside the characters [] or your code wouldn’t be displayed.
Lets write an code template to convert some value to Integer.
To do that add the code inside the CDATA tag.
InternalVarName := Convert.ToInt32(test);
 

Here is the full code template code:

<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns=" http://schemas.borland.com/Delphi/2005/codetemplates " version="1.0.0">
<template name="TestTemplate" invoke="manual">
<description> This is a test template </description>
<author>
Diego M. Garcia
</author>
<point name=”test”>
<text> TextToBeChanged </text>
<hint> This is a test Hint Text </hint>
</point>
<point name="InternalVarName">
<text>TypeAnIntegerValue</text>
<hint>Variable to be used. </hint>
</point>
<script language="Delphi" onenter="false" onleave="true">
DeclareVariable(InternalVarName);
</script>
<code language="Delphi" delimiter=””><![CDATA[InternalVarName := Convert.ToInt32(test);]]> </code>
</template>
</codetemplate>
Save and Execute your template through Ctrl + J or in case you have changed the invoke to auto, then just type the name of the template and press Space.
Ps.: Sometimes Delphi gives some errors when editing inside its editor, if it happens you have to close the IDE an open it again.

Take a look at the templates that comes with Delphi to discover other tags and other properties.

I Hope you guys have liked ;)

Regards,
Diego.

Nenhum comentário: