Bug 245788 - [templates] Add a template variable ${selection} and allow Surround With action to deal consider it
Summary: [templates] Add a template variable ${selection} and allow Surround With acti...
Status: ASSIGNED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 3.4   Edit
Hardware: All All
: P5 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: JDT-Text-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-08-31 13:04 EDT by Sandip Chitale CLA
Modified: 2019-09-06 16:18 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sandip Chitale CLA 2008-08-31 13:04:34 EDT
There are two template varaibles:

${word_selection}
${line_selection}

There needs to be an third variable called:

${selection}

This basically replaces whatever the text selection in was when the template was about to be inserted.

Also to be useful the Surround with... action should use templates that contains the ${selection} variable. That is because it should be possible to insert
such templates using menu item, keybingd or mouse only operation such that the 
selection is not destroyed in the process of invoking the action to select the template to insert.

In fact why is Surround with... action limited to templates containing ${line_selection} only? Why not ${word_selection}?

Here is the implementation of the ${selection} resolver:

package somepackage;

import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateVariableResolver;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.texteditor.ITextEditor;

public class SelectionVariableResolver extends TemplateVariableResolver {
	protected String resolve(TemplateContext context) {
		 IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
		if (editorPart instanceof ITextEditor) {
			ITextEditor textEditor = (ITextEditor) editorPart;
			ISelection selection = textEditor.getSelectionProvider().getSelection();
			if (selection instanceof ITextSelection) {
				ITextSelection textSelection = (ITextSelection) selection;
				return textSelection.getText();
			}
		 }
		return getType();
	}
}

and the extension:

   <extension
         point="org.eclipse.ui.editors.templates">
      <resolver
            class="somepackage.SelectionVariableResolver"
            contextTypeId="java"
            description="Resolves to the selection."
            icon="icons/selection.gif"
            name="selection"
            type="selection">
      </resolver>
   </extension>
Comment 1 Sandip Chitale CLA 2008-08-31 13:40:53 EDT
Come to thik of it this is not JDT specific. The ${selection} template variable should be supported in all text editors.
Comment 2 Dani Megert CLA 2008-09-01 03:12:01 EDT
>In fact why is Surround with... action limited to templates containing
>${line_selection} only? Why not ${word_selection}?
Because 'Surround With' is currently only implemented for the Java editor where it makes sense to only surround blocks/lines.

Can give some concrete use cases where this addition would be useful?

Note that you can add that on top of Eclipse by declaring the template variable resolver in your own plug-in.

You could then contribute this back to the Eclipse project by attaching a patch
here.
Comment 3 kaspar CLA 2009-01-13 05:31:24 EST
I would absolutely undeline the need of such a Variable! It would be great to have the possibility to copy a Variable and use in in a template.
Comment 4 Eclipse Webmaster CLA 2019-09-06 16:18:37 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.