How-To's

Extend/Shrink Selection

Note: This is the first post from a series that we’re hoping to maintain on a regular basis. It will cover specific features provided by ReSharper with reasonable (or even redundant, for true ReSharper Jedis) details, links to other valuable web resources and probably some additional insight. You can think of it as extended online help for those users who feel not very comfortable learning or navigating through ReSharper functionality, as well as for those eager to gain additional expertise.

The Extend Selection feature allows you to successively select expanding blocks of code. You can easily select any expression in the code by clicking somewhere inside it and pressing Ctrl + W (or Ctrl+Alt+Right Arrow in the Visual Studio keyboard layout) a few times, or, alternatively, by choosing ReSharper | Code | Extend Selection.

To be specific, Extend Selection works the following way:

  1. You place the caret at an arbitrary position – in this example, at a field name in a method call.
  2. You press Ctrl + W for the first time.
      • If you selected Use CamelHumps in ReSharper | Options | Editor, the initial selection is made covering the current part of field name that begins with an uppercase character:

        It’s only after you’ve pressed Ctrl + W once more, the whole current word is selected.
    • If CamelHumps is disabled, ReSharper selects the complete field name straight away:
  3. Press Ctrl + W again. ReSharper extends the selection to cover the field property:
  4. The next time you hit the shortcut, the method name is added to the selection – without parentheses though:
  5. Next expansion – parentheses are added:
  6. We’re done with the method call expression. The next time you press Ctrl + W, the whole line is selected:
  7. How the feature acts further depends on the structure of your code. In our example, the method call belongs to the body of a conditional statement. That’s why the next time you hit the shortcut, the whole body is selected, excluding braces:

In this example, consequent keystrokes extend the selection to cover the following:

  • Body of the conditional statement including braces;
  • The conditional statement including condition;
  • Body of the method containing the conditional statement;
  • The entire containing method;
  • The class that this method belongs to;
  • The entire file.

Obviously enough, Shrink Selection does exactly the opposite. Press Ctrl + Shift + W (or Ctrl+Alt+Left Arrow in the Visual Studio keyboard layout) or choose ReSharper | Code | Shrink Selection a number of times, and you can eventually find your selection cut down to the initial position of the caret in strictly reverse order.

This feature is very intuitive but please be aware that there are several scenarios where it should work on a limited basis or not work at all.

    • ReSharper doesn’t process HTML and JavaScript. Because of that, Extend/Shrink Selection interprets any block of JavaScript or HTML code as an integral entity within a pair of tags and only discerns specific words within this block. In other words, when you’re working on an aspx file and you place the caret at a certain position within a JS or HTML block of code and you hit Ctrl + W, ReSharper selects the current word, and upon the next keystroke it selects the entire block of code within a pair of containing tags (for example, in case of JavaScript).
    • You should start with extending a selection using Ctrl + W in order to be able to shrink it back later. In other words, if you made a random selection with your mouse or by clicking Ctrl + A, you can’t shrink it using Ctrl + Shift + W because in this case ReSharper doesn’t know exactly what file member you want to cut the selection to.
  • Extend/Shrink Selection supports CamelHumps-based selections within both C# comments (//, ///, /**/) or VB.NET comments. However, commented code is not parsed and therefore interpreted as an integral entity (see note on JavaScript and HTML above).
Technorati tags: c#, vb.net, features, Extend Selection, Extend/Shrink Selection, Shrink Selection, ReSharper in Detail, ReSharper 3.1
image description