Live Template Script Examples - var declaration with initialization and clipboard contents (Delphi2006)
In the shipping version of Delphi2006 you can type the word ‘var’ in the code editor and hit TAB. This allows the user to fill out a template for a variable declaration. This variable then gets declared in the containing methods var block without having to navigate all over the place. I find this very convenient and so do a number of others. However, there have been some requests for a modification to this template that allows the user to not only declare the variable, but initialize it with a default value as well. I have uploaded this new template and an example of a code template script engine that implements some of this functionality here: http://codecentral.borland.com/item.aspx?id=23910
The use of this new template is as follows:
type “var“
then hit the TAB key.
You will get something that looks like this:
LVar: Integer := 3;
Fill out the variable name, type, and initialization value. Hit ENTER or tab off the last template point and it will be changed to:
LVar := 3;|
(or whatever you named it and initialized it to).
Additonally, there is a template called ‘clippy’ that allows a user to get the clipboard contents into a template point. This example wraps the clipboard contents with a try…finally block. So if you copy/cut some code to the clipboard and then type:
clippy
your clipboard contents should be wrapped in a try finally block.
Notes: You’ll need to download the code from http://codecentral.borland.com/item.aspx?id=23910, unzip it, build it, install it as a design time package in the IDE, and copy the live templates (.xml files) into the code_templates directory (either the Local Settings one or the one under the objRepos directory).
Let me know if there are any other nifty template ideas and I’ll see what I can do.
’til next time!
Share This | Email this page to a friend
Posted by Adam Markowitz archive on January 20th, 2006 under Uncategorized |

RSS Feed

January 20th, 2006 at 6:46 pm
Thanks.
The biggest problem with code templates is having to explain to your boss that rather than actually working on something you where developing a code template
January 20th, 2006 at 9:11 pm
Hahahahaha!
January 21st, 2006 at 5:24 am
Hi,
the url referenced in all the code templates (see title) is not available. I always thought the point of having these urls was to be able to validate xml documents???
twm
January 21st, 2006 at 10:30 am
Thomas,
It is not a requirement that a schema be available at the URI reference location, merely that it be unique and persistable. Check here for more info:
http://www.w3.org/TR/REC-xml-names/#ns-decl
January 22nd, 2006 at 4:06 am
Hi Adam,
… is there any other source of information about the format of these files and which keywords are available? I have been looking throught the online help in vain. It is very scarce on information about the templates. (your blog entries are be the best info source about it, that I could find).
twm
January 22nd, 2006 at 10:30 am
Other than the codetemplates.xsd file that ships, there appears to be no other public info. Sorry
January 23rd, 2006 at 12:50 am
Hi Adam,
as you by now already know from the newsgroups I have started to collect my findings on
http://delphi.wikicities.com/wiki/Live_Templates_Technical_Info
I hereby invite everybody to contribute. It’s a Wiki, so you can just edit the page.
twm
January 23rd, 2006 at 2:35 pm
Holy smokes. I’ve just got D2006 and started tinkering with this template thing. I never even looked at the scripting. Hmmmm… Man, I wish I had more time to put on those. I may have to make time somehow, but it would probably help if I learned Delphi itself a bit better before I launch into something like that. Naaaaa…
January 23rd, 2006 at 3:31 pm
Dan,

Nope. Just jump on in and swim like the rest of us
January 28th, 2006 at 9:24 am
Adam,
Thanks a ton! Your code inspired me to write a PInvoke Live Template Engine at http://shenoyatwork.blogspot.com/2006/01/pinvoke-live-template.html
Also contains a Stock Quote engine, which is somewhat cool and totally impractical.
Cheers!
Deepak
February 2nd, 2006 at 3:39 pm
How do I enter a blank lines in CDATA?
For example, the following does *not* put a blank line before and after the cursor.
<![CDATA[
|end|
]]>
February 2nd, 2006 at 9:44 pm
Currently the template engine trim()’s the CDATA when it is read in. If you want whitespace the ‘trick’ you can do is put in some dummy points that are marked as editable="false" do delimit the begin/end of the code. Then you can add whitespace to your hearts content
HTH,
-Adam
February 3rd, 2006 at 9:21 am
Close, but I still can’t make it work. I have to put non-space text in the <text> element to get it to work, which defeats the purpose. That is, the following does not work. Ideas?
<point name="topX" editable="false">
<text> </text>
</point>
<point name="botX" editable="false">
<text> </text>
</point>
<description>
New Line Wrapper
</description>
<code language="Delphi" context="typedecl" delimiter="|">
<![CDATA[
|topX|
|end|
|botX|
]]>
</code>
February 7th, 2006 at 11:44 am
Strangely, this template didn’t work for me … implementing it as directed I *do* end up with variables being declared properly, but instead of ending up with something like "Lvar:=3;" I end up with "Lvar:Integer:=3;"
I’m open to suggestions
Thanks — Al C (aicolburn at hotmail.com)
April 2nd, 2006 at 5:29 am
Hi Adam,
a bit late, but I just wanted to let you know that I receive the same AVs when re-installing your package I reported on your blog earlier this year.
I’ve reported this to QualityCentral, see http://qc.borland.com/wc/qcmain.aspx?d=26895
I hope this is the AV you are referring to in your Notes (but those should only occur on Exit?). Anyway, this really makes developing ScriptEngines a PITA since you have to restart Delphi after each compile of the package!
Cheers
Sebastian
April 2nd, 2006 at 11:24 am
Sebastian,
Yes, it is a PITA. The problem was that code template items (scripts, templates, points, etc) registered/installed in an IDE package were not properly being uninstalled when the package went away (the package goes away on each installation of the package not just when the IDE goes away). Obviously there would be leftover pointers to invalid memory. This should hopefully be resolved in the near future.
Thanks,
-Adam
April 5th, 2006 at 4:22 am
Adam, I really like this and I tried to expand on the ScriptExample. I wanted to add a function that inserted the date and time automatically in the template. Strange thing is that I can change your clipboard.astext to datetimetostr(now) and it works fine. But if I try to add that as a new function together with the clipboard one I get an access violation when doing the install. Any ideas?
johan
April 5th, 2006 at 5:31 am
OK, I got it to work after some experimentation. I had to remove the installed template. Edit the script file and then do a fresh build and install.
johan
April 5th, 2006 at 5:39 am
BTW: Is it possible to combine these automatic variables in a template with variables that become highlighted and were you are supposed to fill in some text yourself when you use the template?
johan
April 5th, 2006 at 9:02 am
Johan,
Yes, the crash on uninstall/install can be attributed to the IDE not unloading things properly from packages (i.e. when you uninstall a package the IDE needs to go through everything registered with that package and make sure references are cleared otherwise it will be pointing off into bad memory). The uninstall/install crash should be fixed in a later release (hopefully temporally near).
As far as combining these type of points, I don’t see why not. Can you desribe a bit more what you’re trying to do?
Thanks,
-Adam
April 5th, 2006 at 12:07 pm
Thanks, Yes, me and my colleague insert comment lines in xml format to record the update history of the units.
It looks like:
<history>
<event sign="jl" date="2006-etc">bla bla</event>
</history>
So I have one template for a history with one event line and then another template for adding an event line.
By using your package I managed to add a script GetDateTime() that returned datetimetostr(now) and I managed to direct that into the date attribute above.
But I also tried to add a <point name="text" where the actual text of the event-note should go. But I did not get that highlighted as normally happens with other templates. So I thought that the script-thing would take over in some way.
johan
April 5th, 2006 at 1:05 pm
Johan,
Feel free to mail me the template that doesn’t invoke a live sync point and I’ll see if I can find anything wrong with it.
-Adam
April 5th, 2006 at 11:45 pm
So here is one:
<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates"">http://schemas.borland.com/Delphi/2005/codetemplates"
version="1.0.0">
<template name="event" invoke="manual">
<point name="datetime" editable="false">
</point>
<point name="texten" editable="true">
<text>Text</text>
<hint>Insert text here</hint>
</point>
<description>
Insert an eventline
</description>
<author>
Johan Lindgren
</author>
<script language="DelphiExample" onenter="false" onvalidate="true">
|datetime| := GetDateTime();
</script>
<code language="Delphi" context="methodbody" delimiter="|"><![CDATA[<event sign="jl" date="|datetime|">|texten|</event>
]]>
</code>
</template>
</codetemplate>
And it shows the word Text but insertion point is not there.
Another variant that gets the insertion point in the right place but without the hint.
<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates"">http://schemas.borland.com/Delphi/2005/codetemplates"
version="1.0.0">
<template name="event" invoke="manual">
<point name="datetime" editable="false">
</point>
<description>
Insert an eventline
</description>
<author>
Johan Lindgren
</author>
<script language="DelphiExample" onenter="false" onvalidate="true">
|datetime| := GetDateTime();
</script>
<code language="Delphi" context="methodbody" delimiter="|"><![CDATA[<event sign="jl" date="|datetime|">|end|</event>
]]>
</code>
</template>
</codetemplate>
johan
August 24th, 2006 at 8:49 am
Hi Adam,
Any hints on getting the class name and current method name like CodeRush does for the csn keyboard short cut?
The result was something like this:
procedure TMyClass.MyProc;
begin
{$ifdef Debug}
CodeSite.EnterMethod( ‘TMyClass.MyProc’);
{$endif}
end;
I have a template that does this, but I just use the clipboard contents for the method name.
BTW, nice job on the new templates!
Thanks,
Rob
June 20th, 2007 at 5:18 am
I do enjoy your initialized variable example.
Any template / to make a resourcestring? Or any
reference of what functions are available in the
Delphi script language?