use of org.eclipse.jface.text.templates.TemplateBuffer in project webtools.sourceediting by eclipse.
the class ReplaceNameTemplateContext method evaluate.
/*
* @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template)
*/
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
TemplateBuffer buffer = super.evaluate(template);
if (buffer != null) {
if (fInsertOffset > -1 && fInsertOffset > getStart()) {
String prefix = getDocument().get(getStart(), fInsertOffset - getStart());
if (!template.getName().startsWith(prefix)) {
// generate a new buffer that actually contains the
// text that was going to be overwritten
int prefixSize = prefix.length();
TemplateVariable[] newTemplateVar = buffer.getVariables();
for (int i = 0; i < newTemplateVar.length; i++) {
int[] offsets = newTemplateVar[i].getOffsets();
for (int j = 0; j < offsets.length; j++) {
offsets[j] += prefixSize;
}
}
buffer = new TemplateBuffer(prefix + buffer.getString(), newTemplateVar);
}
}
}
return buffer;
}
use of org.eclipse.jface.text.templates.TemplateBuffer in project webtools.sourceediting by eclipse.
the class NewDTDTemplatesWizardPage method getTemplateString.
/**
* Returns template string to insert.
*
* @return String to insert or null if none is to be inserted
*/
String getTemplateString() {
String templateString = null;
Template template = getSelectedTemplate();
if (template != null) {
TemplateContextType contextType = DTDUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsDTD.NEW);
IDocument document = new Document();
TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
try {
TemplateBuffer buffer = context.evaluate(template);
templateString = buffer.getString();
} catch (Exception e) {
// $NON-NLS-1$
Logger.log(Logger.WARNING_DEBUG, "Could not create template for new dtd", e);
}
}
return templateString;
}
use of org.eclipse.jface.text.templates.TemplateBuffer in project webtools.sourceediting by eclipse.
the class NewXMLTemplatesWizardPage method getTemplateString.
/**
* Returns template string to insert.
*
* @return String to insert or null if none is to be inserted
*/
String getTemplateString() {
String templateString = null;
Template template = getSelectedTemplate();
if (template != null) {
TemplateContextType contextType = XMLUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsXML.NEW);
IDocument document = new Document();
TemplateContext context = new DocumentTemplateContext(contextType, document, 0, 0);
try {
TemplateBuffer buffer = context.evaluate(template);
templateString = buffer.getString();
} catch (Exception e) {
// $NON-NLS-1$
Logger.log(Logger.WARNING_DEBUG, "Could not create template for new xml", e);
}
}
return templateString;
}
use of org.eclipse.jface.text.templates.TemplateBuffer in project webtools.sourceediting by eclipse.
the class ReplaceNameTemplateContext method evaluate.
/*
* @see org.eclipse.jface.text.templates.TemplateContext#evaluate(org.eclipse.jface.text.templates.Template)
*/
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
TemplateBuffer buffer = super.evaluate(template);
String templateName = template.getName().toLowerCase();
if (buffer != null) {
if ((fInsertOffset > -1) && (fInsertOffset > getStart())) {
String prefix = getDocument().get(getStart(), fInsertOffset - getStart());
if (!templateName.startsWith(prefix.toLowerCase())) {
// generate a new buffer that actually contains the
// text that was going to be overwritten
int prefixSize = prefix.length();
TemplateVariable[] newTemplateVar = buffer.getVariables();
for (int i = 0; i < newTemplateVar.length; i++) {
int[] offsets = newTemplateVar[i].getOffsets();
for (int j = 0; j < offsets.length; j++) {
offsets[j] += prefixSize;
}
}
buffer = new TemplateBuffer(prefix + buffer.getString(), newTemplateVar);
}
}
}
return buffer;
}
use of org.eclipse.jface.text.templates.TemplateBuffer in project flux by eclipse.
the class StubUtility method evaluateTemplate.
private static String evaluateTemplate(CodeTemplateContext context, Template template, String[] fullLineVariables) throws CoreException {
TemplateBuffer buffer;
try {
buffer = context.evaluate(template);
if (buffer == null)
return null;
String str = fixEmptyVariables(buffer, fullLineVariables);
if (Strings.containsOnlyWhitespaces(str)) {
return null;
}
return str;
} catch (BadLocationException e) {
throw new CoreException(Status.CANCEL_STATUS);
} catch (TemplateException e) {
throw new CoreException(Status.CANCEL_STATUS);
}
}
Aggregations