Creating and handling actions

Action system

Action is a command executed by user in the specific context. Action can be available or not available in the context. Presentation of the action (text, icon, checked state, visibility) may depend on the context.

Action should have associated handler, which processes requests to update action for the context and execute an action. One handler can handle multiply actions, thought it is not common case.

Actions can be created either dynamically by using ActionManager shell component, or in a declarative way by providing XML file in plugin assembly resources.

References

JetBrains.ReSharper.ActionManagement.dll – provides interfaces and attributes for actions
JetBrains.ReSharper.IDECore.dll – provides constants and types specific to ReSharper environment
JetBrains.ReSharper.Util.dll – contains utility classes used throughout the project

Examples below using ISolution, which requires referencing one more assembly:
JetBrains.ReSharper.ProjectModel.dll – contains types and components exposing project model, i.e. solution, projects and files

Actions.xml - declarative way

  • Create embedded resource file actions.xml (or any other name you want)
  • Specify actions and their locations
    <?xml version="1.0" encoding="utf-8" ?>
    <actions>
      <insert group-id="ReSharper" position="last">
        <separator/>
        <action id="PluginName.ActionName" text="Action Title"/>
      </insert>
    </actions>
  • Add assembly attribute to point to file containing actions. Value of the attribute will be used in call to Assembly.GetManifestResourceStream()
    [assembly: ActionsXml("Project.Default.Namespace.Actions.xml")]
  • Create action handler by deriving class from IActionHandler and decorating with ActionHandler attribute. Parameter of ActionHandlerAttribute specifies id of the action it is going to handle.
    [ActionHandler("PluginName.ActionName")]
    public class FindTextAction : IActionHandler {}
  • Implement IActionHandler interface
    public bool Update(IDataContext context, ActionPresentation presentation, DelegateUpdate nextUpdate)
    {
      // check, that current context has solution opened
      return context.CheckAllNotNull(DataConstants.SOLUTION);
    }
    
    public void Execute(IDataContext context, DelegateExecute nextExecute)
    {
      // retrieve solution
      ISolution solution = context.GetData(DataConstants.SOLUTION);
      ... perform action ...
    }

ActionManager - dynamic way

To be described

Samples and power toys

Labels

 
(None)