Pages

Monday, November 19, 2012

Contributing Java Editor templates via a plugin

JDT provides several editor templates. You can also add your own via the Templates View. However, if you want to share the templates between your several workspaces or with others you can also create a plugin.

As a first step, define the following extension for the extension point org.eclipse.ui.editors.templates.
   <extension
         point="org.eclipse.ui.editors.templates">
      <include
      file="templates/default-templates.xml"
      translations="$nl$/templates/default-templates.properties">
      </include>
   </extension>
The xml file just contains the template descriptors. For example, here is a template for printing the enclosing type and the enclosing method.
<template name="debugout" description="%Templates.debugout" id="com.eclipse.jdt.ui.templates.debugout" context="java-statements" enabled="true" autoinsert="true">
System.out.println("${enclosing_type}#${enclosing_method}(..)");${cursor}
</template>
You can read about the available template variables in Eclipse help.

I have also shared a plugin with the above code on GitHub.

2 comments:

  1. Hi Deepak,
    The code you shared is really useful. It's working fine but I have some issue. When I run plugin project as "Eclipse Application", I am able to see new templates. But when I try to export the project as "Deployable plug-ins and features" and add them to eclipse folder and restarts the eclipse, still I am not seeing the new templates.
    Please help !!!

    ReplyDelete
  2. Thanks Deepak, Your blog helped me :)

    ReplyDelete