📄 asformatter.cpp
字号:
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 breakClosingHeaderBrackets is set!
if (!breakClosingHeaderBrackets && foundClosingHeader && (bracketFormatMode == ATTACH_MODE || bracketFormatMode == BDAC_MODE) && previousNonWSChar == '}')
{
isInLineBreak = false;
appendSpacePad();
if (breakBlocks)
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() );
// added C# support
if ( ( sourceStyle != STYLE_CSHARP ) && ( newHeader == &AS_UNSAFE
|| newHeader == &AS_GET || newHeader == &AS_SET
|| newHeader == &AS_ADD || newHeader == &AS_REMOVE ) )
isNonParenHeader = false;
appendSequence(*currentHeader);
goForward(currentHeader->length() - 1);
// if padding is on, and a paren-header is found
// then add a space pad after it.
if (padOperators && !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 (breakBlocks)
{
if (previousHeader == NULL
&& !foundClosingHeader
&& !isCharImmediatelyPostOpenBlock)
{
isPrependPostBlockEmptyLineRequested = true;
}
if (currentHeader == &AS_ELSE
|| currentHeader == &AS_CATCH
|| currentHeader == &AS_FINALLY
|| foundClosingHeader)
{
isPrependPostBlockEmptyLineRequested = false;
}
if (breakClosingHeaderBlocks
&& isCharImmediatelyPostCloseBlock)
{
isPrependPostBlockEmptyLineRequested = true;
}
}
continue;
}
else if ( (newHeader = findHeader(preDefinitionHeaders)) != NULL)
{
foundPreDefinitionHeader = true;
appendSequence(*newHeader);
goForward(newHeader->length() - 1);
if (breakBlocks)
isPrependPostBlockEmptyLineRequested = true;
continue;
}
else if ( (newHeader = findHeader(preCommandHeaders)) != NULL)
{
foundPreCommandHeader = true;
appendSequence(*newHeader);
goForward(newHeader->length() - 1);
continue;
}
}
if (previousNonWSChar == '}' || currentChar == ';')
{
if (breakOneLineStatements && currentChar == ';'
&& (breakOneLineBlocks || (bracketTypeStack->back() != SINGLE_LINE_TYPE)))
{
passedSemicolon = true;
}
if (breakBlocks && currentHeader != NULL && parenStack->back() == 0)
{
isAppendPostBlockEmptyLineRequested = true;
}
if (currentChar != ';')
currentHeader = NULL; //DEVEL: is this ok?
foundQuestionMark = false;
foundPreDefinitionHeader = false;
foundPreCommandHeader = false;
isInPotentialCalculation = false;
}
if (currentChar == ':'
&& breakOneLineStatements
&& !foundQuestionMark // not in a ... ? ... : ... sequence
&& !foundPreDefinitionHeader // not in a definition block (e.g. class foo : public bar
&& previousCommandChar != ')' // not immediately after closing paren of a method header, e.g. ASFormatter::ASFormatter(...) : ASBeautifier(...)
&& previousChar != ':' // not part of '::'
&& peekNextChar() != ':') // not part of '::'
{
passedColon = true;
if (breakBlocks)
isPrependPostBlockEmptyLineRequested = true;
}
if (currentChar == '?')
foundQuestionMark = true;
if (padOperators)
{
if ((newHeader = findHeader(operators)) != NULL)
{
bool shouldPad = (newHeader != &AS_COLON_COLON
&& newHeader != &AS_PAREN_PAREN
&& newHeader != &AS_BLPAREN_BLPAREN
&& newHeader != &AS_PLUS_PLUS
&& newHeader != &AS_MINUS_MINUS
&& newHeader != &AS_NOT
&& newHeader != &AS_BIT_NOT
&& newHeader != &AS_ARROW
&& newHeader != &AS_OPERATOR
&& !(newHeader == &AS_MINUS && isInExponent())
&& !(newHeader == &AS_PLUS && isInExponent())
&& previousOperator != &AS_OPERATOR
&& !((newHeader == &AS_MULT || newHeader == &AS_BIT_AND)
&& isPointerOrReference())
&& !( (isInTemplate || isCharImmediatelyPostTemplate)
&& (newHeader == &AS_LS || newHeader == &AS_GR))
);
if (!isInPotentialCalculation)
if (find(assignmentOperators.begin(), assignmentOperators.end(), newHeader)
!= assignmentOperators.end())
isInPotentialCalculation = true;
// pad before operator
if (shouldPad
&& !(newHeader == &AS_COLON && !foundQuestionMark)
&& newHeader != &AS_SEMICOLON
&& newHeader != &AS_COMMA)
appendSpacePad();
appendSequence(*newHeader);
goForward(newHeader->length() - 1);
// since this block handles '()' and '[]',
// the parenStack must be updated here accordingly!
if (newHeader == &AS_PAREN_PAREN
|| newHeader == &AS_BLPAREN_BLPAREN)
parenStack->back()--;
currentChar = (*newHeader)[newHeader->length() - 1];
// pad after operator
// but do not pad after a '-' that is a unary-minus.
if ( shouldPad && !(newHeader == &AS_MINUS && isUnaryMinus()) )
appendSpacePad();
previousOperator = newHeader;
continue;
}
}
if (padParen)
{
if (currentChar == '(' || currentChar == '[' )
{
char peekedChar = peekNextChar();
isInPotentialCalculation = true;
appendCurrentChar();
if (!(currentChar == '(' && peekedChar == ')')
&& !(currentChar == '[' && peekedChar == ']'))
appendSpacePad();
continue;
}
else if (currentChar == ')' || currentChar == ']')
{
char peekedChar = peekNextChar();
if (!(previousChar == '(' && currentChar == ')')
&& !(previousChar == '[' && currentChar == ']'))
appendSpacePad();
appendCurrentChar();
if (peekedChar != ';' && peekedChar != ',' && peekedChar != '.'
&& !(currentChar == ']' && peekedChar == '['))
appendSpacePad();
continue;
}
}
appendCurrentChar();
}
// return a beautified (i.e. correctly indented) line.
string beautifiedLine;
int readyFormattedLineLength = trim(readyFormattedLine).size();
if (prependEmptyLine
&& readyFormattedLineLength > 0
&& previousReadyFormattedLineLength > 0)
{
TRACE( INFO, "delegation point 2" );
isLineReady = true; // signal that a readyFormattedLine is still waiting
beautifiedLine = beautify("");
}
else
{
TRACE( INFO, "delegation point 3" );
isLineReady = false;
beautifiedLine = beautify(readyFormattedLine);
}
prependEmptyLine = false;
previousReadyFormattedLineLength = readyFormattedLineLength;
return beautifiedLine;
}
/**
* check if there are any indented lines ready to be read by nextLine()
*
* @return are there any indented lines ready?
*/
bool ASFormatter::hasMoreLines() const
{
if (!isFormattingEnabled())
return ASBeautifier::hasMoreLines();
else
return !endOfCodeReached;
}
/**
* check if formatting options are enabled, in addition to indentation.
*
* @return are formatting options enabled?
*/
bool ASFormatter::isFormattingEnabled() const
{
return (bracketFormatMode != NONE_MODE
|| padOperators
|| convertTabs2Space);
}
/**
* check if a specific sequence exists in the current placement of the current line
*
* @return whether sequence has been reached.
* @param sequence the sequence to be checked
*/
bool ASFormatter::isSequenceReached(const string &sequence) const
{
return CONTAINS_AT(currentLine, sequence, sequence.size(), charNum);
}
/**
* jump over several characters.
*
* @param i the number of characters to jump over.
*/
void ASFormatter::goForward(int i)
{
while (--i >= 0)
getNextChar();
}
/**
* peek at the next unread character.
*
* @return the next unread character.
*/
char ASFormatter::peekNextChar() const
{
int peekNum = charNum + 1;
int len = currentLine.size();
char ch = ' ';
while (peekNum < len)
{
ch = currentLine[peekNum++];
if (!isWhiteSpace(ch))
return ch;
}
if (convertTabs2Space && ch == '\t')
ch = ' ';
return ch;
}
/**
* check if current placement is before a comment or line-comment
*
* @return is before a comment or line-comment.
*/
bool ASFormatter::isBeforeComment() const
{
int peekNum = charNum + 1;
int len = currentLine.size();
// char ch = ' ';
bool foundComment = false;
for (peekNum = charNum + 1;
peekNum < len && isWhiteSpace(currentLine[peekNum]);
++peekNum)
;
if (peekNum < len)
foundComment = ( CONTAINS_AT(currentLine, AS_OPEN_COMMENT, 2, peekNum)
|| CONTAINS_AT(currentLine, AS_OPEN_LINE_COMMENT, 2, peekNum) );
return foundComment;
}
/**
* get the next character, increasing the current placement in the process.
* the new character is inserted into the variable currentChar.
*
* @return whether succeded to recieve the new character.
*/
bool ASFormatter::getNextChar()
{
isInLineBreak = false;
bool isAfterFormattedWhiteSpace = false;
if (padOperators && !isInComment && !isInLineComment
&& !isInQuote && !doesLineStartComment && !isInPreprocessor
&& !isBeforeComment())
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -