Source

Resharper Spell Checker / ReSpeller / Bulbs / AdvancedTypoRenameBulbItem.cs

Full commit
using System.Collections.Generic;
using JetBrains.Application;
using JetBrains.Application.DataContext;
using JetBrains.DataFlow;
using JetBrains.ProjectModel;
using JetBrains.ReSharper.Feature.Services.Bulbs;
using JetBrains.ReSharper.Psi;
using JetBrains.ReSharper.Psi.Search;
using JetBrains.ReSharper.Refactorings.Rename;
using JetBrains.ReSharper.Refactorings.Workflow;
using JetBrains.TextControl;
using ReSpeller.Util;
using ReSpeller.Workflows;

namespace ReSpeller.Bulbs
{
  class AdvancedTypoRenameBulbItem : IBulbItem
  {
    private readonly IDeclaredElement myElement;
    private readonly AsyncValue<List<string>> mySuggestedNames; 

    public AdvancedTypoRenameBulbItem(IDeclaredElement element, AsyncValue<List<string>> suggestedNames)
    {
      myElement = element;
      mySuggestedNames = suggestedNames;
    }

    public void Execute(ISolution solution, ITextControl textControl)
    {
      var service = solution.GetComponent<RenameRefactoringService>();
      IRefactoringWorkflow workflow;
        List<string> suggests = mySuggestedNames.Value;
        if (myElement is INamespace)
      {
        workflow = new RenameNamespaceWorkflowDecorator("NamespaceTypoRename", suggests);
      }
      else if (service.IsLocal(myElement))
      {
        workflow = new TypoInlineRenameWorkflowDecorator("InlineTypoRename", suggests);
      }
      else
      {
        workflow = new TypoRenameWorkflow(suggests, Shell.Instance.Locks, 
                                          SearchDomainFactory.Instance, 
                                          RenameRefactoringService.Instance, 
                                          solution, 
                                          "TypoRename");
      }
      Lifetimes.Using(lifetime => 
        RefactoringActionUtil.ExecuteRefactoring(
                    JetBrains.ActionManagement.ShellComponentsEx.ActionManager(Shell.Instance.Components)
                      .DataContexts.CreateOnSelection(lifetime, DataRules.AddRule("DoTypoRenameWorkflow", 
                      JetBrains.ProjectModel.DataContext.DataConstants.SOLUTION, solution)), workflow));
     }

    public string Text
    {
      get { return string.Format("Typo in \"{0}\". Rename to ...", myElement.ShortName); }
    }
  }
}