📄 asbeautifier.cpp
字号:
if (isInQuote)
{
TRACE( INFO, "skipping char as it is quoted" );
continue;
}
// handle comments
if ( ! ( isInComment || isInLineComment ) && CONTAINS_AT( line, AS_OPEN_LINE_COMMENT, 2, i ) )
{
TRACE( INFO, "this starts a line comment" );
isInLineComment = true;
outBuffer.append(1, '/');
++i;
continue;
}
else if ( ! ( isInComment || isInLineComment ) && CONTAINS_AT( line, AS_OPEN_COMMENT, 2, i ) )
{
TRACE( INFO, "this starts a multiline comment" );
isInComment = true;
outBuffer.append(1, '*');
++i;
continue;
}
else if ( ( isInComment || isInLineComment ) && CONTAINS_AT( line, AS_CLOSE_COMMENT, 2, i ) )
{
TRACE( INFO, "this ends a multiline comment" );
isInComment = false;
outBuffer.append(1, '/');
++i;
continue;
}
if ( isInComment || isInLineComment )
{
TRACE( INFO, "skipping char as it is commented out." );
continue;
}
// if we have reached this far then we are NOT in a comment or string of special character...
// TODO: Haven't checked this. probationHeader?
if ( probationHeader != NULL )
{
if ( ( ( probationHeader == &AS_STATIC || probationHeader == &AS_CONST ) && ch == '{' )
|| ( probationHeader == &AS_SYNCHRONIZED && ch == '(' ) )
{
// insert the probation header as a new header
isInHeader = true;
headerStack->push_back(probationHeader);
// handle the specific probation header
isInConditional = (probationHeader == &AS_SYNCHRONIZED);
if (probationHeader == &AS_CONST)
isImmediatelyAfterConst = true;
// isInConst = true;
/* TODO:
* There is actually no more need for the global isInConst variable.
* The only reason for checking const is to see if there is a const
* immediately before an open-bracket.
* Since CONST is now put into probation and is checked during itspost-char,
* isImmediatelyAfterConst can be set by its own...
*/
isInStatement = false;
// if the probation comes from the previous line, then indent by 1 tab count.
if (previousLineProbation && ch == '{')
tabCount++;
previousLineProbation = false;
}
// dismiss the probation header
probationHeader = NULL;
}
// TODO: Haven't checked this.
prevNonSpaceCh = currentNonSpaceCh;
currentNonSpaceCh = ch;
if ( ! isLegalNameChar( ch ) && ch != ',' && ch != ';' )
{
prevNonLegalCh = currentNonLegalCh;
currentNonLegalCh = ch;
}
//if (isInConst)
//{
// isInConst = false;
// isImmediatelyAfterConst = true;
//}
// TODO: Haven't checked this.
if (isInHeader)
{
isInHeader = false;
currentHeader = headerStack->back();
}
else
{
currentHeader = NULL;
}
// handle templates
if ( ( sourceStyle != STYLE_JAVA ) && isInTemplate
&& ( ch == '<' || ch == '>' )
&& findHeader( line, i, nonAssignmentOperators ) == NULL)
{
TRACE( INFO, "isInTemplate..." ); // TODO: Extend tracing
if (ch == '<')
{
++templateDepth;
}
else if (ch == '>')
{
if (--templateDepth <= 0)
{
if (isInTemplate)
ch = ';';
else
ch = 't';
isInTemplate = false;
templateDepth = 0;
}
}
}
// handle parenthesies
if (ch == '(' || ch == '[' || ch == ')' || ch == ']')
{
if (ch == '(' || ch == '[')
{
if (parenDepth == 0)
{
parenStatementStack->push_back(isInStatement);
isInStatement = true;
}
parenDepth++;
inStatementIndentStackSizeStack->push_back(inStatementIndentStack->size());
if (currentHeader != NULL)
registerInStatementIndent(line, i, spaceTabCount, minConditionalIndent/*indentLength*2*/, true);
else
registerInStatementIndent(line, i, spaceTabCount, 0, true);
}
else if (ch == ')' || ch == ']')
{
parenDepth--;
if (parenDepth == 0)
{
isInStatement = parenStatementStack->back();
parenStatementStack->pop_back();
ch = ' ';
isInConditional = false;
}
if (!inStatementIndentStackSizeStack->empty())
{
unsigned previousIndentStackSize = inStatementIndentStackSizeStack->back();
inStatementIndentStackSizeStack->pop_back();
while (previousIndentStackSize < inStatementIndentStack->size())
inStatementIndentStack->pop_back();
if (!parenIndentStack->empty())
{
int poppedIndent = parenIndentStack->back();
parenIndentStack->pop_back();
if (i == 0)
spaceTabCount = poppedIndent;
}
}
}
continue;
}
if (ch == '{')
{
bool isBlockOpener = false;
// first, check if '{' is a block-opener or an static-array opener
isBlockOpener = ( (prevNonSpaceCh == '{' && bracketBlockStateStack->back())
|| prevNonSpaceCh == '}'
|| prevNonSpaceCh == ')'
|| prevNonSpaceCh == ';'
|| isInClassHeader
|| isBlockOpener
|| isImmediatelyAfterConst
|| (isInDefine &&
(prevNonSpaceCh == '('
|| prevNonSpaceCh == '_'
|| isalnum(prevNonSpaceCh))) );
isInClassHeader = false;
if (!isBlockOpener && currentHeader != NULL)
{
for (unsigned n=0; n < nonParenHeaders.size(); n++)
if (currentHeader == nonParenHeaders[n])
{
isBlockOpener = true;
break;
}
}
bracketBlockStateStack->push_back(isBlockOpener);
if (!isBlockOpener)
{
inStatementIndentStackSizeStack->push_back(inStatementIndentStack->size());
registerInStatementIndent(line, i, spaceTabCount, 0, true);
parenDepth++;
if (i == 0)
shouldIndentBrackettedLine = false;
continue;
}
// this bracket is a block opener...
++lineOpeningBlocksNum;
if (isInClassHeader)
isInClassHeader = false;
if (isInClassHeaderTab)
{
isInClassHeaderTab = false;
tabCount -= 2;
}
blockParenDepthStack->push_back(parenDepth);
blockStatementStack->push_back(isInStatement);
inStatementIndentStackSizeStack->push_back(inStatementIndentStack->size());
blockTabCount += isInStatement? 1 : 0;
parenDepth = 0;
isInStatement = false;
tempStacks->push_back(new vector<const string*>);
headerStack->push_back(&AS_OPEN_BRACKET);
lastLineHeader = &AS_OPEN_BRACKET; // <------
continue;
}
//check if a header has been reached
if (prevCh == ' ')
{
bool isIndentableHeader = true;
const string *newHeader = findHeader(line, i, headers);
if (newHeader != NULL)
{
// if we reached here, then this is a header...
isInHeader = true;
vector<const string*> *lastTempStack;
if (tempStacks->empty())
lastTempStack = NULL;
else
lastTempStack = tempStacks->back();
// if a new block is opened, push a new stack into tempStacks to hold the
// future list of headers in the new block.
// take care of the special case: 'else if (...)'
if (newHeader == &AS_IF && lastLineHeader == &AS_ELSE)
{
//spaceTabCount += indentLength; // to counter the opposite addition that occurs when the 'if' is registered below...
headerStack->pop_back();
}
// take care of 'else'
else if (newHeader == &AS_ELSE)
{
if (lastTempStack != NULL)
{
int indexOfIf = indexOf(*lastTempStack, &AS_IF); // <---
if (indexOfIf != -1)
{
// recreate the header list in headerStack up to the previous 'if'
// from the temporary snapshot stored in lastTempStack.
int restackSize = lastTempStack->size() - indexOfIf - 1;
for (int r=0; r<restackSize; r++)
{
headerStack->push_back(lastTempStack->back());
lastTempStack->pop_back();
}
if (!closingBracketReached)
tabCount += restackSize;
}
/*
* If the above if is not true, i.e. no 'if' before the 'else',
* then nothing beautiful will come out of this...
* I should think about inserting an Exception here to notify the caller of this...
*/
}
}
// check if 'while' closes a previous 'do'
else if (newHeader == &AS_WHILE)
{
if (lastTempStack != NULL)
{
int indexOfDo = indexOf(*lastTempStack, &AS_DO); // <---
if (indexOfDo != -1)
{
// recreate the header list in headerStack up to the previous 'do'
// from the temporary snapshot stored in lastTempStack.
int restackSize = lastTempStack->size() - indexOfDo - 1;
for (int r=0; r<restackSize; r++)
{
headerStack->push_back(lastTempStack->back());
lastTempStack->pop_back();
}
if (!closingBracketReached)
tabCount += restackSize;
}
}
}
// check if 'catch' closes a previous 'try' or 'catch'
else if (newHeader == &AS_CATCH || newHeader == &AS_FINALLY)
{
if (lastTempStack != NULL)
{
int indexOfTry = indexOf(*lastTempStack, &AS_TRY);
if (indexOfTry == -1)
indexOfTry = indexOf(*lastTempStack, &AS_CATCH);
if (indexOfTry != -1)
{
// recreate the header list in headerStack up to the previous 'try'
// from the temporary snapshot stored in lastTempStack.
int restackSize = lastTempStack->size() - indexOfTry - 1;
for (int r=0; r<restackSize; r++)
{
headerStack->push_back(lastTempStack->back());
lastTempStack->pop_back();
}
if (!closingBracketReached)
tabCount += restackSize;
}
}
}
else if (newHeader == &AS_CASE)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -