Sunday, December 25, 2005

Code Templates: Define the Cursor Position

Today I was asked how you can define where the cursor will appear after the code template editing was done. Imagine a simple template, which only will add following code.
begin

end;
Now, you would like to position the cursor (shown as "|") as following after invoking the template.
begin
|
end;
The XML template for that task is rather easy, let's say you have not defined any delimiter character, so the IDE will default to the dollar "$" sign. Then you would define the template like the following snippet.
<code
language="Delphi"
context="methodbody">
<![CDATA[begin
$selected$$*$$end$
end;]]>
</code>
If you want to use another delimiter you can simple declare it with a delimiter attribute like the following code snippet will show.
<code
language="Delphi"
context="methodbody"
delimiter="|">
<![CDATA[begin
|selected||*||end|
end;]]>
</code>
I hope that this will help you in your endavours in creating new code templates.

An afterthougt: Why does that work and why do you need to write |selected||*||end|. Well, in my template I have setting surround="true". Why? Simply, you can select some code in your editor and call that template from the Surround -> context menu point.

The editor will now take the selected code and place it into the place designated by |selected||*||end|. If no code is selected, when you invoke the template, the IDE will simply put your cursor there, because it is the selection by itself. ;-)

0 Comments:

Post a Comment

<< Home

Google
Search WWW Search delphi-notes.blogspot.com