通过Java编辑器的插件向Eclipse提供模板,但应根据上下文而有所不同 [英] Contributing a template to Eclipse via a Plugin to the Java Editor, but should vary based on the context

查看:306
本文介绍了通过Java编辑器的插件向Eclipse提供模板,但应根据上下文而有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了一个插件,通过使用扩展名org.eclipse.ui.editors.templates向Eclipse中的java编辑器贡献一个模板。这只是添加了模板。这不是我想要的。我想用新的模板替换现有的模板,这也是基于条件的。

So I wrote a plugin to contribute a template to the java editor in eclipse by using the extension "org.eclipse.ui.editors.templates". This just adds the template. That's not what I want. I want to replace the existing template with the new one, that too based on a condition.

例如,如果文件名为john.java,则在该文件中,如果输入sysout,我希望我的模板显示而不是默认一。任何想法我可以如何做到这一点?

For example, if the file name is "john.java", within that file if I type "sysout", I want my template to show up rather than the default one. Any ideas how I might be able to do this ?

推荐答案

好的。我终于找到了一个工作。 JDT插件不提供我正在尝试做的扩展点。所以我得到了TemplateStore并修改了它以满足我的需要。

OK. I finally found a work around. JDT plugin does not offer an extension point for what I was trying to do. So I got the TemplateStore and modified it to suit my needs.

@Override
public void sessionStarted() {
    filterTemplates();
}

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

    if (//file name is john.java) {
        deleteTemplate(templateStore, "org.eclipse.jdt.ui.templates.sysout");
    } else {
        deleteTemplate(templateStore, "com.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);
    }
}

@Override
public void sessionEnded() {
    getTemplateStore().restoreDeleted();
}

private TemplateStore getTemplateStore() {
    if (templateStore == null) {
        final ContributionContextTypeRegistry registry = new ContributionContextTypeRegistry(JavaUI.ID_CU_EDITOR);
        final IPreferenceStore store = PreferenceConstants.getPreferenceStore();
        templateStore = new ContributionTemplateStore(registry, store, "org.eclipse.jdt.ui.text.custom_templates");
        try {
            templateStore.load();
        } catch (IOException e) {
        }
        templateStore.startListeningForPreferenceChanges();
    }
    return templateStore;
}

这整个写在扩展IJavaCompletionProposalComputer的类中。所以每次按Ctrl + Space时,这个代码都会被执行,你的模板风格就会被执行。

This whole this is written inside a class that extends IJavaCompletionProposalComputer. So every time you press Ctrl+Space, this code will be executed and your flavor of template will get executed.

在上面的代码中,我已经替换了普通的java sysout与我的插件基于条件贡献的,所以在任何时间点,提案中只显示了一个版本的sysout。这是一个黑客,但我的事情完成了。

In the above code, I have replaced the normal java sysout with the one that my plugin contributes based on the condition, so that at any point in time only one version of the 'sysout' is shown in the proposal. This is a hack, but I got my things done.

这篇关于通过Java编辑器的插件向Eclipse提供模板,但应根据上下文而有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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