use of org.eclipse.jface.text.templates.TemplateContext in project xtext-eclipse by eclipse.
the class AbstractTemplateProposalProvider method createTemplates.
@Override
public void createTemplates(ContentAssistContext context, ITemplateAcceptor acceptor) {
if (!acceptor.canAcceptMoreTemplates())
return;
TemplateContext[] templateContexts = createTemplateContexts(context);
if (templateContexts == null || templateContexts.length == 0)
return;
ITemplateAcceptor nullSafe = new NullSafeTemplateAcceptor(acceptor);
for (TemplateContext templateContext : templateContexts) {
if (!nullSafe.canAcceptMoreTemplates())
return;
// name of the selection variables {line, word}_selection //$NON-NLS-1$
templateContext.setVariable("selection", context.getSelectedText());
createTemplates(templateContext, context, nullSafe);
}
}
use of org.eclipse.jface.text.templates.TemplateContext in project xtext-eclipse by eclipse.
the class XtextTemplateProposal method getAdditionalProposalInfo.
@Override
public String getAdditionalProposalInfo() {
try {
TemplateContext context = getContext();
context.setReadOnly(true);
TemplateBuffer templateBuffer;
try {
Template template = getTemplate();
if (context instanceof XtextTemplateContext) {
templateBuffer = ((XtextTemplateContext) context).evaluateForDisplay(template);
} else {
templateBuffer = context.evaluate(template);
}
} catch (TemplateException e) {
return null;
}
return templateBuffer.getString();
} catch (BadLocationException e) {
return null;
}
}
use of org.eclipse.jface.text.templates.TemplateContext in project erlide_eclipse by erlang.
the class ErlTemplateProposal method getAdditionalProposalInfo.
@Override
public String getAdditionalProposalInfo() {
try {
final TemplateContext context = getContext();
context.setReadOnly(true);
TemplateBuffer templateBuffer;
try {
final Template template = getTemplate();
if (context instanceof ErlangTemplateContext) {
final ErlangTemplateContext etc = (ErlangTemplateContext) context;
templateBuffer = etc.evaluate(template, true);
} else {
templateBuffer = context.evaluate(template);
}
} catch (final TemplateException e) {
return null;
}
return templateBuffer.getString();
} catch (final BadLocationException e) {
return null;
}
}
use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class XMLTemplateCompletionProcessor method computeCompletionProposals.
/*
* Copied from super class except instead of calling createContext(viewer,
* region) call createContext(viewer, region, offset) instead
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// adjust offset to end of normalized selection
if (selection.getOffset() == offset) {
offset = selection.getOffset() + selection.getLength();
}
String prefix = extractPrefix(viewer, offset);
Region region = new Region(offset - prefix.length(), prefix.length());
TemplateContext context = createContext(viewer, region, offset);
if (context == null) {
return new ICompletionProposal[0];
}
// name of the selection variables {line, word}_selection
// $NON-NLS-1$
context.setVariable("selection", selection.getText());
Template[] templates = getTemplates(context.getContextType().getId());
List matches = new ArrayList();
for (int i = 0; i < templates.length; i++) {
Template template = templates[i];
try {
context.getContextType().validate(template.getPattern());
} catch (TemplateException e) {
continue;
}
if (template.matches(prefix, context.getContextType().getId())) {
matches.add(createProposal(template, context, (IRegion) region, getRelevance(template, prefix)));
}
}
Collections.sort(matches, fgProposalComparator);
return (ICompletionProposal[]) matches.toArray(new ICompletionProposal[matches.size()]);
}
use of org.eclipse.jface.text.templates.TemplateContext in project webtools.sourceediting by eclipse.
the class NewXSLFileTemplatesWizardPage method getTemplateString.
String getTemplateString(int[] offset) {
String templateString = null;
offset[0] = 0;
Template template = getSelectedTemplate();
if (template != null) {
TemplateContextType contextType = XSLUIPlugin.getDefault().getTemplateContextRegistry().getContextType(XSLUIConstants.TEMPLATE_CONTEXT_XSL_NEW);
IDocument document = new Document();
TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
try {
TemplateBuffer buffer = context.evaluate(template);
templateString = buffer.getString();
for (TemplateVariable t : buffer.getVariables()) {
if (t.getName().equals(org.eclipse.jface.text.templates.GlobalTemplateVariables.Cursor.NAME)) {
if (t.getOffsets().length > 0)
offset[0] = t.getOffsets()[0];
}
}
} catch (Exception e) {
XSLUIPlugin.log(e);
}
}
return templateString;
}
Aggregations