Bug 487327 - [templates] Allow specifying a default value in case
the line_selection is empty

Still WIP, awaiting feedback from Dani for
https://bugs.eclipse.org/bugs/show_bug.cgi?id=486903#c18

Change-Id: I6868dced6122a5479d9f786be6f8955011fbec82
Signed-off-by: Lars Vogel <Lars.Vogel@vogella.com>
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 571e6f9..087c298 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
+ *     Lars Vogel <Lars.Vogel@vogella.com> - Bug 486903, 487327
  *******************************************************************************/
 package org.eclipse.jface.text.templates;
 
@@ -120,6 +120,29 @@
 				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 253bb5e..0ef52b8 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
@@ -22,4 +22,4 @@
 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= <b>${id\:word_selection[(default)]}</b><br>Evaluates to the selected text. 'default' is an optional parameter, which specifies the text if the selected text is empty. <br><br><b>Examples:</b><br><code>${word_selection}</code><br><code>${currentWord:word_selection(myStringVariable)}</code><br><code>${currentWord:word_selection('"Press me"')}</code><br>
-GlobalVariables.variable.description.selectedLines= The selected lines
+GlobalVariables.variable.description.selectedLines= <b>${id\:line_selection[(default)]}</b><br>Evaluates to the selected text for multiple lines. 'default' is an optional parameter, which specifies the text if the selected text is empty. <br><br><b>Examples:</b><br><code>${line_selection}</code><br><code>${currentLine:line_selection(myStringVariable)}</code><br><code>${currentLine:line_selection('"A default text"')}</code><br>
\ No newline at end of file