Creating a simplest refactoring using the RefactoringProvider control
In this article we will create the simplest refactoring which will remove the active comment. Sometimes, there are comments left that are not utilized any longer, such as implemented TO-DO comments, UNDONE comment that is now complete, etc. We will use the RefactoringProvider control shipped in DevExpress Refactor! Pro for this purpose.
Here are the steps we need to perform:
1. Create a new DXCore plug-in
2. Drop the RefactoringProvider component from the Toolbox:
3. Fill its properties:
- ProviderName (e.g. “Remove Comment”)
- Description (e.g. “Completely removes this comment and its neighboring comments”)
- AutoUndo (true)
4. Subscribe to the CheckAvailability, Apply and PreparePreview events:
5. Add the following code into event handlers:
CheckAvailability event handler
To indicate that the refactoring provider is available in the current context, we need to set the corresponding Available property. This refactoring should be available when the active code element is a comment, so we simply check if the element is of the Comment type:
private void refactoringProvider1_CheckAvailability(object sender, CheckContentAvailabilityEventArgs ea) { ea.Available = ea.Element is Comment; }
Apply event handler
In the Apply event handler we are going to remove the comment with its neighboring comments and surrounding white space characters including empty lines. To do this, we get the active comment, then calculate its cut range for removal using the special SourceRange methods, and, finally, deleting the calculated range:
private void refactoringProvider1_Apply(object sender, ApplyContentEventArgs ea) { SourceRange cutRange = GetCutRange(ea.Element as Comment); ea.TextDocument.DeleteText(cutRange); }
GetCutRange method:
private SourceRange GetCutRange(Comment comment) { Comment startComment; Comment endComment; comment.GetFirstAndLastConnectedComments(out startComment, out endComment); BlockElements blockElements = BlockElements.AllWhiteSpaces | BlockElements.SupportComments; return new SourceRange(startComment.GetFullBlockCutRange(blockElements).Start, endComment.GetFullBlockCutRange(blockElements).End); }
PreparePreview event handler
This event handler is required if you would like to show a preview hint for the refactoring. We will strike-through the cut range of a comment:
private void refactoringProvider1_PreparePreview(object sender, PrepareContentPreviewEventArgs ea) { SourceRange cutRange = GetCutRange(ea.Element as Comment); ea.AddStrikethrough(cutRange); }
Now, compile the plug-in and see it in action:
The sample code is attached (14,098 bytes, Visual Studio 2010, C#).
—– Products: DXCore, Refactor! Pro Versions: 11.1 and up VS IDEs: any Updated: Sep/30/2011 ID: D118Similar Posts:
- How to manage the refactoring provider appearance and sub menus in the Popup menu
- How to use CodeMetricProvider control to build you own code metric
- Creating a new code issue and its fix for Java Script
- How to perform an existing refactoring from another one
- Refactor! Pro/DXCore components – Refactoring provider