Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Vogel2016-02-24 11:47:23 -0500
committerLars Vogel2016-02-24 11:50:10 -0500
commit9eed8099525346097ff1b50e9fa317249dc283b4 (patch)
treeeb79ffa1089cb1692054db9cdf3ad33737437be0 /org.eclipse.text
parent4c660fe130091516bd9db3552dbf580f8b209c81 (diff)
downloadeclipse.platform.text-9eed8099525346097ff1b50e9fa317249dc283b4.tar.gz
eclipse.platform.text-9eed8099525346097ff1b50e9fa317249dc283b4.tar.xz
eclipse.platform.text-9eed8099525346097ff1b50e9fa317249dc283b4.zip
Bug 487901 - [templates] Create base class for adjust word_selection and
line_selection variable to avoid code duplication Change-Id: I905c56a7f59e5779f5445fc0d274cdf2189c9887 Signed-off-by: Lars Vogel <Lars.Vogel@vogella.com>
Diffstat (limited to 'org.eclipse.text')
-rw-r--r--org.eclipse.text/src/org/eclipse/jface/text/templates/GlobalTemplateVariables.java69
1 files changed, 26 insertions, 43 deletions
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 087c29861..c70305378 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,7 +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, 487327
+ * Lars Vogel <Lars.Vogel@vogella.com> - Bug 486903, 487327, 487901
*******************************************************************************/
package org.eclipse.jface.text.templates;
@@ -51,20 +51,17 @@ public class GlobalTemplateVariables {
}
/**
- * The word selection variable determines templates that work on a full
- * lines selection.
+ * The base class for text selection variables that allows to return the current text selection. Sub-classes can
+ * define the name and the description of the variable via the constructor
+ *
+ * @since 3.6
*/
- public static class WordSelection extends SimpleTemplateVariableResolver {
+ public static class Selection extends SimpleTemplateVariableResolver {
- /** Name of the word selection variable, value= {@value} */
- public static final String NAME= "word_selection"; //$NON-NLS-1$
-
- /**
- * Creates a new word selection variable
- */
- public WordSelection() {
- super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedWord")); //$NON-NLS-1$
+ public Selection(String name, String description) {
+ super(name, description);
}
+
@Override
protected String resolve(TemplateContext context) {
String selection= context.getVariable(SELECTION);
@@ -95,14 +92,30 @@ public class GlobalTemplateVariables {
variable.setUnambiguous(true);
variable.setResolved(true);
}
+ }
+
+ /**
+ * The word selection variable determines templates that work on a full
+ * lines selection.
+ */
+ public static class WordSelection extends Selection {
+ /** Name of the word selection variable, value= {@value} */
+ public static final String NAME= "word_selection"; //$NON-NLS-1$
+
+ /**
+ * Creates a new word selection variable
+ */
+ public WordSelection() {
+ super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedWord")); //$NON-NLS-1$
+ }
}
/**
* The line selection variable determines templates that work on selected
* lines.
*/
- public static class LineSelection extends SimpleTemplateVariableResolver {
+ public static class LineSelection extends Selection {
/** Name of the line selection variable, value= {@value} */
public static final String NAME= "line_selection"; //$NON-NLS-1$
@@ -113,36 +126,6 @@ public class GlobalTemplateVariables {
public LineSelection() {
super(NAME, TextTemplateMessages.getString("GlobalVariables.variable.description.selectedLines")); //$NON-NLS-1$
}
- @Override
- protected String resolve(TemplateContext context) {
- String selection= context.getVariable(SELECTION);
- if (selection == null)
- 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);
- }
}
/**

Back to the top