예제 #1
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(NoWhitespacePredicate);
                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);
                    EnsureText(prevOffset, next, "");
                    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);
                    EnsureText(prevOffset, braceOffset2, " ");
                }
                break;

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

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

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

            case BraceStyle.NextLineShifted2:
                prev = lbrace.GetPrevNode(NoWhitespacePredicate);
                if (prev is PreProcessorDirective)
                {
                    return;
                }
                prevOffset  = document.GetOffset(prev.EndLocation);
                braceOffset = document.GetOffset(lbrace.StartLocation);
                curIndent.Push(IndentType.Block);
                EnsureText(prevOffset, braceOffset, options.EolMarker + curIndent.IndentString);
                curIndent.Pop();
                break;
            }
        }
예제 #2
0
        public void Push(char ch)
        {
            if (readPreprocessorExpression) {
                wordBuf.Append(ch);
            }

            if (inside.HasFlag(Inside.VerbatimString) && pc == '"' && ch != '"') {
                inside &= ~Inside.StringLiteral;
            }
            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 ' ':
                    currentIndent.Append(' ');
                    break;
                case '\t':
                    var nextTabStop = (col - 1 + textEditorOptions.IndentSize) / textEditorOptions.IndentSize;
                    col = 1 + nextTabStop * textEditorOptions.IndentSize;
                    currentIndent.Append('\t');
                    offset++;
                    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;
                    indent.Push(indentDelta);
                    indentDelta = new Indent(textEditorOptions);

                    if (addContinuation) {
                        indent.Push(IndentType.Continuation);
                    }
                    thisLineindent = indent.Clone();
                    addContinuation = false;
                    IsLineStart = true;
                    readPreprocessorExpression = false;
                    col = 1;
                    line++;
                    currentIndent.Length = 0;
                    break;
                case '\n':
                    if (pc == '\r')
                        break;
                    goto case '\r';
                case '"':
                    if (IsInComment || IsInPreProcessorComment)
                        break;
                    if (inside.HasFlag(Inside.StringLiteral)) {
                        if (pc != '\\')
                            inside &= ~Inside.StringLiteral;
                        break;
                    }

                    if (pc == '@') {
                        inside |= Inside.VerbatimString;
                    } else {
                        inside |= Inside.StringLiteral;
                    }
                    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.Count > 0)
                        parenStack.Pop();
                    if (indent.Count > 0)
                        indent.Pop();
                    indent.ExtraSpaces = 0;
                    break;
                case ',':
                    if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                        break;
                    if (parenStack.Count > 0 && parenStack.Peek().Line == line) {
                        if (indent.Count > 0)
                            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;
                    if (indentDelta.CurIndent > 0) {
                        indentDelta.Pop();
                        if (indentDelta.Count > 0 && indentDelta.Peek() == IndentType.Continuation)
                            indentDelta.Pop();
                    } else {
                        if (thisLineindent.Count > 0)
                            thisLineindent.Pop();
                        if (indent.Count > 0)
                            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.StringLiteral) || 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++;
            offset++;
        }
예제 #3
0
        public void Push(char ch)
        {
            if (readPreprocessorExpression)
            {
                wordBuf.Append(ch);
            }

            if (inside.HasFlag(Inside.VerbatimString) && pc == '"' && ch != '"')
            {
                inside &= ~Inside.StringLiteral;
            }
            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 ' ':
                currentIndent.Append(' ');
                break;

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

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

                if (pc == '@')
                {
                    inside |= Inside.VerbatimString;
                }
                else
                {
                    inside |= Inside.StringLiteral;
                }
                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.Count > 0)
                {
                    parenStack.Pop();
                }
                if (indent.Count > 0)
                {
                    indent.Pop();
                }
                indent.ExtraSpaces = 0;
                break;

            case ',':
                if (IsInComment || IsInStringOrChar || IsInPreProcessorComment)
                {
                    break;
                }
                if (parenStack.Count > 0 && parenStack.Peek().Line == line)
                {
                    if (indent.Count > 0)
                    {
                        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;
                }
                if (indentDelta.CurIndent > 0)
                {
                    indentDelta.Pop();
                    if (indentDelta.Count > 0 && indentDelta.Peek() == IndentType.Continuation)
                    {
                        indentDelta.Pop();
                    }
                }
                else
                {
                    if (thisLineindent.Count > 0)
                    {
                        thisLineindent.Pop();
                    }
                    if (indent.Count > 0)
                    {
                        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.StringLiteral) || IsInPreProcessorComment)
                {
                    break;
                }
                if (inside.HasFlag(Inside.CharLiteral))
                {
                    if (pc != '\\')
                    {
                        inside &= ~Inside.CharLiteral;
                    }
                }
                else
                {
                    inside &= Inside.CharLiteral;
                }
                break;

            default:
                var nl = NewLine.GetDelimiterLength(ch, pc);
                if (nl == 2)
                {
                    break;
                }
                if (nl == 1)
                {
                    if (readPreprocessorExpression)
                    {
                        if (!eval(wordBuf.ToString()))
                        {
                            inside |= Inside.PreProcessorComment;
                        }
                    }

                    inside &= ~(Inside.Comment | Inside.String | Inside.CharLiteral | Inside.PreProcessor);
                    CheckKeyword(wordBuf.ToString());
                    wordBuf.Length = 0;
                    indent.Push(indentDelta);
                    indentDelta = new Indent(textEditorOptions);


                    if (addContinuation)
                    {
                        indent.Push(IndentType.Continuation);
                    }
                    thisLineindent             = indent.Clone();
                    addContinuation            = false;
                    IsLineStart                = true;
                    readPreprocessorExpression = false;
                    col = 1;
                    line++;
                    currentIndent.Length = 0;
                }
                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' || NewLine.IsNewLine(ch);
            pc           = ch;
            if (!NewLine.IsNewLine(ch))
            {
                col++;
            }
            offset++;
        }