From 29a4b8d4a63e15e425cefc4bb28fae83eb5eb45c Mon Sep 17 00:00:00 2001 From: Peter Vingelmann Date: Fri, 23 Sep 2016 23:15:50 +0200 Subject: [PATCH] Basic handling for initializer blocks in statements --- src/ASBeautifier.cpp | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/ASBeautifier.cpp b/src/ASBeautifier.cpp index 077ab68..4c044a0 100644 --- a/src/ASBeautifier.cpp +++ b/src/ASBeautifier.cpp @@ -2869,7 +2869,7 @@ void ASBeautifier::parseCurrentLine(const string& line) if (isInClassInitializer || isInEnumTypeID) { // decrease tab count if bracket is broken - if (lineBeginsWithOpenBracket) + if (lineBeginsWithOpenBracket && parenDepth == 0) { indentCount -= classInitializerIndents; // decrease one more if an empty class @@ -2908,32 +2908,31 @@ void ASBeautifier::parseCurrentLine(const string& line) if (!inStatementIndentStack->empty()) { - // completely purge the inStatementIndentStack - while (!inStatementIndentStack->empty()) - popLastInStatementIndent(); - if (isInClassInitializer || isInClassHeaderTab) - { - if (lineBeginsWithOpenBracket || lineBeginsWithComma) - spaceIndentCount = 0; - } - // Do not zero the spaceIndentCount for the current line - // if we are within a statement. This is useful for lambda - // expressions in multiline statements: - // - // std::generate(data.begin(), data.end(), - // [&]() { return randval(engine); }); - else if (!isInStatement || lineBeginsWithOpenBracket) + bool initializerBlock = + (isInStatement && (prevNonSpaceCh == ',' || prevNonSpaceCh == '(')); + + // Purge the inStatementIndentStack if the line begins with '{' + // and this is not a possible initializer block in a statement, + // for example: function(arg, { 1, 2, 3, 4 }); + if (lineBeginsWithOpenBracket && !initializerBlock) + { + while (!inStatementIndentStack->empty()) + popLastInStatementIndent(); spaceIndentCount = 0; + } } blockTabCount += (isInStatement ? 1 : 0); if (g_preprocessorCppExternCBracket == 3) ++g_preprocessorCppExternCBracket; + if (parenDepth == 0) + { + isInClassInitializer = false; + isInEnumTypeID = false; + } parenDepth = 0; isInClassHeader = false; isInClassHeaderTab = false; - isInClassInitializer = false; - isInEnumTypeID = false; isInStatement = false; isInQuestion = false; isInLet = false; @@ -3318,9 +3317,6 @@ void ASBeautifier::parseCurrentLine(const string& line) ++lineClosingBlocksNum; - if (!inStatementIndentStackSizeStack->empty()) - popLastInStatementIndent(); - if (!blockParenDepthStack->empty()) { parenDepth = blockParenDepthStack->back(); @@ -3333,7 +3329,7 @@ void ASBeautifier::parseCurrentLine(const string& line) } closingBracketReached = true; - if (i == 0) + if (i == 0 && !isInStatement) spaceIndentCount = 0; isInAsmBlock = false; isInAsm = isInAsmOneLine = isInQuote = false; // close these just in case