Search in sources :

Example 6 with TemplateVariableResolver

use of org.eclipse.jface.text.templates.TemplateVariableResolver in project flux by eclipse.

the class ContributionContextTypeRegistry method createContextType.

/**
	 * Tries to create a context type given an id. Contributions to the
	 * <code>org.eclipse.ui.editors.templates</code> extension point are
	 * searched for the given identifier and the specified context type
	 * instantiated if it is found. Any contributed
	 * {@link org.eclipse.jface.text.templates.TemplateVariableResolver}s
	 * are also instantiated and added to the context type.
	 *
	 * @param id the id for the context type as specified in XML
	 * @return the instantiated and configured context type, or
	 *         <code>null</code> if it is not found or cannot be instantiated
	 */
public static TemplateContextType createContextType(String id) {
    Assert.isNotNull(id);
    IConfigurationElement[] extensions = getTemplateExtensions();
    TemplateContextType type;
    try {
        type = createContextType(extensions, id);
        if (type != null) {
            TemplateVariableResolver[] resolvers = createResolvers(extensions, id);
            for (int i = 0; i < resolvers.length; i++) type.addResolver(resolvers[i]);
        }
    } catch (CoreException e) {
        //EditorsPlugin.log(e);
        type = null;
    }
    return type;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 7 with TemplateVariableResolver

use of org.eclipse.jface.text.templates.TemplateVariableResolver in project flux by eclipse.

the class CodeTemplateContext method evaluate.

/*
	 * @see org.eclipse.jdt.internal.corext.template.TemplateContext#evaluate(org.eclipse.jdt.internal.corext.template.Template)
	 */
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
    // test that all variables are defined
    Iterator<TemplateVariableResolver> iterator = getContextType().resolvers();
    while (iterator.hasNext()) {
        TemplateVariableResolver var = iterator.next();
        if (var instanceof CodeTemplateContextType.CodeTemplateVariableResolver) {
            //$NON-NLS-1$ //$NON-NLS-2$
            Assert.isNotNull(getVariable(var.getType()), "Variable " + var.getType() + "not defined");
        }
    }
    if (!canEvaluate(template))
        return null;
    String pattern = changeLineDelimiter(template.getPattern(), fLineDelimiter);
    TemplateTranslator translator = new TemplateTranslator();
    TemplateBuffer buffer = translator.translate(pattern);
    getContextType().resolve(buffer, this);
    return buffer;
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Aggregations

TemplateVariableResolver (org.eclipse.jface.text.templates.TemplateVariableResolver)7 CoreException (org.eclipse.core.runtime.CoreException)3 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)3 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)2 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)2 TemplateTranslator (org.eclipse.jface.text.templates.TemplateTranslator)2 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 CodeTemplateContextType (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContextType)1