Using a template in a template - eclipse

As I crawl slowly in my transition from .net to Java, I find more and more interesting things about the Eclipse IDE. I recently stumbled upon his templates and I love him. This leads me to the question: can I call a template from a template? Of course, that would be just copy and paste, but I wonder if this can be done.

+8
java eclipse
source share
1 answer

Yes, in fact, you can and there is an example on the right in the default set.

If you go to your settings -> Java -> Code Style -> Code Templates, you can Export all the provided Java templates. There you will see the following file template (formatted for readability):

<template autoinsert="true" context="filecomment_context" deleted="false" description="Comment for created Java files" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.filecomment" name="filecomment"> /** * */ </template> 

and a little further down, a new type that uses this file template:

 <template autoinsert="true" context="newtype_context" deleted="false" description="Newly created files" enabled="true" id="org.eclipse.jdt.ui.text.codetemplates.newtype" name="newtype"> ${filecomment} ${package_declaration} ${typecomment} ${type_declaration} </template> 

So, if you want the template to use another, the main form is to refer to the identifier of your subpattern with the dollar sign prefix. For example:

 <template autoinsert="true" context="BobOuter_context" deleted="false" description="Bob example outer template" enabled="true" id="bob.example.outertemplate" name="BobOuter"> BobOuterBegins Insert inner template ${bob.example.innertemplate} BobOuterEnds </template> <template autoinsert="true" context="BobInner_context" deleted="false" description="Bob example inner template" enabled="true" id="bob.example.innertemplate" name="BobInner"> BobInnerBegins Super awesome content goes here BobInnerEnds </template> 
+4
source share

All Articles