Resharper Spell Checker / ReSpeller / QuickFixes / TypoQuickFix.cs

using System.Collections.Generic;
using System.Windows.Controls;
using JetBrains.Annotations;
using JetBrains.Application;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Feature.Services.Bulbs;
using JetBrains.ReSharper.Intentions.Extensibility;
using JetBrains.ReSharper.Intentions.Extensibility.Menu;
using JetBrains.Util;
using ReSpeller.Analyzers;
using ReSpeller.Bulbs;
using ReSpeller.Highlightings;
using ReSpeller.SpellEngine;
using ReSpeller.Util;

namespace ReSpeller.QuickFixes
{
  [QuickFix]
  internal class TypoQuickFix : IQuickFix
  {
    private readonly ISpellChecker myChecker = Shell.Instance.GetComponent<ISpellChecker>();
    private readonly SuggestionGenerator mySuggestionGenerator = Shell.Instance.GetComponent<SuggestionGenerator>();
    private readonly IHighlighting myHighlighting;
    private readonly List<IBulbItem> myBulbItems = new List<IBulbItem>();

    public TypoQuickFix([NotNull] IdentifierTypoHighlighting highlighting)
    {
      myHighlighting = highlighting;
      string prefix;
      string suffix;
      AsyncValue<List<string>> suggests = new AsyncValue<List<string>>(() =>
                                                             {
                                                               List<TextPart> decomposedName =
                                                                 AnalyzerHelper.DecomposeName(highlighting.TreeNode,
                                                                                              highlighting.Element,
                                                                                              out prefix, out suffix);
                                                               return mySuggestionGenerator.Generate(decomposedName,
                                                                                                     prefix, suffix);
                                                             });

      myBulbItems.Add(new AdvancedTypoRenameBulbItem(highlighting.Element, suggests));
      string word = highlighting.WrongPart.Text;
      myBulbItems.Add(new AddToDictionaryBulbItem(word));
      myBulbItems.Add(new EditAddToDictionaryBulb(word));

      AddInternalDictionaryItemDebugOnly(word);
    }

    public TypoQuickFix([NotNull] TextRangeTypoHighlighting highlighting)
    {
      myHighlighting = highlighting;
      AsyncValue<List<string>> suggests = new AsyncValue<List<string>>(() => myChecker.Suggestions(highlighting.Range.GetText()));
      myBulbItems.Add(new TextRangeTypoReplaceBulbItem(highlighting.Range, suggests));
      string word = highlighting.Range.GetText();
      myBulbItems.Add(new AddToDictionaryBulbItem(word));
      myBulbItems.Add(new EditAddToDictionaryBulb(word));
      AddInternalDictionaryItemDebugOnly(word);
    }

    private void AddInternalDictionaryItemDebugOnly(string word)
    {
#if DEBUG 
      myBulbItems.Add(new AddToInternalDictionaryBulbItem(word));
#endif
    }

    public void CreateBulbItems(BulbMenu menu, Severity severity)
    {
      BulbGroup group = menu.GetOrCreateGroup(AnchorsForBulbMenuGroups.QuickFixesAnchor);
      foreach (var bulbItem in myBulbItems)
      {
        group.AddBulbAction(AnchorsForBulbMenuGroups.QuickFixesAnchor, bulbItem, ImgThemedIcons.Icon.Id);
      }
    }

    public bool IsAvailable(IUserDataHolder cache)
    {
      return myHighlighting.IsValid();
    }
  }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.