Example #1
0
        /// <summary>
        /// Spell check.
        /// </summary>
        /// <param name='element'>
        /// Element in the document.
        /// </param>
        /// <param name='lineNumber'>
        /// Line number.
        /// </param>
        /// <param name='document'>
        /// Document XML.
        /// </param>
        /// <param name='type'>
        /// Type of the XML node
        /// </param>
        private void SpellCheck(CsElement element, int lineNumber, XmlNode document, string type)
        {
            this.WriteMessage("Spell check");
            this.WriteMessage(lineNumber.ToString());
            this.WriteMessage(type);
            if (this.speller != null)
            {
                this.WriteMessage("Spell checker available");
                if (document != null && !string.IsNullOrEmpty(document.InnerText))
                {
                    this.WriteMessage("Document available");
                    WordParser parser = new WordParser(document.InnerText, WordParserOptions.SplitCompoundWords);
                    this.WriteMessage("Document parsed");
                    if (parser.PeekWord() != null)
                    {
                        this.WriteMessage("Parser has found a word");
                        var recognizedWords = element.Document.SourceCode.Project.RecognizedWords;
                        this.WriteMessage("Found recognized words");
                        string word = parser.NextWord();
                        this.WriteMessage(word);
                        do
                        {
                            this.WriteMessage(word);
                            if ((!word.StartsWith("$") || !word.EndsWith("$")) && (!recognizedWords.Contains(word) && !this.speller.Spell(word)))
                            {
                                this.WriteMessage("Violation found");
                                this.AddViolation(element, lineNumber, "CommentSpellingIncorrect", word, type);
                                this.WriteMessage("Violation added");
                            }
                        }
                        while ((word = parser.NextWord()) != null);
                        this.WriteMessage("Done parsing");
                    }
                }
            }

            this.WriteMessage("Spell check call completed");
        }
        /// <summary>
        /// Returns True if the text has incorrect spelling.
        /// </summary>
        /// <param name="element">
        /// The element containing the text we're checking.
        /// </param>
        /// <param name="text">
        /// The text to check.
        /// </param>
        /// <param name="spellingError">
        /// Returns a comma separated list of words encountered as spelling errors.
        /// </param>
        /// <returns>
        /// True if the text contains an incorrect spelling.
        /// </returns>
        private static bool TextContainsIncorectSpelling(CsElement element, string text, out string spellingError)
        {
            NamingService namingService = NamingService.GetNamingService(element.Document.SourceCode.Project.Culture);

            spellingError = string.Empty;

            if (namingService.SupportsSpelling)
            {
                ICollection<string> recognizedWords = element.Document.SourceCode.Project.RecognizedWords;

                WordParser parser = new WordParser(text, WordParserOptions.SplitCompoundWords, recognizedWords);
                if (parser.PeekWord() != null)
                {
                    string word = parser.NextWord();
                    do
                    {
                        // Ignore words starting and ending with '$'.
                        // Ignore if in our recognized words list or correct spelling
                        if ((word.StartsWith("$") && word.EndsWith("$")) || (recognizedWords.Contains(word) || IsSpelledCorrectly(namingService, word)))
                        {
                            continue;
                        }

                        if (!string.IsNullOrEmpty(spellingError))
                        {
                            spellingError += ", ";
                        }

                        spellingError += word;
                    }
                    while ((word = parser.NextWord()) != null);
                }
            }

            return !string.IsNullOrEmpty(spellingError);
        }