New CodeRush Feature: Quick Pair (Parens, Brackets, Quotes, etc.) for Visual Studio

01 June 2011

Today was the first CodeRush Feature Wednesday webinar presented by Rory Becker and myself. And for our first feature we decided to implement something I wanted – a better way to enter paired delimiters in code. When entering paired delimiters, there seem to be a number of common scenarios:

  • Enter the pair and move on (for example, parens in a C# method call that takes no arguments)
  • Enter the pair and place the caret between the pair (but then you have the challenge of moving outside the pair)
  • Surround a selection with the pair

To implement this feature, in the webinar we created a new Action called QuickPair which accepts leading and trailing delimiters, and an optional third parameter to determine whether the caret goes inside the delimiters or to the right outside the delimiters.

You can download and install the feature here:

http://code.google.com/p/dxcorecommunityplugins/wiki/CR_QuickPair

This plug-in includes shortcut bindings based on the US keyboard. You may need to change these bindings to match your keyboard locale settings. For example, on a US keyboard, the parens are located above the 9 and 0 keys, respectively, like this:

90

 

The curly braces are above the brackets:

Brackets

 

The angle brackets are above the comma and period keys:

AngleBrackets

 

And the double-quote is above the apostrophe (and the key to the left is the semi-colon):

Quote

 

With each of these key pairs, we’ve assigned two functionalities. The left key (e.g., Ctrl+[ ) places the caret between the two delimiters (adding a field, so you can just press Enter to get to the right of the delimiter pair), and the right key (e.g., Ctrl+] ) places the caret after the two delimiters (no fields needed). If there’s a selection, the left key will embed the selection within the delimiters (maintaining the original selection), and the right key will embed the selection within the delimiters, extending the selection to include the delimiters. Examples of all the bindings created in the webinar appear below.

Given these key positions, the table below shows available functionality (the “|” character indicates the caret position and the blue background indicates the selection, after code is inserted):

On a US Keyboard, press this: to get this:
(no selection)
or to modify a selection:
Ctrl+(   (|)   (My Selection)
Ctrl+)   ()|   (My Selection)
Ctrl+[   [|]   [My Selection]
Ctrl+]   []|   [My Selection]
Ctrl+Shift+{   {|}   {My Selection}
Ctrl+Shift+}   {}|   {My Selection}
Ctrl+Shift+<   <|>   <My Selection>
Ctrl+Shift+>   <>|   <My Selection>
Ctrl+;   "|"   "My Selection"
Ctrl+"   ""|   “My Selection”
Ctrl+=   (|) =>   (My Selection) =>

You may notice a lack of symmetry with the Ctrl+= binding (there is no corresponding binding to place the caret to the right of the lambda operator). I omitted this binding due to a lack of a suitable neighboring key to the equals sign. If you want to correct this asymmetry and add your own binding, feel free to. After installing this plug-in, you may view or change the shortcuts bindings in the IDE\Shortcuts options page. The QuickPair bindings are in the Code\QuickPair folder:

QuickPairBindings

Have a request for a new feature? Email roryb@devexpress.com and let us know what you’d like to see us build for you on CodeRush Feature Wednesdays.

3 comment(s)
Bob Archer

Don't smart brackets give you the equiv of Ctrl+( and Ctrl+[ ?

What I would like to see is if I just type a close ) or ] and it sees that there is no un-opened version of that block type it puts the open before it and gives me the caret after it. So rather than use Ctrl+) I can just type ).

Oh, and for bonus points, when I choose a function from intellesense and press a . I would like coderush to add the prenes if I chose a function rather than a property.

BOb

2 June, 2011
Robert Leahey

Thanks Mark, the webinar was great.

2 June, 2011
Mark Miller (DevExpress)

Bob Archer: It's certainly possible to add a missing leading paren when the closing paren is pressed. Maybe we'll write that in a future CodeRush Feature Workshop (same for your suggestion about automatically adding parens when a method is selected in Intellisense).  

2 June, 2011

Please login or register to post comments.