Paul Robertson's words, punctuated

Thoughts on development, user-centered design, code, etc. by Paul Robertson

Flash Builder 4.5 Tip: Using "selection" templates

I’ve already written about how to use the code templates feature in Flash Builder 4.5. One less-obvious aspect of templates is that some templates behave differently if you have some characters or lines selected.

For example, suppose you have one or more lines of code you’ve entered, and now you realize you want them to be wrapped in an if statement.

First select the line(s) you want to wrap:

A line of code selected in the editor.

Next hit Ctrl+Space to trigger the code hint menu, then again to switch to the template code hints:

Open the template code hints.

Choose the template name with the mouse, by using the arrow keys, or by typing the name of the template. (Sometimes it looks like the characters you’re typing replace the selected code, but don’t worry–your selection will reappear once you choose the template.)

Type to choose the template.

Accept the code hint, and the template appears with your selection in its proper place:

The template is entered in the editor.

If you want to create your own templates that support selections, there are two template variables you can use. Use the ${line_selection} variable for one or more lines of code. For example, I created this template to wrap any lines of code in curly braces:

{
    ${line_selection}
}

This also works with MXML. For example, here is a template I created to wrap a block selection in an MXML tag:

<${tag_name}>
    ${line_selection}
</${tag_name}>

You can use the ${word_selection} variable in the same way if you want a template for surround a selection that is only part of a line of code. However, the ${word_selection} variable currently isn’t as useful as it could be, because templates with the ${word_selection} variable only show up in the code hints at the first character of a line.

Comments