void AddIndentation(BraceStyle braceStyle)
        {
            switch (braceStyle)
            {
            case BraceStyle.DoNotChange:
            case BraceStyle.EndOfLine:
            case BraceStyle.EndOfLineWithoutSpace:
            case BraceStyle.NextLine:
            case BraceStyle.NextLineShifted:
            case BraceStyle.BannerStyle:
                indentDelta.Push(IndentType.Block);
                break;

            case BraceStyle.NextLineShifted2:
                indentDelta.Push(IndentType.DoubleBlock);
                break;
            }
        }
Exemplo n.º 2
0
        void FixOpenBrace(BraceStyle braceStyle, AstNode lbrace)
        {
            if (lbrace.IsNull)
            {
                return;
            }
            switch (braceStyle)
            {
            case BraceStyle.DoNotChange:
                return;

            case BraceStyle.BannerStyle:
            case BraceStyle.EndOfLine:
                var prev = lbrace.GetPrevNode();
                while (prev is NewLineNode)
                {
                    prev = prev.GetPrevNode();
                }
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                int prevOffset = document.GetOffset(prev.EndLocation);

                if (prev is Comment || prev is PreProcessorDirective)
                {
                    int next = document.GetOffset(lbrace.GetNextNode().StartLocation);
                    AddChange(prevOffset, next - prevOffset, "");
                    while (prev is Comment || prev is PreProcessorDirective)
                    {
                        prev = prev.GetPrevNode();
                    }
                    prevOffset = document.GetOffset(prev.EndLocation);
                    AddChange(prevOffset, 0, " {");
                }
                else
                {
                    int braceOffset2 = document.GetOffset(lbrace.StartLocation);
                    AddChange(prevOffset, braceOffset2 - prevOffset, " ");
                }
                break;

            case BraceStyle.EndOfLineWithoutSpace:
                prev = lbrace.GetPrevNode();
                while (prev is NewLineNode)
                {
                    prev = prev.GetPrevNode();
                }
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset = document.GetOffset(prev.EndLocation);
                int braceOffset = document.GetOffset(lbrace.StartLocation);
                AddChange(prevOffset, braceOffset - prevOffset, "");
                break;

            case BraceStyle.NextLine:
                prev = lbrace.GetPrevNode();
                while (prev is NewLineNode)
                {
                    prev = prev.GetPrevNode();
                }
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                AddChange(prevOffset, braceOffset - prevOffset, options.EolMarker + curIndent.IndentString);
                break;

            case BraceStyle.NextLineShifted:
                prev = lbrace.GetPrevNode();
                while (prev is NewLineNode)
                {
                    prev = prev.GetPrevNode();
                }
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                curIndent.Push(IndentType.Block);
                AddChange(prevOffset, braceOffset - prevOffset, options.EolMarker + curIndent.IndentString);
                curIndent.Pop();
                break;

            case BraceStyle.NextLineShifted2:
                prev = lbrace.GetPrevNode();
                while (prev is NewLineNode)
                {
                    prev = prev.GetPrevNode();
                }
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                curIndent.Push(IndentType.Block);
                AddChange(prevOffset, braceOffset - prevOffset, options.EolMarker + curIndent.IndentString);
                curIndent.Pop();
                break;
            }
        }
        void Push(char ch)
        {
            if (readPreprocessorExpression)
            {
                wordBuf.Append(ch);
            }

            if (inside.HasFlag(Inside.VerbatimString) && pc == '"' && ch != '"')
            {
                inside &= ~Inside.String;
            }
            switch (ch)
            {
            case '#':
                if (IsLineStart)
                {
                    inside = Inside.PreProcessor;
                }
                break;

            case '/':
                if (IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (pc == '/')
                {
                    if (inside.HasFlag(Inside.Comment))
                    {
                        inside |= Inside.DocComment;
                    }
                    else
                    {
                        inside |= Inside.Comment;
                    }
                }
                break;

            case '*':
                if (IsInStringOrChar || IsInComment || IsInPreProcessorComment)
                {
                    break;
                }
                if (pc == '/')
                {
                    inside |= Inside.MultiLineComment;
                }
                break;

            case '\t':
                var nextTabStop = (col - 1 + textEditorOptions.IndentSize) / textEditorOptions.IndentSize;
                col = 1 + nextTabStop * textEditorOptions.IndentSize;
                return;

            case '\r':

                if (readPreprocessorExpression)
                {
                    if (!eval(wordBuf.ToString()))
                    {
                        inside |= Inside.PreProcessorComment;
                    }
                }

                inside &= ~(Inside.Comment | Inside.String | Inside.CharLiteral | Inside.PreProcessor);
                CheckKeyword(wordBuf.ToString());
                wordBuf.Length = 0;
                if (addContinuation)
                {
                    indent.Push(IndentType.Continuation);
                }
                thisLineindent             = indent.Clone();
                addContinuation            = false;
                IsLineStart                = true;
                readPreprocessorExpression = false;
                col = 1;
                line++;
                break;

            case '\n':
                if (pc == '\r')
                {
                    break;
                }
                goto case '\r';

            case '"':
                if (IsInComment || IsInPreProcessorComment)
                {
                    break;
                }
                if (inside.HasFlag(Inside.String))
                {
                    if (pc != '\\')
                    {
                        inside &= ~Inside.String;
                    }
                    break;
                }

                if (pc == '@')
                {
                    inside |= Inside.VerbatimString;
                }
                else
                {
                    inside |= Inside.String;
                }
                break;

            case '<':
            case '[':
            case '(':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                parenStack.Push(new TextLocation(line, col));
                popNextParenBlock = true;
                indent.Push(IndentType.Block);
                break;

            case '>':
            case ']':
            case ')':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (popNextParenBlock)
                {
                    parenStack.Pop();
                }
                indent.Pop();
                indent.ExtraSpaces = 0;
                break;

            case ',':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (parenStack.Count > 0 && parenStack.Peek().Line == line)
                {
                    indent.Pop();
                    popNextParenBlock  = false;
                    indent.ExtraSpaces = parenStack.Peek().Column - 1 - thisLineindent.CurIndent;
                }
                break;

            case '{':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                currentBody = nextBody;
                if (indent.Count > 0 && indent.Peek() == IndentType.Continuation)
                {
                    indent.Pop();
                }
                addContinuation = false;
                AddIndentation(currentBody);
                break;

            case '}':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                indent.Pop();
                if (indent.Count > 0 && indent.Peek() == IndentType.Continuation)
                {
                    indent.Pop();
                }
                break;

            case ';':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (indent.Count > 0 && indent.Peek() == IndentType.Continuation)
                {
                    indent.Pop();
                }
                break;

            case '\'':
                if (IsInComment || inside.HasFlag(Inside.String) || IsInPreProcessorComment)
                {
                    break;
                }
                if (inside.HasFlag(Inside.CharLiteral))
                {
                    if (pc != '\\')
                    {
                        inside &= ~Inside.CharLiteral;
                    }
                }
                else
                {
                    inside &= Inside.CharLiteral;
                }
                break;
            }

            if (!IsInComment && !IsInStringOrChar && !readPreprocessorExpression)
            {
                if ((wordBuf.Length == 0 ? char.IsLetter(ch) : char.IsLetterOrDigit(ch)) || ch == '_')
                {
                    wordBuf.Append(ch);
                }
                else
                {
                    if (inside.HasFlag(Inside.PreProcessor))
                    {
                        if (wordBuf.ToString() == "endif")
                        {
                            inside &= ~Inside.PreProcessorComment;
                        }
                        else if (wordBuf.ToString() == "if")
                        {
                            readPreprocessorExpression = true;
                        }
                        else if (wordBuf.ToString() == "elif")
                        {
                            inside &= ~Inside.PreProcessorComment;
                            readPreprocessorExpression = true;
                        }
                    }
                    else
                    {
                        CheckKeyword(wordBuf.ToString());
                    }
                    wordBuf.Length = 0;
                }
            }
            if (addContinuation)
            {
                indent.Push(IndentType.Continuation);
                addContinuation = false;
            }
            IsLineStart &= ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
            pc           = ch;
            if (ch != '\n' && ch != '\r')
            {
                col++;
            }
        }