📄 asformatter.cpp
字号:
{
isInComment = true;
if (shouldPadOperators)
appendSpacePad();
appendSequence(AS_OPEN_COMMENT);
goForward(1);
continue;
}
else if (currentChar == '"' || currentChar == '\'')
{
isInQuote = true;
quoteChar = currentChar;
//// if (shouldPadOperators) // BUGFIX: these two lines removed. seem to be unneeded, and interfere with L"
//// appendSpacePad(); // BUFFIX: 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 == '#')
isInPreprocessor = true;
if (isInPreprocessor)
{
appendCurrentChar();
continue;
}
/* not in preprocessor ... */
if (isImmediatelyPostComment)
{
isImmediatelyPostComment = false;
isCharImmediatelyPostComment = true;
}
if (isImmediatelyPostLineComment)
{
isImmediatelyPostLineComment = false;
isCharImmediatelyPostLineComment = true;
}
if (shouldBreakLineAfterComments)
{
shouldBreakLineAfterComments = false;
shouldReparseCurrentChar = true;
breakLine();
continue;
}
// reset isImmediatelyPostHeader information
if (isImmediatelyPostHeader)
{
isImmediatelyPostHeader = false;
isCharImmediatelyPostHeader = true;
// Make sure headers are broken from their succeeding blocks
// (e.g.
// if (isFoo) DoBar();
// should become
// if (isFoo)
// DoBar;
// )
// But treat else if() as a special case which should not be broken!
if (shouldBreakOneLineStatements)
{
// if may break 'else if()'s, ythen simply break the line
if (shouldBreakElseIfs)
isInLineBreak = true;
else
{
// make sure 'else if()'s are not broken.
bool isInElseIf = false;
const string *upcomingHeader;
upcomingHeader = findHeader(headers);
if (currentHeader == &AS_ELSE && upcomingHeader == &AS_IF)
isInElseIf = true;
if (!isInElseIf)
isInLineBreak = true; ////BUGFIX: SHOULD NOT BE breakLine() !!!
}
}
}
if (passedSemicolon)
{
passedSemicolon = false;
if (parenStack->back() == 0)
{
shouldReparseCurrentChar = true;
isInLineBreak = true;
continue;
}
}
if (passedColon)
{
passedColon = false;
if (parenStack->back() == 0)
{
shouldReparseCurrentChar = true;
isInLineBreak = true;
continue;
}
}
// Check if in template declaration, e.g. foo<bar> or foo<bar,fig>
// If so, set isInTemplate to true
//
if (!isInTemplate && currentChar == '<')
{
int templateDepth = 0;
const string *oper;
for ( int i=charNum;
i< currentLine.length();
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)
{
// this is a template!
//
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]))
{
// this is not a template -> leave...
//
isInTemplate = false;
break;
}
}
}
// handle parenthesies
//
if (currentChar == '(' || currentChar == '[' || (isInTemplate && currentChar == '<'))
{
parenStack->back()++;
}
else if (currentChar == ')' || currentChar == ']' || (isInTemplate && currentChar == '>'))
{
parenStack->back()--;
if (isInTemplate && parenStack->back() == 0)
{
isInTemplate = false;
isCharImmediatelyPostTemplate = true;
}
// check if this parenthesis closes a header, e.g. if (...), while (...)
//
if (isInHeader && parenStack->back() == 0)
{
isInHeader = false;
isImmediatelyPostHeader = true;
}
}
// handle brackets
//
BracketType bracketType = NULL_TYPE;
if (currentChar == '{')
{
bracketType = getBracketType();
foundPreDefinitionHeader = false;
foundPreCommandHeader = false;
bracketTypeStack->push_back(bracketType);
preBracketHeaderStack->push_back(currentHeader);
currentHeader = NULL;
isPreviousBracketBlockRelated = !IS_A(bracketType, ARRAY_TYPE);
}
else if (currentChar == '}')
{
// 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 not need for the post block empty line.
//
isAppendPostBlockEmptyLineRequested = false;
if (!bracketTypeStack->empty())
{
bracketType = bracketTypeStack->back();
bracketTypeStack->pop_back();
isPreviousBracketBlockRelated = !IS_A(bracketType, ARRAY_TYPE);
}
if (!preBracketHeaderStack->empty())
{
currentHeader = preBracketHeaderStack->back();
preBracketHeaderStack->pop_back();
}
else
currentHeader = NULL;
}
if (!IS_A(bracketType, ARRAY_TYPE))
{
if (currentChar == '{')
{
parenStack->push_back(0);
}
else if (currentChar == '}')
{
if (!parenStack->empty())
{
parenStack->pop_back();
}
}
if (bracketFormatMode != NONE_MODE)
{
if (currentChar == '{')
{
if ( ( bracketFormatMode == ATTACH_MODE
|| bracketFormatMode == BDAC_MODE && bracketTypeStack->size()>=2
&& IS_A((*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
&& IS_A((*bracketTypeStack)[bracketTypeStack->size()-2], DEFINITION_TYPE))
{
if ( shouldBreakOneLineBlocks || !IS_A(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
&& (shouldBreakOneLineBlocks || !IS_A(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 (shouldBreakOneLineBlocks || !IS_A(bracketType, SINGLE_LINE_TYPE))
shouldBreakLineAfterComments = true;
}
if (shouldBreakBlocks)
{
isAppendPostBlockEmptyLineRequested =true;
}
continue;
}
}
}
if ( ( (previousCommandChar == '{'
&& isPreviousBracketBlockRelated)
|| (previousCommandChar == '}'
&& !isImmediatelyPostEmptyBlock // <--
&& isPreviousBracketBlockRelated
&& !isPreviousCharPostComment // <-- Fixes wrongly appended newlines after '}' immediately after comments... 10/9/1999
&& peekNextChar() != ' '))
&& (shouldBreakOneLineBlocks || !IS_A(bracketTypeStack->back(), SINGLE_LINE_TYPE)) )
{
isCharImmediatelyPostOpenBlock = (previousCommandChar == '{');
isCharImmediatelyPostCloseBlock = (previousCommandChar == '}');
previousCommandChar = ' ';
isInLineBreak = true; //<----
}
// reset block handling flags
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
if ( (newHeader == &AS_ELSE && currentHeader == &AS_IF)
|| (newHeader == &AS_WHILE && currentHeader == &AS_DO)
|| (newHeader == &AS_CATCH && currentHeader == &AS_TRY)
|| (newHeader == &AS_CATCH && currentHeader == &AS_CATCH)
|| (newHeader == &AS_FINALLY && currentHeader == &AS_TRY)
|| (newHeader == &AS_FINALLY && currentHeader == &AS_CATCH) )
foundClosingHeader = true;
previousHeader = currentHeader;
currentHeader = newHeader;
// If in ATTACH or LINUX bracket modes, attach closing headers (e.g. 'else', 'catch')
// to their preceding bracket,
// But do not perform the attachment if the shouldBreakClosingHeaderBrackets is set!
if (!shouldBreakClosingHeaderBrackets && foundClosingHeader && (bracketFormatMode == ATTACH_MODE || bracketFormatMode == BDAC_MODE) && previousNonWSChar == '}')
{
isInLineBreak = false;
appendSpacePad();
if (shouldBreakBlocks)
isAppendPostBlockEmptyLineRequested = false;
}
//Check if a template definition as been reached, e.g. template<class A>
if (newHeader == &AS_TEMPLATE)
{
isInTemplate = true;
}
// check if the found header is non-paren header
isNonParenHeader = ( find(nonParenHeaders.begin(), nonParenHeaders.end(),
newHeader) != nonParenHeaders.end() );
appendSequence(*currentHeader);
goForward(currentHeader->length() - 1);
// if padding is on, and a paren-header is found
// then add a space pad after it.
if (shouldPadOperators && !isNonParenHeader)
appendSpacePad();
// Signal that a header has been reached
// *** But treat a closing while() (as in do...while)
// as if it where NOT a header since a closing while()
// should never have a block after it!
if (!(foundClosingHeader && currentHeader == &AS_WHILE))
{
isInHeader = true;
if (isNonParenHeader)
{
isImmediatelyPostHeader = true;
isInHeader = false;
}
}
if (currentHeader == &AS_IF && previousHeader == &AS_ELSE)
isInLineBreak = false;
if (shouldBreakBlocks)
{
if (previousHeader == NULL
&& !foundClosingHeader
&& !isCharImmediatelyPostOpenBlock)
{
isPrependPostBlockEmptyLineRequested = true;
}
if (currentHeader == &AS_ELSE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -