Mark Miller

June 2011 - Posts

  • Advanced CodeRush Plug-ins: Converting an Object Initializer into a Constructor

    Today Rory Becker and I presented an Advanced CodeRush Plug-ins session, showing how to create CodeProviders and RefactoringProviders. These providers show up on the CodeRush/Refactor menu, like this:


    The session included several moments where we tasted sweet success, and at least one tough coding segment where we were pushing through some challenging code.

    The new feature converts code like this:

      Box box = new Box() { Height = 33, Width = 44, Depth = 22 };

    into this:

      Box box = new Box(33, 44, 22);

    and creates a constructor if needed.

    At the end of the two-hour session, we were left with a refactoring that worked, but a CodeProvider that needed more effort. The CodeProvider was challenging because it needed to modify code in two places (it is adding a constructor and replacing the object initializer with a call to new constructor), potentially in two files.

    Here’s an overview of the points we learned in the session:

    1. The Expressions Lab can reveal many of the hidden details behind the source code you want to analyze or generate.

    2. The ObjectCreationExpression class represents a call to a constructor.

    3. ObjectCreationExpression elements have three interesting properties:

    • Arguments – the parameters to the constructor call, if any
    • ObjectType – a reference to the type being constructed
    • ObjectInitializer – an ObjectInitializerExpression (if the constructor call includes an initializer).
    4. ObjectInitializerExpression has one interesting property:
    • Initializers – a collection of MemberInitializerExpressions
    5. MemberInitializerExpression has two interesting properties:
    • Name – the property name being assigned.
    • Value – the value assigned to the property in the initializer expression.
    6. MemberInitializerExpression also has an interesting method, Resolve, which allows us to determine the type of the initializer argument.

    7. We can use the FileChange object to queue up changes to multiple files.

    At the end of the webinar, we encountered an issue where the CodeProvider wouldn’t generate any code. The problem was due to this line of code which set the source file Path property of a FileChange instance to the folder holding the text document (but not the actual file name):

      newConstructor.Path = CodeRush.Documents.ActiveTextDocument.Path;

    it should have been this:

      newConstructor.Path = CodeRush.Documents.ActiveTextDocument.FileNode.Name;

    Correcting the two places where we set FileChange’s Path property enabled the plug-in to generate code correctly. We’ll record a part 2 to this webinar where we’ll see this fix and wrap up all remaining issues:

    1. Fix the FileChange code generation.
    2. Add code that transfers parameters to the properties.
    3. Declare parameters in the developer’s preferred style.
    4. Test, and handle any new issues found.
  • New CodeRush Feature: Quick Pair (Parens, Brackets, Quotes, etc.) for Visual Studio

    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:

    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:



    The curly braces are above the brackets:



    The angle brackets are above the comma and period keys:



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



    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:


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


Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at or call us at +1 (818) 844-3383


DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, Delphi, HTML5 or iOS & Android development. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2018 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners