Commits

Nerzhul500 committed ded5a3e

Changeset backport from Pro

Comments (0)

Files changed (14)

ReSpeller/Actions/AddArbitraryWordHelper.cs

         promptWinForm.FormBorderStyle = FormBorderStyle.FixedDialog;
         if (promptWinForm.ShowDialog())
         {
-          Shell.Instance.GetComponent<ISpellChecker>().AddWordToUserDict(promptWinForm.Value);
+          Shell.Instance.GetComponent<ISingleLanguageWordStorage>().AddWordToUserDict(promptWinForm.Value);
         }
       }
     }

ReSpeller/Bulbs/AddToDictionaryBulbItem.cs

 
     public void Execute(ISolution solution, ITextControl textControl)
     {
-      var spellChecker = Shell.Instance.GetComponent<ISpellChecker>();
-      spellChecker.AddWordToUserDict(myNewWord);
+      var languageWordStorage = Shell.Instance.GetComponent<ISingleLanguageWordStorage>();
+      languageWordStorage.AddWordToUserDict(myNewWord);
       Daemon.GetInstance(solution).Invalidate();
     }
 

ReSpeller/Bulbs/AddToInternalDictionaryBulbItem.cs

 
     public void Execute(ISolution solution, ITextControl textControl)
     {
-      var spellChecker = Shell.Instance.GetComponent<ISpellChecker>();
-      spellChecker.AddWordToCustomDict(myNewWord);
+      var languageWordStorage = Shell.Instance.GetComponent<ISingleLanguageWordStorage>();
+      languageWordStorage.AddWordToCustomDict(myNewWord);
       Daemon.GetInstance(solution).Invalidate();
     }
 

ReSpeller/ReSpeller.csproj

     </Compile>
     <Compile Include="SpellEngine\ConfigurableWordChecker.cs" />
     <Compile Include="SpellEngine\Constants.cs" />
-    <Compile Include="SpellEngine\Dictionaries\DictionaryFactory.cs" />
     <Compile Include="SpellEngine\Dictionaries\FileDictionaryStorage.cs" />
+    <Compile Include="SpellEngine\Dictionaries\HunspellDictionaryFactory.cs" />
     <Compile Include="SpellEngine\Dictionaries\HunspellEngine.cs" />
+    <Compile Include="SpellEngine\Dictionaries\IDictionaryFactory.cs" />
     <Compile Include="SpellEngine\Dictionaries\IDictionaryStorage.cs" />
     <Compile Include="SpellEngine\Dictionaries\IDictionaryEngine.cs" />
     <Compile Include="SpellEngine\Dictionaries\ILanguageDictionary.cs" />
+    <Compile Include="SpellEngine\ISingleLanguageWordStorage.cs" />
     <Compile Include="SpellEngine\ISpellChecker.cs" />
     <Compile Include="SpellEngine\Dictionaries\IUserInternalDictionary.cs" />
     <Compile Include="SpellEngine\IWordSuggestor.cs" />
     <Compile Include="SpellEngine\SingleDictionarySuggestor.cs" />
     <Compile Include="SpellEngine\SpellChecker.cs" />
     <Compile Include="SpellEngine\SpellCheckerConfig.cs" />
+    <Compile Include="SpellEngine\SuggestFilter.cs" />
     <Compile Include="SpellEngine\SuggestionGenerator.cs" />
     <Compile Include="SpellEngine\TextSplitter.cs" />
     <Compile Include="SpellEngine\Dictionaries\UserInternalDictionary.cs" />

ReSpeller/SpellEngine/Constants.cs

 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 
 namespace ReSpeller.SpellEngine
     public const string UserDictSubDir = "ReSpeller";
     public const string HunspellAffixExt = "aff";
     public const string HunspellDictExt = "dic";
+    public static readonly string PluginPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof (ISpellChecker)).Location);
+    public static readonly string MainDictDir = Path.Combine(PluginPath, MainDictSubDir);
+    public static readonly string UserDictDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), UserDictSubDir);
   }
 }

ReSpeller/SpellEngine/Dictionaries/DictionaryFactory.cs

-using System;
-
-namespace ReSpeller.SpellEngine.Dictionaries
-{
-  public interface IDictionaryFactory
-  {
-    ILanguageDictionary CreateDictionary(string language);
-  }
-
-  public class HunspellDictionaryFactory : IDictionaryFactory
-  {
-    private readonly string myMainDictionariesDir;
-    private readonly string myUserDictionariesDir;
-
-
-    public HunspellDictionaryFactory(string mainDictionariesDir, string userDictionariesDir)
-    {
-      myMainDictionariesDir = mainDictionariesDir;
-      myUserDictionariesDir = userDictionariesDir;
-    }
-
-    public ILanguageDictionary CreateDictionary(string language)
-    {
-      throw new NotImplementedException();
-    }
-  }
-}

ReSpeller/SpellEngine/Dictionaries/HunspellDictionaryFactory.cs

+using System.IO;
+
+namespace ReSpeller.SpellEngine.Dictionaries
+{
+  public class HunspellDictionaryFactory : IDictionaryFactory
+  {
+    private readonly string myMainDictionariesDir;
+    private readonly string myUserDictionariesDir;
+
+
+    public HunspellDictionaryFactory(string mainDictionariesDir, string userDictionariesDir)
+    {
+      myMainDictionariesDir = mainDictionariesDir;
+      myUserDictionariesDir = userDictionariesDir;
+    }
+
+    public IUserInternalDictionary CreateDictionary(string language)
+    {
+      var userStorage = new FileDictionaryStorage(Path.Combine(myUserDictionariesDir, language + "." + Constants.DictExt));
+      var internalStorage = new FileDictionaryStorage(Path.Combine(myMainDictionariesDir, language + Constants.InternalDictSuffix + "." + Constants.DictExt));
+      HunspellEngine hunspellEngine = HunspellEngine.CreateForLanguage(Constants.MainDictDir, language);
+      return new UserInternalDictionary(hunspellEngine, language, userStorage, internalStorage);
+    }
+  }
+}

ReSpeller/SpellEngine/Dictionaries/HunspellEngine.cs

   {
     private readonly Hunspell myHunspell;
 
+    static HunspellEngine()
+    {
+      NativeDllPath = Constants.PluginPath;
+    }
+
     public static string NativeDllPath
     {
       get { return Hunspell.NativeDllPath; }

ReSpeller/SpellEngine/Dictionaries/IDictionaryFactory.cs

+using System;
+
+namespace ReSpeller.SpellEngine.Dictionaries
+{
+  public interface IDictionaryFactory
+  {
+    IUserInternalDictionary CreateDictionary(string language);
+  }
+}

ReSpeller/SpellEngine/ISingleLanguageWordStorage.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace ReSpeller.SpellEngine
+{
+  public interface ISingleLanguageWordStorage
+  {
+    void AddWordToUserDict(string word);
+    void AddWordToCustomDict(string word);
+  }
+}

ReSpeller/SpellEngine/ISpellChecker.cs

   {
     bool CheckWordSpelling(string word);
     List<string> Suggestions(string word);
-    void AddWordToUserDict(string word);
-    void AddWordToCustomDict(string word);
     void UpdateConfig();
   }
 }

ReSpeller/SpellEngine/SingleDictionarySuggestor.cs

 
 namespace ReSpeller.SpellEngine
 {
-    public class SingleDictionarySuggestor : IWordSuggestor
+  public class SingleDictionarySuggestor : IWordSuggestor
+  {
+    private readonly ILanguageDictionary myDictionary;
+
+    public SingleDictionarySuggestor(ILanguageDictionary dictionary)
     {
-        private readonly ILanguageDictionary myDictionary;
+      myDictionary = dictionary;
+    }
 
-        public SingleDictionarySuggestor(ILanguageDictionary dictionary)
-        {
-            myDictionary = dictionary;
-        }
-
-        public List<string> Suggest(string word)
-        {
-            List<string> result = myDictionary.Suggests(word)
-                .Where(s => s.ToLower() != word.ToLower()) // filter suggests not equals original word
-                .Select(s => NamingStyleConverter.Convert(s, word)).ToList(); // convert style
-            return result.Any() ? result.ToList() : new List<string> { word };
-        }
+    public List<string> Suggest(string word)
+    {
+      List<string> suggests = myDictionary.Suggests(word);
+      List<string> result = SuggestFilter.Filter(word, suggests);
+      return result.Any() ? result.ToList() : new List<string> {word};
     }
+  }
 }

ReSpeller/SpellEngine/SpellChecker.cs

 namespace ReSpeller.SpellEngine
 {
   [ShellComponent]
-  public sealed class SpellChecker : IDisposable, ISpellChecker
+  public sealed class SpellChecker : IDisposable, ISpellChecker, ISingleLanguageWordStorage
   {
     private readonly IContextBoundSettingsStore myStore;
     private const string CurrentLanguage = "en_us";
 
-    private readonly UserInternalDictionary myDictionary;
-    private readonly string myPluginPath = Path.GetDirectoryName(Assembly.GetAssembly(typeof (ISpellChecker)).Location);
+    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);      
-      HunspellEngine.NativeDllPath = myPluginPath;
-      string mainDictDir = Path.Combine(myPluginPath, Constants.MainDictSubDir);
-      string userDictDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Constants.UserDictSubDir);
-      var userStorage = new FileDictionaryStorage(Path.Combine(userDictDir, CurrentLanguage + "." + Constants.DictExt));
-      var internalStorage = new FileDictionaryStorage(Path.Combine(mainDictDir, CurrentLanguage + Constants.InternalDictSuffix + "." + Constants.DictExt));
-      HunspellEngine hunspellEngine = HunspellEngine.CreateForLanguage(mainDictDir, CurrentLanguage);
-      myDictionary = new UserInternalDictionary(hunspellEngine, CurrentLanguage, userStorage, internalStorage);
+      IDictionaryFactory factory = new HunspellDictionaryFactory(Constants.MainDictDir, Constants.UserDictDir);
+      myDictionary = factory.CreateDictionary(CurrentLanguage);
       myWordChecker = new ConfigurableWordChecker(new List<ILanguageDictionary> {myDictionary});
       myWordSuggestor = new SingleDictionarySuggestor(myDictionary);
     }

ReSpeller/SpellEngine/SuggestFilter.cs

+using System.Collections.Generic;
+using System.Linq;
+
+namespace ReSpeller.SpellEngine
+{
+  public static class SuggestFilter
+  {
+    public static List<string> Filter(string originWord, IEnumerable<string> suggests)
+    {
+      return suggests
+        .Where(s => s.ToLower() != originWord.ToLower()) // filter suggests not equals original originWord
+        .Select(s => NamingStyleConverter.Convert(s, originWord)).ToList(); // convert style
+    }
+  }
+}