How do a create a ReSharper plugin that I can run against multiple selected files?

I want to select a directory or multiple files in Visual Studio's "Solution Explorer", right-click them, select the "Refactor" sub-menu and select my plugin code to run against the selected files.

Is there currently a way to do this in ReSharper?

1 comment
Comment actions Permalink

This is implemented as an Action (http://confluence.jetbrains.com/display/NETCOM/2.03+Actions+and+Menu+Items+%28R8%29), which is a command that can be attached to a menu item.

In the actions.xml file that describes where the link between menus and actions, you'll need a snippet like this:

<actions>
  <action id="YOUR_ACTION_ID" text="Your Action" />
  <!-- ... -->
  <insert group-id="OWNING_ACTION_GROUP_ID" position="last">
    <action-ref id="YOUR_ACTION_ID" />
  </insert>
</actions>



Where YOUR_ACTION_ID is an action that you've previously created, or created in the file. You can create it at the top level of the file, and reference it in several places using "action-ref". Alternatively, you can create it directly inside the "insert" element.

The OWNING_ACTION_GROUP_ID is a value for an existing menu to add the action to. Unfortunately, these aren't documented, which makes life rather awkward (even for me trying to write this up :-/ ) There are a number of internal action group ids for various nodes in the solution explorer, but they pretty much all include the following groups:
  • SolutionItemHead
  • SolutionItemLeaf
  • SolutionItemTests
  • SolutionItemContainer
  • ProjectItemGroup
  • SolutionItemGroup
  • SolutionFolderGroup


The menu displayed on the project node doesn't play terribly nicely with this pattern - it includes some items directly, but also includes "ProjectItemGroup" and "SolutionItemContainer", so I'd suggest adding your action to "SolutionItemGroup", "ProjectItemGroup" and "SolutionFolderGroup".

(For future reference, these groups and actions are defined in the JetBrains.PsiFeatures.VisualStudio.Core.Resoures.Actions.xml resource of the JetBrains.PsiFeatures.VisualStudio.Core.dll. Open this file in e.g. dotPeek, and you'll be able to see the actions.xml reference and look to see how they hang together. Unfortunately, any assembly that defines actions will have its own actions.xml file, so getting at this information for the whole product isn't easy)

Once you've got your menu item added to the write place, you can use:

context.GetData(ProjectModel.DataContext.DataConstants.PROJECT_MODEL_ELEMENTS);



to get an array of IProjectModelElement objects that represent each selected node in the solution explorer. You'll have to downcast to get the interface you need - usually ISolution, IProjectFile or IProjectFolder, although there are other interfaces that represent e.g. references.

This should give you enough information to then be able to perform an action against these files or folders.
0

Please sign in to leave a comment.