Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Java编辑器的插件向Eclipse提供模板,但应根据上下文而有所不同_Eclipse_Templates_Plugins_Editor_Eclipse Jdt - Fatal编程技术网

通过Java编辑器的插件向Eclipse提供模板,但应根据上下文而有所不同

通过Java编辑器的插件向Eclipse提供模板,但应根据上下文而有所不同,eclipse,templates,plugins,editor,eclipse-jdt,Eclipse,Templates,Plugins,Editor,Eclipse Jdt,因此,我编写了一个插件,使用扩展名“org.eclipse.ui.editors.templates”为eclipse中的java编辑器提供一个模板。这只是添加了模板。那不是我想要的。我想用新模板替换现有模板,这也是基于条件的 例如,如果文件名为“john.java”,如果我键入“sysout”,我希望在该文件中显示我的模板,而不是默认模板。你知道我该怎么做吗?好的。我终于找到了工作。JDT插件并没有为我试图做的事情提供扩展点。所以我得到了TemplateStore并修改它以满足我的需要 @Ov

因此,我编写了一个插件,使用扩展名“org.eclipse.ui.editors.templates”为eclipse中的java编辑器提供一个模板。这只是添加了模板。那不是我想要的。我想用新模板替换现有模板,这也是基于条件的


例如,如果文件名为“john.java”,如果我键入“sysout”,我希望在该文件中显示我的模板,而不是默认模板。你知道我该怎么做吗?

好的。我终于找到了工作。JDT插件并没有为我试图做的事情提供扩展点。所以我得到了TemplateStore并修改它以满足我的需要

@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时,都会执行此代码,并执行您的模板风格

在上面的代码中,我已经用我的插件根据条件提供的版本替换了普通的java sysout,因此在任何时候,提案中都只显示了一个版本的“sysout”。这是一个黑客,但我已经完成了我的事情