/// <summary>
        /// Configures the environment to load the plugin manager and verify we
        /// have access to our plugin and projectPlugin.
        /// </summary>
        private void SetupPlugin(
			out BlockCommandContext context,
			out ProjectBlockCollection blocks,
			out BlockCommandSupervisor commands,
			out PluginSupervisor plugins,
			out SpellEngineSpellingProjectPlugin projectPlugin)
        {
            // Start getting us a simple plugin manager.
            var spelling = new SpellingFrameworkPlugin();
            var nhunspell = new HunspellSpellingPlugin();
            var pluginManager = new PluginManager(spelling, nhunspell);

            PluginManager.Instance = pluginManager;

            // Create a project and pull out the useful properties we'll use to
            // make changes.
            var project = new Project();

            context = new BlockCommandContext(project);
            blocks = project.Blocks;
            commands = project.Commands;
            plugins = project.Plugins;

            // Load in the immediate correction editor.
            if (!plugins.Add("Spelling Framework"))
            {
                // We couldn't load it for some reason.
                throw new ApplicationException("Cannot load 'Spelling' plugin.");
            }

            if (!plugins.Add("NHunspell"))
            {
                // We couldn't load it for some reason.
                throw new ApplicationException("Cannot load 'NHunspell' plugin.");
            }

            // Pull out the projectPlugin for the correction and cast it (since we know
            // what type it is).
            ProjectPluginController pluginController = plugins.Controllers[1];
            projectPlugin =
                (SpellEngineSpellingProjectPlugin) pluginController.ProjectPlugin;
        }
예제 #2
0
        public HunspellSpellingPlugin()
        {
            // Set up the spell engine for multi-threaded access.
            SpellEngine = new SpellEngine();

            // Assume we are disabled unless we can configure it properly.
            projectPlugin = new DisabledSpellingProjectPlugin();

            // Figure out the paths to the dictionary files. For the time being,
            // we're going to assume we're using U.S. English.
            string affixFilename;
            string dictionaryFilename;

            if (!GetDictionaryPaths("en_US", out affixFilename, out dictionaryFilename))
            {
                // We couldn't get the dictionary paths, so just stick with the
                // disabled spelling plugin.
                return;
            }

            // Attempt to load the NHunspell plugin. This is a high-quality
            // plugin based on Managed C++. This works nicely in Windows, but
            // has no support for Linux.
            try
            {
                // Attempt to load the U.S. English language. This will throw
                // an exception if it cannot load. If it does, then we use it.
                var englishUnitedStates = new LanguageConfig
                {
                    LanguageCode     = "en_US",
                    HunspellAffFile  = affixFilename,
                    HunspellDictFile = dictionaryFilename,
                    HunspellKey      = string.Empty
                };

                SpellEngine.AddLanguage(englishUnitedStates);

                // If we got this far, set the project plugin to the one that
                // uses the SpellEngine from NHunspell.
                projectPlugin = new SpellEngineSpellingProjectPlugin(this);
                return;
            }
            catch (Exception exception)
            {
                // Report that we can't load the first attempt.
                Console.WriteLine("Cannot load NHunspell: " + exception);
            }

            // If we got this far, we couldn't load the NHunspell plugin.
            // Attempt to load in the PInvoke implementation instead.
            try
            {
                // Create a new Hunspell P/Invoke loader.
                var pinvokePlugin = new PInvokeSpellingProjectPlugin(
                    affixFilename, dictionaryFilename);
                projectPlugin = pinvokePlugin;
            }
            catch (Exception exception)
            {
                // Report that we can't load the first attempt.
                Console.WriteLine("Cannot load NHunspell via P/Invoke: " + exception);
            }
        }