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;
}
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());
}
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;
}
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);
}
}
}
}
}
}
}
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;
}
Aggregations