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

时间:2023-01-24 22:24:37

Let's say I have a editor template (which inserts some arbitrary snippet of code) defined in my editor preferences.

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

I'd like to access that template programmatically. How do I do this?

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

I know the classes TemplateStore, TemplatePreferencesPage, and TemplatePersistentData exist, but I haven't been able to put them together into anything working.

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

Is there any example code that would allow me to access my editor template via Java code?

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

2 个解决方案

#1


May be this JavaPlugin class (within org.eclipse.jdt.internal.ui package of eclipse) may provide you with a first lead to follow.

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

 /**
  * 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;
 }

From there, you could find some class using that function:

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

NewASInterfaceWizard seems to need to access those code templates:

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 "";

}

Used like that:

像这样使用:

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

#2


Well this is how I did it.

这就是我做到的。

/**
 * Get the Template Store of the JDT UI.
 * 
 * @return the JDT template store
 */
private TemplateStore getTemplateStore() {
    if (templateStore == null) {
        System.out.println("templateStore is null - Creating a new one");

        final ContributionContextTypeRegistry registry = new ContributionContextTypeRegistry(JavaUI.ID_CU_EDITOR);
        final IPreferenceStore store = PreferenceConstants.getPreferenceStore();

        templateStore = new ContributionTemplateStore(registry, store, TEMPLATES_KEY);

        try {
            templateStore.load();
        } catch (IOException e) {
            WSConsole.e(e);
        }
        templateStore.startListeningForPreferenceChanges();
    }
    return templateStore;
}

The above method returns the TemplateStore. You use the store to add, delete, find templates.

上面的方法返回TemplateStore。您可以使用商店添加,删除,查找模板。

private void filterTemplates() {
    templateStore = getTemplateStore();

    deleteTemplate(templateStore, "org.eclipse.jdt.ui.templates.sysout");

    try {
        templateStore.save();
    } catch (IOException e) {
    }
}

private void deleteTemplate(TemplateStore templateStore, String id) {
    TemplatePersistenceData templateData = templateStore.getTemplateData(id);
    if (templateData != null) {
        templateStore.delete(templateData);
    }
}

This is how to perform operations on the TemplateStore. If you want to find the pattern, name, description of a Template, you can get that by getting a Template object from the TemplateStore. If you want to find the id of a template, you can get that from the TemplatePersistantData object.

这是如何在TemplateStore上执行操作。如果要查找模板的模式,名称,描述,可以通过从TemplateStore获取Template对象来实现。如果要查找模板的ID,可以从TemplatePersistantData对象获取该ID。

#1


May be this JavaPlugin class (within org.eclipse.jdt.internal.ui package of eclipse) may provide you with a first lead to follow.

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

 /**
  * 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;
 }

From there, you could find some class using that function:

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

NewASInterfaceWizard seems to need to access those code templates:

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 "";

}

Used like that:

像这样使用:

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

#2


Well this is how I did it.

这就是我做到的。

/**
 * Get the Template Store of the JDT UI.
 * 
 * @return the JDT template store
 */
private TemplateStore getTemplateStore() {
    if (templateStore == null) {
        System.out.println("templateStore is null - Creating a new one");

        final ContributionContextTypeRegistry registry = new ContributionContextTypeRegistry(JavaUI.ID_CU_EDITOR);
        final IPreferenceStore store = PreferenceConstants.getPreferenceStore();

        templateStore = new ContributionTemplateStore(registry, store, TEMPLATES_KEY);

        try {
            templateStore.load();
        } catch (IOException e) {
            WSConsole.e(e);
        }
        templateStore.startListeningForPreferenceChanges();
    }
    return templateStore;
}

The above method returns the TemplateStore. You use the store to add, delete, find templates.

上面的方法返回TemplateStore。您可以使用商店添加,删除,查找模板。

private void filterTemplates() {
    templateStore = getTemplateStore();

    deleteTemplate(templateStore, "org.eclipse.jdt.ui.templates.sysout");

    try {
        templateStore.save();
    } catch (IOException e) {
    }
}

private void deleteTemplate(TemplateStore templateStore, String id) {
    TemplatePersistenceData templateData = templateStore.getTemplateData(id);
    if (templateData != null) {
        templateStore.delete(templateData);
    }
}

This is how to perform operations on the TemplateStore. If you want to find the pattern, name, description of a Template, you can get that by getting a Template object from the TemplateStore. If you want to find the id of a template, you can get that from the TemplatePersistantData object.

这是如何在TemplateStore上执行操作。如果要查找模板的模式,名称,描述,可以通过从TemplateStore获取Template对象来实现。如果要查找模板的ID,可以从TemplatePersistantData对象获取该ID。