Home > Plug-in Development > How to manage the refactoring provider appearance and sub menus in the Popup menu

How to manage the refactoring provider appearance and sub menus in the Popup menu

September 30th, 2011

The Refactor!/CodeRush Popup menu shows available refactoring and code operations in the current context. When you develop a DXCore plug-in with a refactoring provider component, you might need to tweak its appearance in the popup menu. We will see how to change the caption of a refactoring menu item and the text of the hint describing a refactoring operation. Also, we will learn how to add and manage sub menu items for the popup menu.

Once you have created a DXCore plug-in containing the refactoring provider component and subscribed to its events, you may change the caption of the refactoring provider in the Refactor! popup menu using the CheckAvailability event handler. There are two properties we are interested in:

  • MenuCaption – gets or sets the text that will appear in the Refactor! menu for this refactoring item.
  • HintText – gets or sets the text that will appear in the description hint for the refactoring item when one is selected in the menu.

Here is sample code of the CheckAvailbility event handler:

private void refactoringProvider1_CheckAvailability(object sender, CheckContentAvailabilityEventArgs ea)
{
  ea.MenuCaption = "Menu Caption Test";
  ea.HintText = "Using the HintText property of a RefactoringProvider component, " +
"you can specify its description, which appears in the information BigHint.";

  ea.Available = true;
}

Sub Menu Items

Sub menu items allow you to specify several options for a refactoring or code operation provider. For example, when you add a contract for a method using the Add Contract code provider, you have several options accessible via the sub menu items:

Refactoring Provider Sub Menus sample choices

Sub menu items should be initialized inside the CheckAvailabilty event handler. Once you add several items and execute one of them, an instance of the selected sub menu item is passed to the Apply event handler.

You can add as many items as you require. To add a new item, use the AddSubMenuItem method of the CheckContentAvailabilityEventArgs instance like this:

private void refactoringProvider1_CheckAvailability(object sender, CheckContentAvailabilityEventArgs ea)
{
  ea.AddSubMenuItem("This is a sub menu item #1");
  ea.AddSubMenuItem("This is a sub menu item #2");
  ea.AddSubMenuItem("This is a sub menu item #3");

  ea.Available = true;
}

There are several overloads of the AddSubMenuItem method, which allow you to specify different properties of the sub menu item. A sub menu item has the following properties:

  • Name – the name of the sub menu item. This is a required parameter – it allows you to recognize the selected item inside the Apply event handler.
  • Caption – the caption of the sub menu item in the Refactor! Popup menu. This is an optional parameter. If not specified, the Name property will be used.
  • HintText – the text that appears in the description hint when the sub menu item is selected in the menu. This is an optional parameter. If not specified, the hint will contain the description of the refactoring or code provider.

When sub menu items are added inside the Apply event handler, use the SelectedSubMenuItem property of the ApplyContentEventArgs instance to obtain the selected sub menu item:

private void refactoringProvider1_Apply(object sender, ApplyContentEventArgs ea)
{
  SubMenuItem selectedSubMenuItem = ea.SelectedSubMenuItem;
  if (selectedSubMenuItem != null)
  {
    MessageBox.Show("Sub menu item is selected: " + selectedSubMenuItem.Caption);
  }
}

Bear in mind that the SelectedSubMenuItem property may return null if no sub menu item is selected. In this case, the refactoring was performed by applying the parent refactoring provider menu item:

DXCore Refactoring Provider Sub Menus sample

The sample project is attached (7,639 bytes, Visual Studio 2010, C#).

—–
Products: DXCore, Refactor! Pro
Versions: 11.1 and up
VS IDEs: any
Updated: Sep/30/2011
ID: D119

Similar Posts: