Search in sources :

Example 41 with TemplateBuffer

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

the class GlobalTemplateVariablesDateTest method testInvalidDateFormat.

@Test
public void testInvalidDateFormat() throws Exception {
    TemplateBuffer buffer = fTranslator.translate("Today is ${d:date('invalid')}!");
    fType.resolve(buffer, fContext);
    StringBuffer expected = new StringBuffer();
    expected.append("Today is ");
    expected.append(DateFormat.getDateInstance().format(new java.util.Date()));
    expected.append("!");
    assertBufferStringAndVariables(expected.toString(), buffer);
}
Also used : TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Test(org.junit.Test)

Example 42 with TemplateBuffer

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

the class TemplateTranslatorTest method testMultiParameterizedTypeTemplate2.

@Test
public void testMultiParameterizedTypeTemplate2() throws Exception {
    TemplateBuffer buffer = fTranslator.translate("foo ${var:type(param)} bar ${var} end");
    assertNull(fTranslator.getErrorMessage());
    assertEquals("foo var bar var end", buffer.getString());
    TemplateVariable[] vars = buffer.getVariables();
    assertEquals(1, vars.length);
    assertEquals("var", vars[0].getName());
    assertEquals(2, vars[0].getOffsets().length);
    assertEquals(4, vars[0].getOffsets()[0]);
    assertEquals(12, vars[0].getOffsets()[1]);
    assertEquals(3, vars[0].getLength());
    assertEquals(false, vars[0].isUnambiguous());
    assertEquals("var", vars[0].getDefaultValue());
    assertEquals(1, vars[0].getValues().length);
    assertEquals(vars[0].getDefaultValue(), vars[0].getValues()[0]);
    assertEquals("type", vars[0].getType());
    assertEquals(Collections.singletonList("param"), vars[0].getVariableType().getParams());
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Test(org.junit.Test)

Example 43 with TemplateBuffer

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

the class TemplateTranslatorTest method testParameterizedTypeTemplateWithWhitespace.

@Test
public void testParameterizedTypeTemplateWithWhitespace() throws Exception {
    TemplateBuffer buffer = fTranslator.translate("foo ${ var : type ( param1 , param2 , param3 ) } bar");
    assertNull(fTranslator.getErrorMessage());
    assertEquals("foo var bar", buffer.getString());
    TemplateVariable[] vars = buffer.getVariables();
    assertEquals(1, vars.length);
    assertEquals("var", vars[0].getName());
    assertEquals(1, vars[0].getOffsets().length);
    assertEquals(4, vars[0].getOffsets()[0]);
    assertEquals(3, vars[0].getLength());
    assertEquals(false, vars[0].isUnambiguous());
    assertEquals("var", vars[0].getDefaultValue());
    assertEquals(1, vars[0].getValues().length);
    assertEquals(vars[0].getDefaultValue(), vars[0].getValues()[0]);
    assertEquals("type", vars[0].getType());
    List<String> params = new ArrayList<>(2);
    params.add("param1");
    params.add("param2");
    params.add("param3");
    assertEquals(params, vars[0].getVariableType().getParams());
}
Also used : ArrayList(java.util.ArrayList) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Test(org.junit.Test)

Example 44 with TemplateBuffer

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

the class TemplateTranslatorTest method testSimpleTemplate.

@Test
public void testSimpleTemplate() throws Exception {
    TemplateBuffer buffer = fTranslator.translate("foo ${var} bar");
    assertNull(fTranslator.getErrorMessage());
    assertEquals("foo var bar", buffer.getString());
    TemplateVariable[] vars = buffer.getVariables();
    assertEquals(1, vars.length);
    assertEquals("var", vars[0].getName());
    assertEquals(1, vars[0].getOffsets().length);
    assertEquals(4, vars[0].getOffsets()[0]);
    assertEquals(3, vars[0].getLength());
    assertEquals(false, vars[0].isUnambiguous());
    assertEquals("var", vars[0].getDefaultValue());
    assertEquals(1, vars[0].getValues().length);
    assertEquals(vars[0].getDefaultValue(), vars[0].getValues()[0]);
    assertEquals("var", vars[0].getType());
}
Also used : TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Test(org.junit.Test)

Example 45 with TemplateBuffer

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

the class TemplateTranslatorTest method testQualifiedTypeTemplate.

@Test
public void testQualifiedTypeTemplate() throws Exception {
    TemplateBuffer buffer = fTranslator.translate("foo ${ var : qual.type ( qual.param1, qual.param2 ) } bar");
    assertNull(fTranslator.getErrorMessage());
    assertEquals("foo var bar", buffer.getString());
    TemplateVariable[] vars = buffer.getVariables();
    assertEquals(1, vars.length);
    assertEquals("var", vars[0].getName());
    assertEquals(1, vars[0].getOffsets().length);
    assertEquals(4, vars[0].getOffsets()[0]);
    assertEquals(3, vars[0].getLength());
    assertEquals(false, vars[0].isUnambiguous());
    assertEquals("var", vars[0].getDefaultValue());
    assertEquals(1, vars[0].getValues().length);
    assertEquals(vars[0].getDefaultValue(), vars[0].getValues()[0]);
    assertEquals("qual.type", vars[0].getType());
    List<String> params = new ArrayList<>(2);
    params.add("qual.param1");
    params.add("qual.param2");
    assertEquals(params, vars[0].getVariableType().getParams());
}
Also used : ArrayList(java.util.ArrayList) TemplateVariable(org.eclipse.jface.text.templates.TemplateVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) Test(org.junit.Test)

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