Eclipse Custom Variable for Java Code Templates

前端 未结 7 1095
予麋鹿
予麋鹿 2020-12-30 09:38

How do you add a new variable to be inserted into a Java code template. How do I add a variable to the list in Window->Preferences->Java->Code Style->Code Templates->Code->N

相关标签:
7条回答
  • 2020-12-30 10:00

    I'm pretty sure that the list of "variables" is generated by Eclipse and there is no way to add a new template variable.

    What do you want ${begin_filecomment} and ${end_filecomment} to be? Just type the content into the Edit box - there is nothing that says you cannot put static content in there.

    0 讨论(0)
  • 2020-12-30 10:00

    I didn't try it, but maybe you could edit the template files in the JDT jar:

    eclipse\plugins\org.eclipse.jdt.ui_*.jar\templates\
    
    0 讨论(0)
  • 2020-12-30 10:01

    Yes, you can indeed add a variable to this list. See the extension point called

    org.eclipse.ui.editors.templates

    and you'll find out how.

    0 讨论(0)
  • 2020-12-30 10:02

    See http://thomaswabner.wordpress.com/2009/08/21/use-your-own-variable-in-eclipse-code-templates/

    0 讨论(0)
  • 2020-12-30 10:08

    and see http://ahoehma.wordpress.com/2009/08/24/use-maven-artifact-version-in-eclipse-templates/

    0 讨论(0)
  • 2020-12-30 10:11

    To contribute a new variable, you need to create a plugin and implement the org.eclipse.ui.editors.templates extension-point.

    You have to contribute a sub-class of org.eclipse.jface.text.templates.TemplateVariableResolver that will implement the various resolve methods to resolve the placeholder to your desired value(s).

    See here for help on the extension point and an example that contributes the ant variables.

    As matt b says, you often just need to define your text as boilerplate, so implementing a variable for that is overkill.

    0 讨论(0)
提交回复
热议问题