General Structure[]
A Live Template looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<codetemplate xmlns="http://schemas.borland.com/Delphi/2005/codetemplates" version="1.0.0">
<template name="NameOfTemplate" invoke="none|auto|manual">
<description>Description goes here</description>
<author>Author OfTheTemplate</author>
<point name="NameOfPoint">
<text>default text here</text>
<hint>hint for the user</hint>
</point>
<![CDATA[
The actual content goes here.
]]>
</template>
</codetemplate>
Elements[]
- codetemplate
- template
- description - the text displayed in the template viewer
- author - the author of the template
- point* - 0..n place holders for text
- text - the default text
- hint - a hint to be displayed to the user
- code - the actual template
- <![CDATA[ .... ]]>
- script - this can apparently be added anywhere
- template
template element[]
The template element is the main element containing all the others. It has the following attributes:
Attribute | Required | Type | Default | Description |
---|---|---|---|---|
name | yes | string | - | shortcut name of template |
inplace | no | boolean | "true" | expansion template |
surround | no | boolean | "false" | template that is allowed
to surround text (also does UI filtering) |
invoke | no | enum | "none" | template invocation kind |
Values for invoke:
- auto - invoked by pressing SPACE or TAB
- manual - invoked by pressing TAB
- none - invoked by using CTRL+J, CTRL+SPACE, or using the template viewer
description element[]
The description element contains some descriptive text and apparently has no attributes.
author element[]
The author element contains the name of the template's author (this is free text, so it probably can contain anything, maybe an email address too?)
point element[]
A point element describes a place holder for text. It has the following attributes:
Attribute | Required | Type | Default | Description |
---|---|---|---|---|
name | yes | string | - | id of the point |
default | no | boolean | "false" | if true, this point is the
first selected point |
editable | no | boolean | "true" | if false, this point can
not be navigated to |
There are some predefined points:
- $selected$ - a non editable point that contains the text that is currently selected in the code editor
- $end$ - marks the cursor position after the template has been executed
- $*$ - An indentation based upon the current source options indentation settings (Equivalent to a single CTRL + SHIFT + I indentation level with the default keybinding)
script element[]
Attribute | Required | Type | Default | Description |
---|---|---|---|---|
language | yes | string | - | name of the script engine
to use for execution |
onvalidate | no | boolean | "false" | if true, the script is
executed before the template is inserted |
onenter | no | boolean | "true" | if true, the script is
executed when the template/point is entered |
onleave | no | boolean | "false" | if true, the script is
executed when the template/point is [successfully] left |
There is also an "onexit" attribute described in CodeTemplateApi.pas, but it seems to never be used in the existing templates.
The following function calls are available in script elements (from existing templates):
- language="Delphi":
- InvokeClassCompletion
- InvokeCodeCompletion (see delphi\trycf.xml)
- PopulateCase(|expression|) (see delphi\case.xml)
- DeclareVariable(|variable|) (see delphi\trycf.xml)
- RemoveTemplate (see delphi\inlinevar.xml)
- ValidateForTemplate (see delphi\fors.xml)
- language="C":
- invoke_code_completion (see c\whiles.xml)
- populate_case($expr$); (see c\switch.xml)
- language="DelphiExample" (see link below)
- GetClipboardContents (see clippy.xml)
- VarDeclToAssignment (see var.xml)
code element[]
Attribute | Required | Type | Default | Description |
---|---|---|---|---|
language | yes | string | - | language this template is
for. Used for editor source option mapping |
delimiter | no | string | "$" | jump point positional
delimiter |
context | no | enum | "any" | visibility context of
this template |
- language - one of (List can be extended, see links below)
- Delphi
- C
- CSharp
- VB
- Jsharp (why is that written with a lower case "s"?
- HTML
- XML
- IDE
- SQL
- IDL
- JavaScript
- CSS
- INI
- PHP
context can have the following values ("|" marks the place it describes):
- memberdecl - TFoo = class(TComponent) | end;
- methoddecl - procedure |
- decl - var |
- typedecl - type |
- methodbody - begin | end;
- page - reserved for future use
- file - reserved for future use
- any - any context
According to CodeTemplateApi.pas there are the following additional values, which are probably not supported:
- None - ?
- Comment - inside a comment ?
- StringLiteral - inside a string literal ?
The default delimiter is "$", but since that may conflict with valid Delphi code like in
s := 'line'#$0a'feed';
it can be changed (most seem to use "|"). The delimiter is used in the code in front and after a placeholder point name.
Links[]
- Delphi Live Templates in the Delphi Wiki with quite a lot examples
- Nick Hodges Live Template Video Tutorial (no longer available)
- Creating a script template engine by Daniel "sakura" Wischnewski
- Creating live templates in BDS 2006 by Deborah Pate (on archive.org)
- Creating Live template basics by Adam Markowitz (on archive.org)
- Live Template examples by Adam Markowitz (on archive.org)
- sync edit params (on archive.org) (not quite Live Templates related, but very interesting anyway)
- "Creating a Live Templates Scripting Engine" by Nick Hodges (on archive.org)
- "Live Templates: basics through to script engines" video presentation by Lachlan Gemmell for the Australian Delphi User Group