Search in sources :

Example 11 with TemplateVariableResolver

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

the class ContributionContextTypeRegistry method createResolver.

private static TemplateVariableResolver createResolver(IConfigurationElement element) throws CoreException {
    try {
        String type = element.getAttribute(TYPE);
        if (type != null) {
            TemplateVariableResolver resolver = (TemplateVariableResolver) element.createExecutableExtension(CLASS);
            resolver.setType(type);
            String desc = element.getAttribute(DESCRIPTION);
            // $NON-NLS-1$
            resolver.setDescription(desc == null ? "" : desc);
            return resolver;
        }
    } catch (ClassCastException e) {
    // throw new CoreException(new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, "extension does not implement " + TemplateVariableResolver.class.getName(), e)); //$NON-NLS-1$
    }
    return null;
}
Also used : TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 12 with TemplateVariableResolver

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

the class FunctionVariableResolver method resolve.

@Override
public void resolve(final TemplateVariable variable, final TemplateContext context) {
    @SuppressWarnings("unchecked") final Iterator<TemplateVariableResolver> it = ErlangSourceContextTypeLayout.getDefault().resolvers();
    FunctionNameVariableResolver name_var = null;
    // !TODO: Use BodyVariableResolver
    // BodyVariableResolver body_var = null;
    ArgumentsVariableResolver arg_var = null;
    while (it.hasNext()) {
        final TemplateVariableResolver element = it.next();
        if (element instanceof FunctionNameVariableResolver) {
            name_var = (FunctionNameVariableResolver) element;
        } else if (element instanceof BodyVariableResolver) {
        // body_var = (BodyVariableResolver) element;
        } else if (element instanceof ArgumentsVariableResolver) {
            arg_var = (ArgumentsVariableResolver) element;
        }
    }
    if (arg_var == null || name_var == null) // || body_var == null
    {
        variable.setValue("");
        return;
    }
    final StringBuilder buff = new StringBuilder();
    for (final Object[] element : functions) {
        arg_var.setArity((Integer) element[1]);
        name_var.setFunctionName((String) element[0]);
        final Template commentTemplate = ErlangSourceContextTypeComment.getDefault().getTemplateStore().getTemplateData("org.erlide.ui.erlangsource.functioncomment").getTemplate();
        DocumentTemplateContext commentContext = new DocumentTemplateContext(ErlangSourceContextTypeLayout.getDefault(), new Document(commentTemplate.getPattern()), 0, commentTemplate.getPattern().length());
        TemplateBuffer tb = null;
        try {
            tb = commentContext.evaluate(commentTemplate);
        } catch (final BadLocationException e) {
            ErlLogger.warn(e);
            buff.append("Error: " + e.getMessage());
        } catch (final TemplateException e) {
            ErlLogger.warn(e);
            buff.append("Error: " + commentTemplate.getName() + " could not be validated!");
        }
        if (tb != null) {
            buff.append(tb.getString() + "\n");
        }
        final Template template = ErlangSourceContextTypeComment.getDefault().getTemplateStore().getTemplateData("org.erlide.ui.erlangsource.functionlayout").getTemplate();
        commentContext = new DocumentTemplateContext(ErlangSourceContextTypeLayout.getDefault(), new Document(template.getPattern()), 0, template.getPattern().length());
        try {
            tb = commentContext.evaluate(template);
        } catch (final BadLocationException e) {
            ErlLogger.warn(e);
            buff.append("Error: " + e.getMessage());
        } catch (final TemplateException e) {
            ErlLogger.warn(e);
            buff.append("Error: " + template.getName() + " could not be validated!");
        }
        if (tb != null) {
            buff.append(tb.getString() + "\n");
        }
    }
    variable.setValue(buff.toString());
}
Also used : TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) Template(org.eclipse.jface.text.templates.Template) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) BadLocationException(org.eclipse.jface.text.BadLocationException) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 13 with TemplateVariableResolver

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

the class JavaLanguageServerPlugin method getTemplateContextRegistry.

/**
 * Returns the template context type registry for the java plug-in.
 *
 * @return the template context type registry for the java plug-in
 */
public synchronized ContextTypeRegistry getTemplateContextRegistry() {
    if (fContextTypeRegistry == null) {
        ContextTypeRegistry registry = new ContextTypeRegistry();
        JavaContextType statementContextType = new JavaContextType();
        statementContextType.setId(JavaContextType.ID_STATEMENTS);
        statementContextType.setName(JavaContextType.ID_STATEMENTS);
        statementContextType.initializeContextTypeResolvers();
        // Todo: Some of the resolvers is defined in the XML of the jdt.ui, now we have to add them manually.
        // See: https://github.com/eclipse/eclipse.jdt.ui/blob/cf6c42522ee5a5ea21a34fcfdecf3504d4750a04/org.eclipse.jdt.ui/plugin.xml#L5619-L5625
        TemplateVariableResolver resolver = new VarResolver();
        resolver.setType("var");
        statementContextType.addResolver(resolver);
        registry.addContextType(statementContextType);
        fContextTypeRegistry = registry;
    }
    return fContextTypeRegistry;
}
Also used : JavaContextType(org.eclipse.jdt.ls.core.internal.corext.template.java.JavaContextType) ContextTypeRegistry(org.eclipse.text.templates.ContextTypeRegistry) VarResolver(org.eclipse.jdt.internal.corext.template.java.VarResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver)

Example 14 with TemplateVariableResolver

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

the class TemplateValidator method checkParameters.

@Check
public void checkParameters(Variable variable) {
    Codetemplate template = EcoreUtil2.getContainerOfType(variable, Codetemplate.class);
    Codetemplates templates = EcoreUtil2.getContainerOfType(template, Codetemplates.class);
    if (templates != null && template != null) {
        Grammar language = templates.getLanguage();
        AbstractRule rule = template.getContext();
        ContextTypeIdHelper helper = languageRegistry.getContextTypeIdHelper(language);
        if (helper != null && rule != null && !rule.eIsProxy() && rule instanceof ParserRule) {
            String contextTypeId = helper.getId(rule);
            ContextTypeRegistry contextTypeRegistry = languageRegistry.getContextTypeRegistry(language);
            TemplateContextType contextType = contextTypeRegistry.getContextType(contextTypeId);
            if (contextType != null) {
                Iterator<TemplateVariableResolver> resolvers = Iterators.filter(contextType.resolvers(), TemplateVariableResolver.class);
                String type = variable.getType();
                if (type == null)
                    type = variable.getName();
                while (resolvers.hasNext()) {
                    final TemplateVariableResolver resolver = resolvers.next();
                    if (resolver.getType().equals(type)) {
                        IInspectableTemplateVariableResolver inspectableResolver = registry.toInspectableResolver(resolver);
                        if (inspectableResolver != null) {
                            inspectableResolver.validateParameters(variable, this);
                        }
                    }
                }
            }
        }
    }
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Codetemplates(org.eclipse.xtext.ui.codetemplates.templates.Codetemplates) Codetemplate(org.eclipse.xtext.ui.codetemplates.templates.Codetemplate) ContextTypeRegistry(org.eclipse.jface.text.templates.ContextTypeRegistry) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) ContextTypeIdHelper(org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) IInspectableTemplateVariableResolver(org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver) TemplateVariableResolver(org.eclipse.jface.text.templates.TemplateVariableResolver) Check(org.eclipse.xtext.validation.Check)

Example 15 with TemplateVariableResolver

use of org.eclipse.jface.text.templates.TemplateVariableResolver in project che 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)28 TemplateContextTypeXSL (org.eclipse.wst.xsl.ui.internal.templates.TemplateContextTypeXSL)10 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)9 CoreException (org.eclipse.core.runtime.CoreException)5 ContextTypeRegistry (org.eclipse.jface.text.templates.ContextTypeRegistry)5 ContextTypeIdHelper (org.eclipse.xtext.ui.editor.templates.ContextTypeIdHelper)5 TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)4 IInspectableTemplateVariableResolver (org.eclipse.xtext.ui.codetemplates.ui.resolvers.IInspectableTemplateVariableResolver)4 IConfigurationElement (org.eclipse.core.runtime.IConfigurationElement)3 TemplateTranslator (org.eclipse.jface.text.templates.TemplateTranslator)3 StyledString (org.eclipse.jface.viewers.StyledString)3 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 Grammar (org.eclipse.xtext.Grammar)2 Codetemplate (org.eclipse.xtext.ui.codetemplates.templates.Codetemplate)2 Codetemplates (org.eclipse.xtext.ui.codetemplates.templates.Codetemplates)2 Variable (org.eclipse.xtext.ui.codetemplates.templates.Variable)2 ConfigurableCompletionProposal (org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal)2 ArrayList (java.util.ArrayList)1