Eclipse:从插件代码访问编辑器模板 [英] Eclipse: Accessing a editor template from plugin code

查看:125
本文介绍了Eclipse:从插件代码访问编辑器模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想以编程方式访问该模板。
我如何做到这一点?



我知道TemplateStore,TemplatePreferencesPage和TemplatePersistentData的类别存在,但是我无法把它们放在一起



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

解决方案

可能是这个 JavaPlugin class (在$ code> org.eclipse.jdt.internal.ui eclipse包中)可能会提供你有一个第一个跟随。

  / ** 
*返回代码生成模板的模板存储。
*
* @返回代码生成模板的模板存储
* @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();

//重复模板的兼容性/错误修复代码
// TODO remove for 3.0
CompatibilityTemplateStore.pruneDuplicates(fCodeTemplateStore,true);
}

return fCodeTemplateStore;
}

从那里可以找到一些使用该函数的类:



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

  private String resolveTemplate(String templateName){

模板模板= ASEditorPlugin.getDefault()。getCodeTemplateStore()。findTemplate(templateName);
if(template == null){
showErrorBox(无法解析模板(+ templateName +)。
return;
}

//创建模板上下文
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(Could not evaluate template,e);
} catch(TemplateException e){
logger.error(Couldnt evaluate template,e);
}
return;

}

这样使用:

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


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.

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

解决方案

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

 /**
  * 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:

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

这篇关于Eclipse:从插件代码访问编辑器模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆