Mark Miller

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:

RefactoringMenu

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.
Published Jun 22 2011, 09:16 AM by
Bookmark and Share

Comments

Tom Allard

Allready learned a lot. However i think there is a lot of stuff you need to know about the api before you can make great plugins. Maybe it is a good idea to, as long as not everything is in the eventargs, to make a small api reference for common things you will need to do a lot when making refactorings/codeproviders. (Like when to use ParserServices or that you need to use Resolve to get to the type of an initializer member argument). Also integration of dxcorecommunityplugins (like VS extension manager) in refactor/coderush would be awesome.

June 22, 2011 1:46 PM

Mark Miller (DevExpress)

Tom, your feedback about the less discoverable aspects about the DXCore has been heard and we're already talking about how to improve this in 11.2. Thanks for your support.

June 23, 2011 5:01 PM

About Mark Miller (DevExpress)

Mark Miller is a C# MVP with strong expertise in decoupled design, plug-in architectures, and great UI. Mark is Chief Architect of the IDE Tools division at Developer Express, and is the visionary force behind productivity tools like CodeRush and Refactor!, as well as the DXCore extensibility layer for Visual Studio. Mark is a popular speaker at conferences around the world and has been writing software for over two decades.
LIVE CHAT

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 info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. 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-2016 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners