Do not perform right alignment for assignment and return if line ends with (

This commit is contained in:
Peter Vingelmann 2016-09-22 00:29:29 +02:00
parent 063a8b06b6
commit 81b77fc202
2 changed files with 64 additions and 6 deletions

View File

@ -1261,15 +1261,17 @@ void ASBeautifier::registerInStatementIndent(const string& line, int i, int spac
// text is comment-only // text is comment-only
int nextProgramChar = getNextProgramCharDistance(line, i); int nextProgramChar = getNextProgramCharDistance(line, i);
// if indent is around the last char in the line, indent instead one indent from the previous indent bool isLastProgramChar = (nextProgramChar == remainingCharNum);
if (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_; int previousIndent = spaceTabCount_;
if (!inStatementIndentStack->empty()) if (!inStatementIndentStack->empty())
previousIndent = inStatementIndentStack->back(); previousIndent = inStatementIndentStack->back();
int currIndent = /*2*/ indentLength + previousIndent; int currIndent = /*2*/ indentLength + previousIndent;
if (currIndent > maxInStatementIndent if (currIndent > maxInStatementIndent && line[i] != '{')
&& line[i] != '{')
currIndent = indentLength * 2 + spaceTabCount_; currIndent = indentLength * 2 + spaceTabCount_;
inStatementIndentStack->push_back(currIndent); inStatementIndentStack->push_back(currIndent);
if (updateParenStack) if (updateParenStack)
@ -1358,6 +1360,55 @@ pair<int, int> ASBeautifier::computePreprocessorIndent()
return entry; 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. * 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. * 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; i += foundIndentableHeader->length() - 1;
if (!isInOperator && !isInTemplate) 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; isInStatement = true;
} }
continue; continue;
@ -3598,7 +3652,10 @@ void ASBeautifier::parseCurrentLine(const string& line)
{ {
if (i == 0 && spaceIndentCount == 0) if (i == 0 && spaceIndentCount == 0)
spaceIndentCount += indentLength; 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; isInStatement = true;
} }
} }

View File

@ -339,6 +339,7 @@ protected:
const vector<const string*>* possibleHeaders) const; const vector<const string*>* possibleHeaders) const;
const string* findOperator(const string& line, int i, const string* findOperator(const string& line, int i,
const vector<const string*>* possibleOperators) const; const vector<const string*>* possibleOperators) const;
char getLastProgramChar(const string& line, int i) const;
int getNextProgramCharDistance(const string& line, int i) const; int getNextProgramCharDistance(const string& line, int i) const;
int indexOf(vector<const string*>& container, const string* element) const; int indexOf(vector<const string*>& container, const string* element) const;
void setBlockIndent(bool state); void setBlockIndent(bool state);