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
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.
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:
…And here is what you get when you expand the NewCodeProvider template
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.
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.
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
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:
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.
- 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
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.
As you might imagine, the ContentProvider_Execute method is where you perform the main function of your ContentProvider.
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.