Code templates - unit name clash between Delphi versions
Asked Answered
F

2

6

Different Delphi versions (in my case 2007, XE and XE2) seem to use the same code-template repository, such as ..\Documents\RAD Studio\code_templates. Since XE2 uses namespaced units, this causes problems when one have to use a unit name in a template.

For instance I have a template when I write winbeep which completes to windows.Beep(300, 100); and selects and stops on frequency etc.. This does not compile on XE2 unless I remove the Winapi. part of the Windows unit in the uses clause (or of course add the prefix manually). I have to have the unit name because otherwise SysUtils.Beep gets referred.

I couldn't find an option entry or a registry key or an environment variable specifying the place of the templates. It is not even related to BDSUSERDIR, in fact I don't even have a directory in my computer having the path of BDSUSERDIR (which is ..\Documents\RAD Studio\9.0). Is this my oversight and in fact there's an option to relocate code templates? If not, is it possible to make the IDE use unit aliases in uses clause for new forms?

edit:
Here it is stated that (emphasis by me) "Templates that you create (and templates provided by third party add-ins) are saved by default in the \My Documents\RAD Studio\code templates\ directory." That would imply there should be way to save them in a non-default place. But the article does not mention how.

Felicific answered 13/4, 2012 at 1:14 Comment(0)
P
6

You can put your templates in "Delphi installation folder"\ObjRepos\en\Code_Templates to make them visible only on one Delphi version.

Parkerparkhurst answered 13/4, 2012 at 6:41 Comment(1)
Great! Copying a modified version of the template file to this folder solves it. Looks like Delphi is reading this folder first. Thanks!Felicific
L
2

Edit

As Sertac points out in the comments, Template libraries are related to project templates, not code templates. Leaving this answer in place because it may help people looking for ways to structure / share / split their project template repositories.


In Delphi XE2 you have a "Template libraries" option under the "Tools" menu. When you open that, you get this dialog:

enter image description here

The "Add" button allows you to add a folder, the "Properties" button allows you to view and manage the contents of that repository.

So it would seem to me that you can simply copy the template library, change it XE2 specific stuff, and then point XE2 to its own repository. Or you could do that only for the templates that require specific XE2 unit handling.

Lipsey answered 13/4, 2012 at 6:40 Comment(1)
Thanks Marjan, but as far as I can see this is related with "project templates", not "code templates". See docs.Felicific

© 2022 - 2024 — McMap. All rights reserved.