Search in sources :

Example 1 with Templates

use of org.eclipse.mylyn.internal.wikitext.ui.editor.assist.Templates in project mylyn.docs by eclipse.

the class WikiTextUiPlugin method getTemplates.

/**
 * get templates mapped by their markup language name
 *
 * @return the templates
 */
public Map<String, Templates> getTemplates() {
    if (templates == null) {
        Map<String, Templates> templates = new HashMap<>();
        IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(getPluginId(), EXTENSION_POINT_CONTENT_ASSIST);
        if (extensionPoint != null) {
            IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements();
            for (IConfigurationElement element : configurationElements) {
                String declaringPluginId = element.getDeclaringExtension().getContributor().getName();
                if (EXTENSION_POINT_TEMPLATES.equals(element.getName())) {
                    try {
                        // $NON-NLS-1$
                        String markupLanguage = element.getAttribute("markupLanguage");
                        if (markupLanguage == null) {
                            throw new Exception(Messages.WikiTextUiPlugin_markupLanguageRequired);
                        } else if (!WikiText.getMarkupLanguageNames().contains(markupLanguage)) {
                            throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_invalidMarkupLanguage, new Object[] { markupLanguage }));
                        }
                        Templates markupLanguageTemplates = new Templates();
                        markupLanguageTemplates.setMarkupLanguageName(markupLanguage);
                        for (IConfigurationElement templatesChild : element.getChildren()) {
                            if (EXTENSION_POINT_TEMPLATE.equals(templatesChild.getName())) {
                                try {
                                    // process the template
                                    // $NON-NLS-1$
                                    String name = templatesChild.getAttribute("name");
                                    // $NON-NLS-1$
                                    String description = templatesChild.getAttribute("description");
                                    // $NON-NLS-1$
                                    String content = templatesChild.getAttribute("content");
                                    // $NON-NLS-1$
                                    String autoInsert = templatesChild.getAttribute("autoInsert");
                                    // $NON-NLS-1$
                                    String block = templatesChild.getAttribute("block");
                                    if (name == null || name.length() == 0) {
                                        throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_nameRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
                                    }
                                    if (description == null || description.length() == 0) {
                                        throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_descriptionRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
                                    }
                                    if (content == null || content.length() == 0) {
                                        throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_contentRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
                                    }
                                    // $NON-NLS-1$//$NON-NLS-2$
                                    content = content.replace("\\t", "\t");
                                    content = // $NON-NLS-1$
                                    content.replace("\\r\\n", Text.DELIMITER).replace(// $NON-NLS-1$
                                    "\\r", Text.DELIMITER).replace("\\n", // $NON-NLS-1$
                                    Text.DELIMITER).replace("\\\\", // $NON-NLS-1$ //$NON-NLS-2$
                                    "\\");
                                    if (// $NON-NLS-1$
                                    content.endsWith("$") && !(content.endsWith("\\$") || content.endsWith("$$"))) {
                                        // $NON-NLS-1$ //$NON-NLS-2$
                                        content = content.substring(0, content.length() - 1);
                                    }
                                    if (content.startsWith("^")) {
                                        // $NON-NLS-1$
                                        content = content.substring(1);
                                    }
                                    // $NON-NLS-1$ //$NON-NLS-2$
                                    content = content.replace("\\$", "$$");
                                    markupLanguageTemplates.addTemplate(new Template(name, description, MarkupTemplateCompletionProcessor.CONTEXT_ID, content, autoInsert == null || // $NON-NLS-1$
                                    !"false".equalsIgnoreCase(autoInsert)), // $NON-NLS-1$
                                    block != null && "true".equalsIgnoreCase(block));
                                } catch (Exception e) {
                                    log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_invalidExtension, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, e.getMessage() }), e);
                                }
                            } else {
                                log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_unexpectedExtensionElement, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, templatesChild.getName() }), null);
                            }
                        }
                        Templates previous = templates.put(markupLanguageTemplates.getMarkupLanguageName(), markupLanguageTemplates);
                        if (previous != null) {
                            markupLanguageTemplates.addAll(previous);
                        }
                    } catch (Exception e) {
                        log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_invalidExtension, new Object[] { declaringPluginId, EXTENSION_POINT_TEMPLATES, e.getMessage() }), e);
                    }
                } else {
                    log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_unexpectedExtensionElement, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, element.getName() }), null);
                }
            }
        }
        // now that we have the basic templates, check for language extensions and connect the hierarchy
        // first ensure that all language names have templates defined
        Set<String> languageNames = WikiText.getMarkupLanguageNames();
        for (String languageName : languageNames) {
            Templates languageTemplates = templates.get(languageName);
            if (languageTemplates == null) {
                languageTemplates = new Templates();
                templates.put(languageName, languageTemplates);
            }
        }
        // next connect the hierarchy
        for (String languageName : languageNames) {
            MarkupLanguage markupLanguage = WikiText.getMarkupLanguage(languageName);
            if (markupLanguage != null && markupLanguage.getExtendsLanguage() != null) {
                Templates languageTemplates = templates.get(languageName);
                Templates parentLanguageTemplates = templates.get(markupLanguage.getExtendsLanguage());
                languageTemplates.setParent(parentLanguageTemplates);
            }
        }
        this.templates = Collections.unmodifiableMap(templates);
    }
    return templates;
}
Also used : IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) HashMap(java.util.HashMap) Templates(org.eclipse.mylyn.internal.wikitext.ui.editor.assist.Templates) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) CoreException(org.eclipse.core.runtime.CoreException) Template(org.eclipse.jface.text.templates.Template)

Aggregations

HashMap (java.util.HashMap)1 CoreException (org.eclipse.core.runtime.CoreException)1 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)1 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)1 Template (org.eclipse.jface.text.templates.Template)1 Templates (org.eclipse.mylyn.internal.wikitext.ui.editor.assist.Templates)1 MarkupLanguage (org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage)1