Selection Embedding with CodeRush

Selection Embedding is one of those CodeRush features that doesn’t get a lot of mention, but it is extremely powerful.

It provides the user with the ability to wrap some text (code, mark-up, comments) around some existing code already in place.

Selection Embedding is available from your the Code section of your CodeRush SmartTag menu (As well as via several custom shortcuts)

Each Selection Embedding is context sensitive, so that only those for the current document type are shown to you.

Below is an example of the selection embeddings available from within the C# editor.

SelectionEmbeddingCSharp

These are produced when I highlight some text in the HTML editor.

cSelectionEmbeddingHTML

Selection Embeddings do not have to be picked from the SmartTag menu. As mentioned, some have associated shortcut keys.

Try highlighting 1 or more lines of C# code and then …

…pressing T causes your code will be wrapped in a try..catch..finally block.
…pressing C causes your code will be wrapped in a try..catch block.
…pressing F causes your code will be wrapped in a try..finally block.
…pressing B or { causes your code will be wrapped in a brace (ie { and } ) block.
…pressing ( or ) causes your code will be wrapped in parenthesis.
…pressing R causes your code will be wrapped in a #region…#endregion block.
…pressing U causes your code will be wrapped in a using block.
…pressing W causes your code will be wrapped in a waitcursor block.

Some of these are pretty simple wrapping effects. Others put CodeRush fields and text commands in place to enhance your experience further.

Here the using embedding places your caret within the using expression so you can more easily fill it out.
Additionally, when you press enter, your caret is moved to the bottom of the expression ready for further code.

SelectionEmbeddingUsing

Here CodeRush provides a specialized try..finally block…

SelectionEmbeddingWaitCursor

  • Saves your current cursor
  • Sets your cursor to the WaitCursor.
  • Executes your original code.
  • Restores your cursor to it’s original state, regardless of the success of your code.

This is all boiler-plate code, but it’s boiler-plate code you don’t have to write yourself. This reduces the chance of typos or, worse still, logic errors.

As you can see, Selection Embedding can be a very powerful tool, providing you with boiler-plate code in a fraction of the time, with fewer keystrokes and reducing the chance for typewritten errors across the board.

As with most CodeRush features, Selection Embedding is user extensible.

In my next post, I’ll show you how to make your own Selection Embeddings.

no comments
No Comments

Please login or register to post comments.