From 9e178f2b274b2e8d498ab3c9d86df474fa0ab76a Mon Sep 17 00:00:00 2001 From: Lars Vogel Date: Sun, 31 Jan 2016 10:33:24 +0100 Subject: Bug 486903 - [Templates] Allow specifying a default value in case the word_selection is empty Change-Id: I5a5c2e77f65aa45474362838aa71fc351779dcd7 Signed-off-by: Lars Vogel --- .../text/templates/GlobalTemplateVariables.java | 25 ++++++++++++++++++++++ .../text/templates/TextTemplateMessages.properties | 4 ++-- 2 files changed, 27 insertions(+), 2 deletions(-) (limited to 'org.eclipse.text/src/org/eclipse/jface/text/templates') 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 9eb4710464e..571e6f9a077 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 - Allow to specify format for date variable - https://bugs.eclipse.org/75981 + * Lars Vogel - 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 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 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 c7af24b9544..d8d6af7b469 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=${id\:date[(format[, locale])]}
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.

Examples:
${date}
${currentDate:date('yyyy-MM-dd')}
${d:date('EEEE dd MM yyyy', 'fr_CH')} 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= ${id\:word_selection[('default'])}
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.

Examples:
${word_selection}
${currentword:word_selection('Press')}
GlobalVariables.variable.description.selectedLines= The selected lines -- cgit v1.2.3