Eclipse:从插件代码访问编辑器模板

Pla*_*mer 4 java eclipse eclipse-plugin

假设我的编辑器首选项中定义了一个编辑器模板(插入一些任意代码片段).

我想以编程方式访问该模板.我该怎么做呢?

我知道TemplateStore,TemplatePreferencesPage和TemplatePersistentData这两个类存在,但是我无法将它们放在一起工作.

是否有任何示例代码允许我通过Java代码访问我的编辑器模板?

Von*_*onC 7

可能是这个JavaPlugin(在org.eclipse.jdt.internal.uieclipse的包内)可能会为你提供第一个跟随的引导.

 /**
  * Returns the template store for the code generation templates.
  *
  * @return the template store for the code generation templates
  * @since 3.0
  */
 public TemplateStore getCodeTemplateStore() {
     if (fCodeTemplateStore == null) {
         IPreferenceStore store= getPreferenceStore();
         boolean alreadyMigrated= store.getBoolean(CODE_TEMPLATES_MIGRATION_KEY);
         if (alreadyMigrated)
             fCodeTemplateStore= new ContributionTemplateStore(getCodeTemplateContextRegistry(), store, CODE_TEMPLATES_KEY);
         else {
             fCodeTemplateStore= new CompatibilityTemplateStore(getCodeTemplateContextRegistry(), store, CODE_TEMPLATES_KEY, getOldCodeTemplateStoreInstance());
             store.setValue(CODE_TEMPLATES_MIGRATION_KEY, true);
         }

         try {
             fCodeTemplateStore.load();
         } catch (IOException JavaDoc e) {
             log(e);
         }

         fCodeTemplateStore.startListeningForPreferenceChanges();

         // compatibility / bug fixing code for duplicated templates
         // TODO remove for 3.0
        CompatibilityTemplateStore.pruneDuplicates(fCodeTemplateStore, true);            
     }

     return fCodeTemplateStore;
 }
Run Code Online (Sandbox Code Playgroud)

从那里,你可以找到一些使用该功能的类:

NewASInterfaceWizard似乎需要访问这些代码模板:

private String resolveTemplate(String templateName) {

        Template template = ASEditorPlugin.getDefault().getCodeTemplateStore().findTemplate(templateName);
        if (template == null) {
            showErrorBox("Could not resolve template (" + templateName +").");
            return "";
        }

        // Create the template context
        TemplateContext templeteContext = new TemplateContext(new ASContextType()) {

            public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
                TemplateTranslator translator = new TemplateTranslator();
                TemplateBuffer buffer = translator.translate(template);
                getContextType().resolve(buffer, this);
                return buffer;
            }

            public boolean canEvaluate(Template template) {
                return true;
            }

        };

        try {
            return templeteContext.evaluate(template).getString();
        } catch (BadLocationException e) {
            logger.error("Couldnt evaluate template",e);
        } catch (TemplateException e) {
            logger.error("Couldnt evaluate template",e);
        }
       return "";

}
Run Code Online (Sandbox Code Playgroud)

像这样使用:

        private static final String FILE_HEADER_TEMPLATE = "file_header";
        // Header
        String header = resolveTemplate(FILE_HEADER_TEMPLATE);
        if (header.length() > 0) {
            content.append(header + "\n");
        }
Run Code Online (Sandbox Code Playgroud)