Oct 052016
 

Delphi (XE2) comes with various predefined so called live templates which you can display with View->Templates. One of them is called “raise” and expands to

raise exception.Create('Error Message');

I don’t know about you but I use Exception.CreateFmt much more often than simply Exception.Create, so it annoys the hell out of me when every time I type raise I get the simple Create call and have to edit it. Also, I usually use DxGetText to translate my error messages using the _() function. So I have to add that as well.

Today I got tired of this and simply changed the template:

  1. Since you cannot edit the predefined templates, the first thing you do, is open it and copy the whole text into the clipboard.
  2. Add a new template and paste the text into it.
  3. Change it to your liking.
  4. Save it with the same file name but to the directory for user defined code templates, which is [your documents folder]\RAD Studio\code_templates.

That’s it, now the new user defined template will be called rather than the predefined one.

Now to the template itself. This is the original template:

<?xml version="1.0" encoding="utf-8" ?>
<codetemplate	xmlns="http://schemas.borland.com/Delphi/2005/codetemplates"
				version="1.0.0">
<template name="raise" invoke="auto">
  <description>
	Create and raise an instance of an Exception class
  </description>
  <author>
	Embarcadero (with thanks to Erik Berry)
  </author>
  <point name="exception">
	<script language="Delphi">InvokeCodeCompletion;</script>
	<text>Exception</text>
	<hint>Exception class</hint>
  </point>
  <point name="errormessage">
	<text>Error Message</text>
	<hint>Exception Message</hint>
  </point>
  <code language="Delphi" context="methodbody" delimiter="|">
   <![CDATA[raise |exception|.Create('|errormessage|');|end|]]>
  </code>
</template>
</codetemplate>

And this is my changed template:

<?xml version="1.0" encoding="utf-8" ?>
<codetemplate	xmlns="http://schemas.borland.com/Delphi/2005/codetemplates"
				version="1.0.0">
<template name="raise" invoke="auto">
  <description>
	Create and raise an instance of an Exception class
  </description>
  <author>
	Embarcadero (with thanks to Erik Berry)
  </author>
  <point name="exception">
	<script language="Delphi">InvokeCodeCompletion;</script>
	<text>Exception</text>
	<hint>Exception class</hint>
  </point>
  <point name="errormessage">
	<text>Error Message</text>
	<hint>Exception Message</hint>
  </point>
  <point name="parameters">
	<text>Parameters</text>
	<hint>Parameters go here</hint>
  </point>
  <code language="Delphi" context="methodbody" delimiter="|">
   <![CDATA[raise |exception|.CreateFmt(_('|errormessage|'), [|parameters|]);|end|]]>
  </code>
</template>
</codetemplate>

As you can see, I changed Create to CreateFmt, added the call to _() and also added a point element “parameters” for entering the parameters for the format specifiers, which is then referenced in the code element.

You can find more about live templates here:

Sorry, the comment form is closed at this time.

%d bloggers like this: