Saturday, October 16, 2010

Commenting with ReSharper 5 and Visual Studio 2010

No doubt, ReSharper is great. ReSharper 5 for with Visual Studio 2010 is even greater.

Unfortunately with the new default keyboard shortcuts in ReSharper 5 at least one convenient shortcut has been changed.

In previous versions of ReSharper, by pressing Ctrl+7 (not the function key F7!)  the current line (or lines if several were selected) have been commented out using the double slash single line comment. If the selected lines already were commented out, that slashes had been removed.

Bookmarking overruled commenting

With ReSharper 5 by default the shortcut Ctrl+7 is assigned to a new feature – bookmarking. To go to a specific numbered bookmark you now can press any the keys 0 to 9 while holding down the Ctrl-key.

So when you first try to use your line commenting feature by pressing Ctrl+7 as you’ve grown accustomed to, you will get that annoying hint:

Could not go to bookmark #7. Bookmark is not set.

Could not go to bookmark #7. Bookmark is not set.

If you’re like me you’ll probably like to change that shortcut back to the function it provided with earlier version of ReSharper.

Changing Visual Studio’s keyboard shortcuts

Thanks to Visual Studios extensibility model all the keyboard shortcuts, even those provided by third party addins such as ReSharper, can be manipulated in Visual Studio’s dedicated dialog.

You can open that dialog via Tools – Options – Keyboard:

Visual Studio's options dialog with selection of "Keyboard"-section

If you want to see what commands currently are assigned to a specific keyboard-shortcut, you simple have to set the focus to the textbox labeled “Press shortcut keys:”

image

Here you see the guilty, the command ReSharper_Bookmarks_GoToBookmark7. Now you might think it should be straight forward to reassign that shortcut to our beloved line comment feature.

But – before you get frustrated mind the following: the currently assigned command is not displayed in a simple textbox, but in a combobox. That gives us the hint that in Visual Studio a single shortcut can be applied to several commands. That means that just assigning another command to a shortcut will in general not replace the old assignment, but add to it.

This feature does make sense to assign a shortcut to several specific commands depending on the context that shortcut is used. That context can be specified by selecting an entry in the drop down “Use new shortcut in:”. By default the entry “Global” is used indicating that the shortcut applies to all kinds of editors, be it text or graphical editors. For example with selecting the proper entry you can constrain the assignment of a shortcut to a specific command for the use in some special editor as the editor for UML activity diagrams, the XML schema designer or just the text editor.

Step by step instructions

So let’s go back to our original intention, restoring the shortcut Ctrl+7 to ReSharper line comment feature:

  1. Remove the current assignment
    1. Find the name of the currently assigned command
      Press the shortcut while the focus is on the field “Press shortcut keys:” (Ctrl+7). The current shortcut assignments are shown in “Shortcut currently used by:”
    2. Select the currently assigned command in the command list
      Select the command in the command list (ReSharper_Bookmarks_GoToBookmark7 in our case). You might want to use the filter text box to narrow-down the list (for example “rk7”).
    3. Select the shortcut to remove
      In the dropdown “Shortcuts for selected command:” select the command you want to remove.
      In our case this is “Ctrl+7 (Global)”
    4. Remove the assignment
      Press the button labeled “Remove”
  2. Add the new assignment
    1. Select the command to be assigned in the command list
      In our case this is “ReSharper.ReSharper_LineComment”. Use “comment” for filtering to find that entry quicker.
    2. Assign the command to the shortcut key
      Make sure the desired shortcut (Ctrl+7 for our case) is still shown in “Press shortcut keys:” and klick the button labeled “Assign”

Hope that helps some needy soul in despair. :)

No comments:

Post a Comment