Home > Plug-in Development > How to create shortcuts for refactorings that require a sub-menu choice

How to create shortcuts for refactorings that require a sub-menu choice

March 20th, 2012

There is a built-in capability in CodeRush/DXCore to create shortcuts for any type of content providers, such as refactoring and code providers. Just choose the Refactor command and type in a provider’s name as a parameter. However, this approach does not work for refactorings that require a sub-menu choice; for example:

DevExpress Sort Namespace Reference refactoring preview

Here, you must supply not only a refactoring’s name, but also a sub-menu’s name (that is usually equal to its caption). The Refactor command does not support sub-menus, but you can easily add this option via a new DXCore action. Follow the steps below to create a plug-in containing this action:

1. Create a new DXCore plug-in;
2. Drop the DXCore Action control on the designer surface;
3. Set the ActionName property value; e.g., “ExecuteProvider“:

DevExpress DXCore Action properties

4. Add two non-optional parameters of the String type; e.g., “ProviderName” and “SubMenuName“:

DevExpress DXCore Action parameters

5. Subscribe to the Execute event of the Action component.

Now, it’s time to write several lines of code in the Execute event handler:

private void action1_Execute(ExecuteEventArgs ea)
{
  // retrieve passed-in parameter values:
  string providerName = ea.Action.Parameters.GetString("ProviderName");
  string subMenuName = ea.Action.Parameters.GetString("SubMenuName");
  if (String.IsNullOrEmpty(providerName))
    return;

  // find the Content provider (refactoring or code provider):
  ContentProvider provider = CodeRush.Content.Get(providerName);
  if (provider == null)
    return;

  // set the selected sub-menu item if specified:
  if (!String.IsNullOrEmpty(subMenuName))
  {
    SubMenuItem subMenuItem = new SubMenuItem(subMenuName, subMenuName);
    provider.SetSelectedSubMenuItem(subMenuItem);
  }

  // execute the provider if one is available:
  if (provider.IsAvailable)
    provider.Execute();
}

After compiling the plug-in, you will be able to create a shortcut for Sort Namespace References; for example. Specify two parameters by splitting them with a comma in the corresponding text box:

DXCore - New Refactoring Shortcut

The source code and plug-in are attached (22,847 bytes, C#, VS2010).

—–
Products: DXCore
Versions: 11.2 and up
VS IDEs: 2008 and up
Updated: Mar/20/2012
ID: D143

Similar Posts: