Search in sources :

Example 21 with TemplateBuffer

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;
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer)

Example 22 with TemplateBuffer

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;
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template)

Example 23 with TemplateBuffer

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;
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) TemplateContext(org.eclipse.jface.text.templates.TemplateContext) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType) IDocument(org.eclipse.jface.text.IDocument) Template(org.eclipse.jface.text.templates.Template)

Example 24 with TemplateBuffer

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;
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer)

Example 25 with TemplateBuffer

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);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) TemplateException(org.eclipse.jface.text.templates.TemplateException) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

TemplateBuffer (org.eclipse.jface.text.templates.TemplateBuffer)71 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)30 BadLocationException (org.eclipse.jface.text.BadLocationException)26 TemplateException (org.eclipse.jface.text.templates.TemplateException)26 Document (org.eclipse.jface.text.Document)23 Template (org.eclipse.jface.text.templates.Template)23 Test (org.junit.Test)22 IDocument (org.eclipse.jface.text.IDocument)21 CoreException (org.eclipse.core.runtime.CoreException)17 DocumentTemplateContext (org.eclipse.jface.text.templates.DocumentTemplateContext)11 TemplateTranslator (org.eclipse.jface.text.templates.TemplateTranslator)11 TemplateContext (org.eclipse.jface.text.templates.TemplateContext)9 TemplateContextType (org.eclipse.jface.text.templates.TemplateContextType)8 CodeTemplateContext (org.eclipse.jdt.internal.corext.template.java.CodeTemplateContext)6 SimpleDateFormat (com.ibm.icu.text.SimpleDateFormat)4 CodeGenerationTemplate (org.eclipse.jdt.ls.core.internal.preferences.CodeGenerationTemplate)4 ArrayList (java.util.ArrayList)3 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)3