⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asformatter.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 4 页
字号:
            continue;
        }


        // handle white space or preprocessor statements (simplifies the rest)
        if ( isWhiteSpace( currentChar ) || isInPreprocessor )
        {
            // TODO: stale comment: if ( isLegalNameChar( previousChar ) && isLegalNameChar( peekNextChar() ) )
            appendCurrentChar();
            continue;
        }

        // detect BEGIN of comments or quotes
        if ( isSequenceReached( AS_OPEN_LINE_COMMENT ) )
        {
            TRACE( INFO, "beginning of line comment encountered" );
            isInLineComment = true;
            if ( padOperators )
            {
                appendSpacePad();
            }
            appendSequence( AS_OPEN_LINE_COMMENT );
            goForward( 1 );
            continue;
        }
        else if ( isSequenceReached( AS_OPEN_COMMENT ) )
        {
            TRACE( INFO, "beginning of multi-line comment encountered" );
            isInComment = true;
            if ( padOperators )
            {
                appendSpacePad();
            }
            appendSequence( AS_OPEN_COMMENT );
            goForward( 1 );
            continue;
        }
        else if ( currentChar == '"' || currentChar == '\'' )
        {
            TRACE( INFO, "beginning of quote encountered" );
            isInQuote = true;
            quoteChar = currentChar;
            // if (padOperators)     // BUGFIX: these two lines removed. seem to be unneeded, and interfere with L"
            //     appendSpacePad(); // BUGFIX: TODO: make sure the removal of these lines doesn't reopen old bugs...
            appendCurrentChar();
            continue;
        }

        // not in quote or comment or white-space of any type ...

        // check if in preprocessor
        // ** isInPreprocessor will be automatically reset at the begining of a new line in getNextChar()
        if ( currentChar == '#' )
        {
            TRACE( INFO, "beginning of preprocessor statement encountered" );
            isInPreprocessor = true;
        }
        // TODO: Should not be reached; candidate for removal
        if ( isInPreprocessor )
        {
            TRACE( INFO, "being inside preprocessor. IS THIS EVER ACTUALLY REACHED?" );
            appendCurrentChar();
            continue;
        }

        // not in preprocessor ...
        if ( isImmediatelyPostComment )
        {
            TRACE( INFO, "isImmediatelyPostComment" );
            isImmediatelyPostComment = false;
            isCharImmediatelyPostComment = true;
        }

        if ( isImmediatelyPostLineComment )
        {
            TRACE( INFO, "isImmediatelyPostLineComment" );
            isImmediatelyPostLineComment = false;
            isCharImmediatelyPostLineComment = true;
        }

        if ( shouldBreakLineAfterComments )
        {
            TRACE( INFO, "breaking line after comments" );
            shouldBreakLineAfterComments = false;
            shouldReparseCurrentChar = true;
            breakLine();
            continue;
        }

        if ( isImmediatelyPostHeader )
        {
            TRACE( INFO, "isImmediatelyPostHeader" );

            // reset post-header information
            isImmediatelyPostHeader = false;
            isCharImmediatelyPostHeader = true;

            // If configured, break headers from their succeeding blocks.
            // Make sure that else if()'s are excepted unless configured
            // to be broken too.
            if ( breakOneLineStatements && 
                 ( breakElseIfs || 
                 ( currentHeader != &AS_ELSE || findHeader( headers ) != &AS_IF ) ) )
            {
                TRACE( INFO, "breaking line after header" );
                isInLineBreak = true;
            }
        }

        if ( passedSemicolon )
        {
            TRACE( INFO, "passedSemicolon" );
            passedSemicolon = false;
            if ( parenStack->back() == 0 )
            {
                TRACE( INFO, "parenStack->back() == 0" );
                shouldReparseCurrentChar = true;
                isInLineBreak = true;
                continue;
            }
        }

        if ( passedColon )
        {
            TRACE( INFO, "passedColon" );
            passedColon = false;
            if ( parenStack->back() == 0 )
            {
                TRACE( INFO, "parenStack->back() == 0" );
                shouldReparseCurrentChar = true;
                isInLineBreak = true;
                continue;
            }
        }

        // Check if in template declaration, e.g. foo<bar> or foo<bar,fig>
        if ( ! isInTemplate && currentChar == '<' )
        {
            TRACE( ENTRY, "checking for template..." );
            int templateDepth = 0; // TODO: hides astyle::ASBeautifier::templateDepth
            const string *oper;
            for ( unsigned i = charNum; i < currentLine.size(); i += ( oper ? oper->length() : 1 ) )
            {
                oper = ASBeautifier::findHeader( currentLine, i, operators );

                if ( oper == &AS_LS )
                {
                    templateDepth++;
                }
                else if ( oper == &AS_GR )
                {
                    templateDepth--;
                    if ( templateDepth == 0 )
                    {
                        TRACE( EXIT, "template encountered!" );
                        isInTemplate = true;
                        break;
                    }
                }
                else if ( oper == &AS_COMMA            // comma,     e.g. A<int, char>
                       || oper == &AS_BIT_AND          // reference, e.g. A<int&>
                       || oper == &AS_MULT             // pointer,   e.g. A<int*>
                       || oper == &AS_COLON_COLON)     // ::,        e.g. std::string
                {
                    continue;
                }
                else if ( ! isLegalNameChar( currentLine[i] ) && ! isWhiteSpace( currentLine[i] ) )
                {
                    TRACE( EXIT, "false alarm - not a template" );
                    isInTemplate = false;
                    break;
                }
            }
        }

        // handle parenthesies
        if ( currentChar == '(' || currentChar == '[' || ( isInTemplate && currentChar == '<' ) )
        {
            TRACE( ENTRY, "opening paren '" << currentChar << "' encountered." );
            parenStack->back()++;
        }
        else if ( currentChar == ')' || currentChar == ']' || ( isInTemplate && currentChar == '>' ) )
        {
            TRACE( EXIT, "closing paren '" << currentChar << "' encountered." );
            parenStack->back()--;

            if ( isInTemplate && parenStack->back() == 0 )
            {
                TRACE( INFO, "(also marks end of template)" );
                isInTemplate = false;
                isCharImmediatelyPostTemplate = true;
            }

            // check if this parenthesis closes a header, e.g. if (...), while (...)
            if ( isInHeader && parenStack->back() == 0 )
            {
                TRACE( INFO, "(also marks end of header)" );
                isInHeader = false;
                isImmediatelyPostHeader = true;
            }

        }

        // handle brackets
        BracketType bracketType = NULL_TYPE;

        if ( currentChar == '{' )
        {
            TRACE( ENTRY, "opening bracket encountered" );
            bracketType = getBracketType();
            foundPreDefinitionHeader = false;
            foundPreCommandHeader = false;

            bracketTypeStack->push_back(bracketType);
            preBracketHeaderStack->push_back(currentHeader);
            currentHeader = NULL;

            isPreviousBracketBlockRelated = (bracketType != ARRAY_TYPE);
        }
        else if ( currentChar == '}' )
        {
            TRACE( EXIT, "closing bracket encountered" );

            // if a request has been made to append a post block empty line,
            // but the block exists immediately before a closing bracket,
            // then there is no need for the empty line.
            isAppendPostBlockEmptyLineRequested = false;

            if ( ! bracketTypeStack->empty() )
            {
                TRACE( INFO, "popping from bracketTypeStack..." );
                bracketType = bracketTypeStack->back();
                bracketTypeStack->pop_back();

                isPreviousBracketBlockRelated = ( bracketType != ARRAY_TYPE );
            }

            if ( ! preBracketHeaderStack->empty() )
            {
                TRACE( INFO, "popping from preBracketHeaderStack..." );
                currentHeader = preBracketHeaderStack->back();
                preBracketHeaderStack->pop_back();
            }
            else
            {
                TRACE( INFO, "currentHeader = NULL" );
                currentHeader = NULL;
            }
        }

        if ( bracketType != ARRAY_TYPE )
        {
            TRACE( INFO, "bracket is not of ARRAY_TYPE" );

            if ( currentChar == '{' )
            {
                TRACE( INFO, "pushing zero to parenStack" );
                parenStack->push_back(0);
            }
            else if ( currentChar == '}' && ! parenStack->empty() )
            {
                TRACE( INFO, "popping from parenStack" );
                parenStack->pop_back();
            }

            if ( bracketFormatMode != NONE_MODE )
            {
                TRACE( INFO, "bracketFormatMode != NONE_MODE" );
                
                // TODO: Haven't looked at this yet.
                if ( currentChar == '{' )
                {
                    if ( ( bracketFormatMode == ATTACH_MODE
                            || bracketFormatMode == BDAC_MODE && bracketTypeStack->size() >= 2
                            && ( (*bracketTypeStack)[ bracketTypeStack->size() - 2 ] == COMMAND_TYPE ) /*&& isInLineBreak*/)
                            && ! isCharImmediatelyPostLineComment )
                    {
                        appendSpacePad();
                        if ( ! isCharImmediatelyPostComment   // do not attach '{' to lines that end with /**/ comments.
                               && previousCommandChar != '{'
                               && previousCommandChar != '}'
                               && previousCommandChar != ';') // '}' , ';' chars added for proper handling of '{' immediately after a '}' or ';'
                        {
                            appendCurrentChar(false);
                        }
                        else
                        {
                            appendCurrentChar(true);
                        }
                        continue;
                    }
                    else if ( bracketFormatMode == BREAK_MODE
                               || bracketFormatMode == BDAC_MODE && bracketTypeStack->size() >= 2
                               && ( (*bracketTypeStack)[ bracketTypeStack->size() - 2 ] == DEFINITION_TYPE))
                    {
                        if ( breakOneLineBlocks || (bracketType != SINGLE_LINE_TYPE) )
                        {
                            breakLine();
                        }
                        appendCurrentChar();
                        continue;
                    }
                }
                else if ( currentChar == '}' )
                {
                    // bool origLineBreak = isInLineBreak;

                    // mark state of immediately after empty block
                    // this state will be used for locating brackets that appear immedately AFTER an empty block (e.g. '{} \n}').
                    if ( previousCommandChar == '{' )
                    {
                        isImmediatelyPostEmptyBlock = true;
                    }

                    if ( ( ! ( previousCommandChar == '{' && isPreviousBracketBlockRelated ) ) // this '{' does not close an empty block
                          && ( breakOneLineBlocks || ( bracketType != SINGLE_LINE_TYPE ) )     // astyle is allowed to break on line blocks
                          && ! isImmediatelyPostEmptyBlock)                                    // this '}' does not immediately follow an empty block
                    {
                        breakLine();
                        appendCurrentChar();
                    }
                    else
                    {
                        if ( ! isCharImmediatelyPostComment )
                        {
                            isInLineBreak = false;
                        }
                        appendCurrentChar();
                        if ( breakOneLineBlocks || ( bracketType != SINGLE_LINE_TYPE ) )
                        {
                            shouldBreakLineAfterComments = true;
                        }
                    }

                    if ( breakBlocks )
                    {
                        isAppendPostBlockEmptyLineRequested =true;
                    }

                    continue;
                }
            }
        } // bracketType != ARRAY_TYPE

        // TODO: What's this? An attempt at the longest conditional in the world?
        if ( ( ( previousCommandChar == '{' && isPreviousBracketBlockRelated)
            || ( previousCommandChar == '}' && ! isImmediatelyPostEmptyBlock   // <--
                                            && isPreviousBracketBlockRelated
                                            && ! isPreviousCharPostComment    // <-- Fixes wrongly appended newlines after '}' immediately after comments... 10/9/1999
                                            && peekNextChar() != ' ' ) )
            && ( breakOneLineBlocks || ( bracketTypeStack->back() != SINGLE_LINE_TYPE ) ) )
        {
            isCharImmediatelyPostOpenBlock = (previousCommandChar == '{');
            isCharImmediatelyPostCloseBlock = (previousCommandChar == '}');

            previousCommandChar = ' ';
            isInLineBreak = true;  // <----
        }

        // reset block handling flag
        isImmediatelyPostEmptyBlock = false;

        // look for headers
        if ( ! isInTemplate )
        {
            if ( (newHeader = findHeader(headers)) != NULL)
            {
                foundClosingHeader = false;
                const string *previousHeader;

                // recognize closing headers of do..while, if..else, try..catch..finally

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -