Search in sources :

Example 51 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project eclipse.platform.text by eclipse.

the class TemplateTranslatorTest method testNoVarTemplate.

public void testNoVarTemplate() throws Exception {
    TemplateBuffer buffer = fTranslator.translate("foo bar");
    assertNull(fTranslator.getErrorMessage());
    TemplateVariable[] vars = buffer.getVariables();
    assertEquals(0, vars.length);
    assertEquals("foo bar", buffer.getString());
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer)

Example 52 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project eclipse.platform.text by eclipse.

the class TemplateVariablesWordSelectionTest method testWithParameter.

@Test
public void testWithParameter() throws Exception {
    TemplateBuffer buffer = fTranslator.translate("No selection results in the ${w:word_selection('default')} text.");
    fType.resolve(buffer, fContext);
    StringBuffer expected = new StringBuffer();
    expected.append("No selection results in the default text.");
    assertBufferStringAndVariables(expected.toString(), buffer);
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Test(org.junit.Test)

Example 53 with TemplateBuffer

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

use of org.eclipse.jface.text.templates.TemplateBuffer in project webtools.sourceediting by eclipse.

the class NewHTMLTemplatesWizardPage 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 = HTMLUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsHTML.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 html", 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 55 with TemplateBuffer

use of org.eclipse.jface.text.templates.TemplateBuffer in project webtools.sourceediting by eclipse.

the class NewJSPTemplatesWizardPage 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 = JSPUIPlugin.getDefault().getTemplateContextRegistry().getContextType(TemplateContextTypeIdsJSP.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 jsp", 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)

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