Source

Resharper Spell Checker / ReSpeller / SpellEngine / SpellChecker.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using JetBrains.Application;
using JetBrains.Application.Components;
using JetBrains.Application.Settings;
using ReSpeller.Settings;
using ReSpeller.SpellEngine.Dictionaries;

namespace ReSpeller.SpellEngine
{
  [ShellComponent(ProgramConfigurations.ALL & ~ProgramConfigurations.TEST)]
  public sealed class SpellChecker : IDisposable, ISpellChecker, ISingleLanguageWordStorage
  {
    private readonly IContextBoundSettingsStore myStore;
    private const string CurrentLanguage = "en_us";

    private readonly IUserInternalDictionary myDictionary;
    private volatile object mySync = new object();
    private readonly ConfigurableWordChecker myWordChecker;
    private readonly IWordSuggestor myWordSuggestor;

    public SpellChecker(ISettingsStore store)
    {
      myStore = store.BindToContextTransient(ContextRange.ApplicationWide);      
      IDictionaryFactory factory = new HunspellDictionaryFactory(Constants.MainDictDir, Constants.UserDictDir);
      myDictionary = factory.CreateDictionary(CurrentLanguage);
      myWordChecker = new ConfigurableWordChecker(new List<ILanguageDictionary> {myDictionary});
      myWordSuggestor = new SingleDictionarySuggestor(myDictionary);
    }

    #region IDisposable Members

    public void Dispose()
    {
      myDictionary.Dispose();
    }

    #endregion

    public bool CheckWordSpelling(string word)
    {     
      lock (mySync)
      {
        return myWordChecker.Check(word);
      }
    }

    public List<string> Suggestions(string word)
    {
      lock (mySync)
      {
        return myWordSuggestor.Suggest(word);
      }
    }

    public void AddWordToUserDict(string word)
    {
      lock (mySync)
      {
        myDictionary.AddToUserDict(word);
      }
    }

    public void AddWordToCustomDict(string word)
    {
      lock (mySync)
      {
        myDictionary.AddToInternalDict(word);
      }
    }

    public void UpdateConfig()
    {
      var settings = myStore.GetKey<ReSpellerSettings>(SettingsOptimization.DoMeSlowly);
      myWordChecker.Config.MinWordLength = settings.MinWordLength;
      myWordChecker.Config.SplitInPairs = settings.SplitInPairs;
    }
  }
}