Click here to Skip to main content
15,895,557 members
Articles / Programming Languages / C#

Code Spelling Checker Extension for Visual Studio 2010 (VSX)

Rate me:
Please Sign up or sign in to vote.
4.98/5 (43 votes)
7 Apr 2010CPOL6 min read 111.6K   2.4K   74  
Building a spelling checker for source code as an extension for Visual Studio 2010
using System;


namespace DigitalSamurai.SpellSharp.InputProcessing.Processors
{
    public partial class CSharpProcessor
    {
        public partial class CSharpParser
        {
            public static class ReferencedType
            {
                public static bool Skip (CSharpParsingState current, bool throwError = true)
                {
                    if (current == null)
                    {
                        throw new ArgumentNullException ("current");
                    }

                    if (current.Node == null)
                    {
                        return false;
                    }

                    current.StartScope (ScopeTypes.ReferencedType);
                    try
                    {
                        if (current.IsLexicalElementMemberOf (LexicalElements.Group_LanguageTypes))
                        {
                            current.CommitScopeAndMoveToNext ();
                            return true;
                        }

                        if (current.LexicalElement == LexicalElements.Global)
                        {
                            current.MoveToNextExplicit ();
                            current.IsLexicalElementExpected (LexicalElements.Symbol_Colon_Colon);
                            current.MoveToNextExplicit ();
                        }

                        var isIdentifier = true;
                        while (HasSkippedSection (current, isIdentifier, throwError))
                        {
                            current.MoveToNextExplicit ();

                            isIdentifier = !isIdentifier;
                        }

                        if (isIdentifier)
                        {
                            if (throwError)
                            {
                                throw new ParseException ();
                            }

                            current.RollbackScope ();
                            return false;
                        }

                        switch (current.LexicalElement)
                        {
                            case LexicalElements.Symbol_Asterix:
                                current.MoveToNextExplicit ();
                                break;

                            case LexicalElements.Symbol_SquaredBracketOpen:
                                current.SkipToExplicitly (LexicalElements.Symbol_SquaredBracketClose);
                                current.MoveToNextExplicit ();
                                break;
                        }

                        current.CommitScope ();
                        return true;
                    }
                    catch
                    {
                        current.RollbackScope ();

                        if (throwError)
                        {
                            throw;
                        }

                        return false;
                    }
                }

                private static bool HasSkippedSection (CSharpParsingState current, bool isIdentifier, bool throwError)
                {
                    if (isIdentifier)
                    {
                        if (!HasStartedWith_UnderScoreOrLetter_ContinuesWith_UnderScoreOrLetterOrDigit (current.Token.Text))
                        {
                            if (throwError)
                            {
                                throw new ParseException ();
                            }
                            return false;
                        }

                        return true;
                    }

                    if (current.LexicalElement == LexicalElements.Symbol_AngledBracketOpen)
                    {
                        do
                        {
                            current.MoveToNextExplicit ();

                            ReferencedType.Skip (current);
                        }
                        while (current.LexicalElement == LexicalElements.Symbol_Comma);

                        current.IsLexicalElementExpected (LexicalElements.Symbol_AngledBracketClose);
                        current.MoveToNextExplicit ();
                        return false;
                    }

                    return current.LexicalElement == LexicalElements.Symbol_Dot;
                }


                private static bool HasStartedWith_UnderScoreOrLetter_ContinuesWith_UnderScoreOrLetterOrDigit (string value)
                {
                    if (string.IsNullOrWhiteSpace (value) || !value[0].IsUnderScoreOrLetter ())
                    {
                        return false;
                    }

                    for (int index = 1; index < value.Length; index++)
                    {
                        if (!value[index].IsUnderScoreOrLetterOrDigit ())
                        {
                            return false;
                        }
                    }

                    return true;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions