Rory Becker - DevExpress CodeRush Blog

New CodeRush Plugin Templates – NewCodeProvider and NewRefactoring

Note: The templates referred to by this post do not ship with CodeRush. To install them, see instructions towards the end of the post.

This post describes two more templates in my Plugin Templates series. The series itself is introduced in my previous post - So you want to write a CodeRush plugin

PurposeCodeRushSmartTagMenu

The NewRefactoring and NewCodeProvider templates are the single quickest way to get started creating Refactoring and CodeProvider(Collectively called ContentProviders) plugins

These types of plugins add items to the Red (Refactoring) and Blue(Code) sections of the CodeRush SmartTag menu.

Usage

The templates work very similarly to My previous NewAction template in terms of placement. ie You should expand them in the body of your plugin class and then call the generated register methods from within your plugin’s the InitializePlugin method

This is how the NewRefactoring template expands:

TemplateNewRefactoring

…And here is what you get when you expand the NewCodeProvider template

TemplateNewCodeProvider

Note: As before, you’ll need to call the appropriate registration method from your InitializePlugin method

You can see they are structurally very similar. they both create Register, CheckAvailability and Apply methods. In each case there is a small amount of customization to be done in order to get your ContentProviders up and running.

You’ll have to give your ContentProvider 3 names.

  • Your own reference.
    The first name is your own reference. The phrase you type here is used to name the objects and associated methods of your ContentProvider.
  • Internal name
    This value is used internally by CodeRush. It should be unique, since it will be used to add  your ContentProviders to an internal dictionary. Later you can use this name to get a reference your ContentProvider from another plugin.
  • Display Name
    This value is used for the text displayed when your provider is added to the CodeRush SmartTag menus

Next you’ll have to provide the code for:

  • Your CheckAvailability method
  • Your Execute method.

CheckAvailability

Availability is about determining if it is currently appropriate for your ContentProvider to be present on the CodeRush SmartTag menu.

Your CheckAvailability method is where you do this. In the unlikely event that your Refactoring\CodeProvider is always supposed to be available, simply leave the code as it is.

ContentProvider_CheckAvailability

In most cases, your ContentProvider will only make sense under certain circumstances. If these circumstances are not met, then it makes more sense to simple not show your ContentProvider in the SmartTag menu.

Consider the “Split String” refactoring. This refactoring is designed to allow you to change

HelloWorldString

…Into…

HelloWorldStringSplit

It’s clear, there is no point in providing this Refactoring, unless the user’s caret is within a string.

Thus the SplitString_CheckAvailability method might look something like this:

SplitString_CheckAvailability

See how it only sets ea.Available to true if the ActiveString is not null. ie The caret is on a string literal.

Of course you should feel free to make your determination in any way you see fit.

You could:

  • Analyse the code where the caret is.
  • Analyse the project or the files within it.
  • You could base your own availability on the availability of another CodeProvider.
  • Look for code smells or other patterns.

If you really want to, you could call out to the web, and check a webcam to see if there’s a blue moon Smile

Just keep in mind that the check you perform here, will be performed quite often as the user moves around the code. This code needs to be as fast as possible.

Execute

As you might imagine, the ContentProvider_Execute method is where you perform the main function of your ContentProvider.

You might:

The sky really is the limit.

Ok Cool, so how do I get these templates?

The NewRefactoring and NewCodeProvider templates are available in the CodeRush Plugin Templates repository on GitHub

You can import them the traditional way

OR you can use the Template Importer and point it at

  • This URL for the NewRefactoring template
  • This URL for the NewCodeProvider template

If you have any suggestions to improve any of the templates in this series, feel free to contact me via email or on twitter.

If you so inclined you could even fork the project, make some changes and send me a pull request.

Published Mar 04 2014, 05:13 PM by
Bookmark and Share

Comments

Robert Leahey

Hi Rory,

Under "Usage" the image that is supposed to be showing the NewRefactoring template is actually showing the NewCodeProvider template.

March 5, 2014 12:07 PM

Rory Becker (DevExpress)

Thanks Robert. Updated now :)

March 5, 2014 12:25 PM
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-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners