예제 #1
0
        /// <summary>
        /// This creates an instance of the WPF spell checker for the given text box
        /// </summary>
        /// <param name="textBox">The text box to initialize</param>
        private static void AddWpfSpellChecker(TextBox textBox)
        {
            // Don't do anything if the default spell checker is enabled or it's read-only
            if (!textBox.SpellCheck.IsEnabled && !textBox.IsReadOnly)
            {
                lock (syncRoot)
                {
                    // Create the shared configuration and dictionary on first use
                    if (configuration == null)
                    {
                        configuration = new SpellCheckerConfiguration();
                        configuration.Load(SpellingConfigurationFile.GlobalConfigurationFilename);

                        var globalDictionaries = configuration.DictionaryLanguages.Select(l =>
                                                                                          GlobalDictionary.CreateGlobalDictionary(l, null,
                                                                                                                                  configuration.AdditionalDictionaryFolders, configuration.RecognizedWords)).Where(
                            d => d != null).Distinct().ToList();

                        dictionary = new SpellingDictionary(globalDictionaries, configuration.IgnoredWords);
                    }

                    // Ignore it if disabled or it's an excluded text box
                    string name = ElementName(textBox);

                    if (!configuration.EnableWpfTextBoxSpellChecking || configuration.VisualStudioExclusions.Any(
                            v => v.IsMatch(name)))
                    {
                        return;
                    }
                }

                var wsc = new WpfTextBoxSpellChecker(textBox);

                wpfSpellCheckers.AddOrUpdate(textBox, wsc, (k, v) => wsc);
            }
        }
예제 #2
0
        //=====================================================================

        /// <summary>
        /// Spell check the current document and start showing suggestions for replacement
        /// </summary>
        private bool SpellCheckCurrentDocument()
        {
            bool success = false;

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                // Use the language from the current project to do the spell checking
                var projectExplorer = this.FindProjectExplorerWindow();

                if (dictionary == null || (projectExplorer != null &&
                                           projectExplorer.CurrentProject.Language != dictionary.Language))
                {
                    dictionary = GlobalDictionary.CreateGlobalDictionary(projectExplorer?.CurrentProject.Language);
                }

                if (dictionary == null)
                {
                    lblMisspelledWord.Text = "Unable to create dictionary!";
                }
                else
                {
                    var speller = new FileSpellChecker(dictionary);

                    speller.MisspelledWord += new EventHandler <SpellingEventArgs>(speller_MisspelledWord);
                    speller.DoubledWord    += new EventHandler <SpellingEventArgs>(speller_DoubledWord);

                    string text = currentTopicWindow.GetAllText();

                    string ext = Path.GetExtension(currentTopicWindow.Filename).ToLowerInvariant();

                    switch (ext)
                    {
                    case ".aml":
                    case ".axml":
                    case ".ascx":
                    case ".asp":
                    case ".aspx":
                    case ".config":
                    case ".content":
                    case ".htm":
                    case ".html":
                    case ".items":
                    case ".sitemap":
                    case ".snippets":
                    case ".tokens":
                    case ".xaml":
                    case ".xml":
                    case ".xsl":
                    case ".xslt":
                    case ".xamlcfg":
                        using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(text)))
                            using (var reader = XmlReader.Create(ms, new XmlReaderSettings {
                                DtdProcessing = DtdProcessing.Ignore
                            }))
                            {
                                speller.SpellCheckXmlReader(reader);
                            }
                        break;

                    default:
                        speller.SpellCheckText(text);
                        break;
                    }

                    success = true;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);

                lblMisspelledWord.Text = "Unable to spell check file: " + ex.Message;
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }

            return(success);
        }