diff --git a/src/ASBeautifier.cpp b/src/ASBeautifier.cpp index 9d75080..bb16bb8 100644 --- a/src/ASBeautifier.cpp +++ b/src/ASBeautifier.cpp @@ -1261,15 +1261,17 @@ void ASBeautifier::registerInStatementIndent(const string& line, int i, int spac // text is comment-only int nextProgramChar = getNextProgramCharDistance(line, i); - // if indent is around the last char in the line, indent instead one indent from the previous indent - if (nextProgramChar == remainingCharNum) + bool isLastProgramChar = (nextProgramChar == remainingCharNum); + + // if indent is around the last char in the line or the line ends with '(', + // then add only one indent from the previous indent + if (isLastProgramChar || getLastProgramChar(line, i) == '(') { int previousIndent = spaceTabCount_; if (!inStatementIndentStack->empty()) previousIndent = inStatementIndentStack->back(); int currIndent = /*2*/ indentLength + previousIndent; - if (currIndent > maxInStatementIndent - && line[i] != '{') + if (currIndent > maxInStatementIndent && line[i] != '{') currIndent = indentLength * 2 + spaceTabCount_; inStatementIndentStack->push_back(currIndent); if (updateParenStack) @@ -1358,6 +1360,55 @@ pair ASBeautifier::computePreprocessorIndent() return entry; } +/** + * Get the last non-whitespace, non-comment character in the line. + * if no such character exists, return ' ' + * + * @param i the search starts from this index (must be a program char). + */ +char ASBeautifier::getLastProgramChar(const string& line, int i) const +{ + bool inComment = false; + int remainingCharNum = line.length() - i; + char lastProgramChar = ' '; + int charDistance; + char ch; + + for (charDistance = 0; charDistance < remainingCharNum; charDistance++) + { + ch = line[i + charDistance]; + if (inComment) + { + if (line.compare(i + charDistance, 2, "*/") == 0) + { + charDistance++; + inComment = false; + } + continue; + } + else if (ch == '/') + { + if (line.compare(i + charDistance, 2, "//") == 0) + return lastProgramChar; + if (line.compare(i + charDistance, 2, "/*") == 0) + { + charDistance++; + inComment = true; + } + else + { + lastProgramChar = ch; + } + } + else if (!isWhiteSpace(ch)) + { + lastProgramChar = ch; + } + } + + return lastProgramChar; +} + /** * get distance to the next non-white space, non-comment character in the line. * if no such character exists, return the length remaining to the end of the line. @@ -3399,7 +3450,10 @@ void ASBeautifier::parseCurrentLine(const string& line) i += foundIndentableHeader->length() - 1; if (!isInOperator && !isInTemplate) { - registerInStatementIndent(line, i, spaceIndentCount, tabIncrementIn, 0, false); + // Do not align on the right side of the return statement + // if this is a continuation line that ends with '(' + if (getLastProgramChar(line, i) != '(') + registerInStatementIndent(line, i, spaceIndentCount, tabIncrementIn, 0, false); isInStatement = true; } continue; @@ -3598,7 +3652,10 @@ void ASBeautifier::parseCurrentLine(const string& line) { if (i == 0 && spaceIndentCount == 0) spaceIndentCount += indentLength; - registerInStatementIndent(line, i, spaceIndentCount, tabIncrementIn, 0, false); + // Do not align on the right side of the '=' sign + // if this is a continuation line that ends with '(' + if (getLastProgramChar(line, i) != '(') + registerInStatementIndent(line, i, spaceIndentCount, tabIncrementIn, 0, false); isInStatement = true; } } diff --git a/src/astyle.h b/src/astyle.h index 6802104..6275dd5 100644 --- a/src/astyle.h +++ b/src/astyle.h @@ -339,6 +339,7 @@ protected: const vector* possibleHeaders) const; const string* findOperator(const string& line, int i, const vector* possibleOperators) const; + char getLastProgramChar(const string& line, int i) const; int getNextProgramCharDistance(const string& line, int i) const; int indexOf(vector& container, const string* element) const; void setBlockIndent(bool state);