Using Word_Selection in an Eclipse Template

Eclipse Templates can save a ton of typing. One of the reasons they are so versatile is their support for variables like ${word_selection}. As its name implies, the Word_Selection variable evaluates the content of the current text selection. Here's a list of all template variables from the Eclipse Help Guide.

Let's jump to the end of the story as I often like to do. Here is the final code we'll be generating, where we'll be wrapping a MessageSource.Format([STRING], ); around our [STRING] word_selection.

Now let's look at the template which we named msource. Side Note: I place any import statements at the end of the template so the generated code is on the same line as my editor cursor.

Using the ${Word_Selection} Variable

This is primarily why I wrote this post, cause the first time using a Word_Select variable can be tricky. We enter the text we're going to select AND THEN SELECT IT WITH

ALT-SHIFT UP/DOWN

That's important, thus the caps. Now activate Context Assist with

CTRL-SPACE

which you might have to enter twice depending on your Template Type display order. Then press [ENTER.]

The immediate result is shown below, with our cursor placed after the “,”.

Here's the configuration of our msource MessageSource template where we add it to the Java Statement Context. That's also important to remember if you want it to appear in the Content Assist template lists on CTRL-SPACE.

Posted December 12, 2015 01:48 PM EST

More Like This Post