Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplateVariablesWordSelectionTest.java79
-rwxr-xr-xorg.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java7
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java25
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties4
4 files changed, 110 insertions, 5 deletions
diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplateVariablesWordSelectionTest.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplateVariablesWordSelectionTest.java
new file mode 100644
index 000000000..040683c65
--- /dev/null
+++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplateVariablesWordSelectionTest.java
@@ -0,0 +1,79 @@
+/*******************************************************************************
+ * Copyright (c) 2016 vogella GmbH and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Lars Vogel <Lars.Vogel@vogella.com> - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.text.tests.templates;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.eclipse.jface.text.Document;
+import org.eclipse.jface.text.templates.DocumentTemplateContext;
+import org.eclipse.jface.text.templates.GlobalTemplateVariables;
+import org.eclipse.jface.text.templates.TemplateBuffer;
+import org.eclipse.jface.text.templates.TemplateContextType;
+import org.eclipse.jface.text.templates.TemplateTranslator;
+import org.eclipse.jface.text.templates.TemplateVariable;
+
+public class TemplateVariablesWordSelectionTest {
+
+ private TemplateTranslator fTranslator;
+
+ private DocumentTemplateContext fContext;
+
+ private TemplateContextType fType;
+
+ @Before
+ public void setUp() {
+ fTranslator= new TemplateTranslator();
+
+ fType= new TemplateContextType();
+ fType.addResolver(new GlobalTemplateVariables.WordSelection());
+
+ fContext= new DocumentTemplateContext(fType, new Document(), 0, 0);
+ }
+
+ @Test
+ public void testWithoutParameter() throws Exception {
+ TemplateBuffer buffer= fTranslator.translate("Selected word is ${word_selection}!");
+ fType.resolve(buffer, fContext);
+
+ StringBuffer expected= new StringBuffer();
+ expected.append("Selected word is !");
+ assertBufferStringAndVariables(expected.toString(), buffer);
+ }
+
+ @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);
+ }
+
+ /**
+ * Ensures that {@link TemplateBuffer#getString()} equals the expected String and that all
+ * {@link TemplateBuffer#getVariables()} are resolved and unambiguous.
+ *
+ * @param expected expected result
+ * @param buffer the template buffer
+ */
+ private static void assertBufferStringAndVariables(String expected, TemplateBuffer buffer) {
+ assertEquals(expected, buffer.getString());
+ for (TemplateVariable v : buffer.getVariables()) {
+ assertTrue(v.isResolved());
+ assertTrue(v.isUnambiguous());
+ }
+ }
+}
diff --git a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java
index 5160075d6..422bf62be 100755
--- a/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java
+++ b/org.eclipse.text.tests/src/org/eclipse/text/tests/templates/TemplatesTestSuite.java
@@ -8,7 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
* Jeremie Bresson <jbr@bsiag.com> - Allow to specify format for date variable - https://bugs.eclipse.org/75981
- * Lars Vogel <Lars.Vogel@vogella.com> - Bug 486889
+ * Lars Vogel <Lars.Vogel@vogella.com> - Bug 486889, 486903
*******************************************************************************/
package org.eclipse.text.tests.templates;
@@ -22,8 +22,9 @@ import org.junit.runners.Suite.SuiteClasses;
*
*/
@RunWith(Suite.class)
-@SuiteClasses({
- TemplateTranslatorTest.class,
+@SuiteClasses({
+ TemplateTranslatorTest.class,
+ TemplateVariablesWordSelectionTest.class,
GlobalTemplateVariablesDateTest.class})
public class TemplatesTestSuite {
// intentionally empty
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java b/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java
index 9eb471046..571e6f9a0 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java
@@ -9,6 +9,7 @@
* IBM Corporation - initial API and implementation
* Sebastian Davids: sdavids@gmx.de - see bug 25376
* Jeremie Bresson <jbr@bsiag.com> - Allow to specify format for date variable - https://bugs.eclipse.org/75981
+ * Lars Vogel <Lars.Vogel@vogella.com> - Bug 486903
*******************************************************************************/
package org.eclipse.jface.text.templates;
@@ -71,6 +72,30 @@ public class GlobalTemplateVariables {
return ""; //$NON-NLS-1$
return selection;
}
+
+ @Override
+ public void resolve(TemplateVariable variable, TemplateContext context) {
+ List<String> params= variable.getVariableType().getParams();
+ if (params.size() >= 1 && params.get(0) != null) {
+ resolveWithParams(variable, context, params);
+ } else {
+ // No parameter, use default:
+ super.resolve(variable, context);
+ }
+ }
+
+ private void resolveWithParams(TemplateVariable variable, TemplateContext context, List<String> params) {
+ String selection= context.getVariable(SELECTION);
+ if (selection != null) {
+ variable.setValue(selection);
+ } else {
+ String defaultValue= params.get(0);
+ variable.setValue(defaultValue);
+ }
+ variable.setUnambiguous(true);
+ variable.setResolved(true);
+ }
+
}
/**
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties b/org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties
index c7af24b95..d8d6af7b4 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties
+++ b/org.eclipse.text/src/org/eclipse/jface/text/templates/TextTemplateMessages.properties
@@ -20,6 +20,6 @@ GlobalVariables.variable.description.dollar=The dollar symbol
GlobalVariables.variable.description.date=<b>${id\:date[(format[, locale])]}</b><br>Evaluates to the current date in the specified format and locale. 'format' and 'locale' are optional parameters. 'format' is a pattern compatible with java.text.SimpleDateFormat. 'locale' is an RFC 3066 locale ID.<br><br><b>Examples:</b><br><code>${date}</code><br><code>${currentDate:date('yyyy-MM-dd')}</code><br><code>${d:date('EEEE dd MM yyyy', 'fr_CH')}</code>
GlobalVariables.variable.description.year=Current year. Use the 'date' variable if you want to specify the format.
GlobalVariables.variable.description.time=Current time. Use the 'date' variable if you want to specify the format.
-GlobalVariables.variable.description.user=User name
-GlobalVariables.variable.description.selectedWord= The selected word
+GlobalVariables.variable.description.user=User name
+GlobalVariables.variable.description.selectedWord= <b>${id\:word_selection[('default'])}</b><br>Evaluates to the selected text. 'default' is an optional parameter, if specified it is used as default text if the current text selection is empty. <br><br><b>Examples:</b><br><code>${word_selection}</code><br><code>${currentword:word_selection('Press')}</code><br>
GlobalVariables.variable.description.selectedLines= The selected lines

Back to the top