clang-format: Add ability to align assignment operators.

In Objective-C some style guides use a style where assignment operators are
aligned, in an effort to increase code readability. This patch adds an option
to the format library which allows this functionality. It is disabled by
default for all the included styles, so it must be explicitly enabled.

The option will change code such as:
  - (void)method {
      NSNumber *one = @1;
      NSNumber *twentyFive = @25;
  }

to:
  - (void)method {
      NSNumber *one        = @1;
      NSNumber *twentyFive = @25;
  }

Patch by Matt Oakes. Thank you!

Accidentally reformatted all the tests...

llvm-svn: 236100
This commit is contained in:
Daniel Jasper 2015-04-29 13:06:49 +00:00
parent 301f937765
commit a4499133b1
6 changed files with 579 additions and 332 deletions

View File

@ -160,6 +160,17 @@ the configuration (without a prefix: ``Auto``).
argument2);
\endcode
**AlignConsecutiveAssignments** (``bool``)
If ``true``, aligns consecutive assignments.
This will align the assignment operators of consecutive lines. This
will result in formattings like
\code
int aaaa = 12;
int b = 23;
int ccc = 23;
\endcode
**AlignEscapedNewlinesLeft** (``bool``)
If ``true``, aligns escaped newlines as far left as possible.
Otherwise puts them into the right-most column.

View File

@ -247,6 +247,17 @@ struct FormatStyle {
/// \brief If \c true, aligns trailing comments.
bool AlignTrailingComments;
/// \brief If \c true, aligns consecutive assignments.
///
/// This will align the assignment operators of consecutive lines. This
/// will result in formattings like
/// \code
/// int aaaa = 12;
/// int b = 23;
/// int ccc = 23;
/// \endcode
bool AlignConsecutiveAssignments;
/// \brief If \c true, aligns escaped newlines as far left as possible.
/// Otherwise puts them into the right-most column.
bool AlignEscapedNewlinesLeft;

View File

@ -174,6 +174,7 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
IO.mapOptional("AlignOperands", Style.AlignOperands);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
IO.mapOptional("AlignConsecutiveAssignments", Style.AlignConsecutiveAssignments);
IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
Style.AllowAllParametersOfDeclarationOnNextLine);
IO.mapOptional("AllowShortBlocksOnASingleLine",
@ -329,6 +330,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.AlignAfterOpenBracket = true;
LLVMStyle.AlignOperands = true;
LLVMStyle.AlignTrailingComments = true;
LLVMStyle.AlignConsecutiveAssignments = false;
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
LLVMStyle.AllowShortBlocksOnASingleLine = false;

View File

@ -93,6 +93,7 @@ const tooling::Replacements &WhitespaceManager::generateReplacements() {
std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
calculateLineBreakInformation();
alignConsecutiveAssignments();
alignTrailingComments();
alignEscapedNewlines();
generateChanges();
@ -141,6 +142,96 @@ void WhitespaceManager::calculateLineBreakInformation() {
}
}
// Walk through all of the changes and find sequences of "=" to align. To do
// so, keep track of the lines and whether or not an "=" was found on align. If
// a "=" is found on a line, extend the current sequence. If the current line
// cannot be part of a sequence, e.g. because there is an empty line before it
// or it contains non-assignments, finalize the previous sequence.
void WhitespaceManager::alignConsecutiveAssignments() {
if (!Style.AlignConsecutiveAssignments)
return;
unsigned MinColumn = 0;
unsigned StartOfSequence = 0;
unsigned EndOfSequence = 0;
bool FoundAssignmentOnLine = false;
bool FoundLeftParenOnLine = false;
unsigned CurrentLine = 0;
auto AlignSequence = [&] {
alignConsecutiveAssignments(StartOfSequence, EndOfSequence, MinColumn);
MinColumn = 0;
StartOfSequence = 0;
EndOfSequence = 0;
};
for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
if (Changes[i].NewlinesBefore != 0) {
CurrentLine += Changes[i].NewlinesBefore;
if (StartOfSequence > 0 &&
(Changes[i].NewlinesBefore > 1 || !FoundAssignmentOnLine)) {
EndOfSequence = i;
AlignSequence();
}
FoundAssignmentOnLine = false;
FoundLeftParenOnLine = false;
}
if ((Changes[i].Kind == tok::equal &&
(FoundAssignmentOnLine || ((Changes[i].NewlinesBefore > 0 ||
Changes[i + 1].NewlinesBefore > 0)))) ||
(!FoundLeftParenOnLine && Changes[i].Kind == tok::r_paren)) {
if (StartOfSequence > 0)
AlignSequence();
} else if (Changes[i].Kind == tok::l_paren) {
FoundLeftParenOnLine = true;
if (!FoundAssignmentOnLine && StartOfSequence > 0)
AlignSequence();
} else if (!FoundAssignmentOnLine && !FoundLeftParenOnLine &&
Changes[i].Kind == tok::equal) {
FoundAssignmentOnLine = true;
EndOfSequence = i;
if (StartOfSequence == 0)
StartOfSequence = i;
unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
MinColumn = std::max(MinColumn, ChangeMinColumn);
}
}
if (StartOfSequence > 0) {
EndOfSequence = Changes.size();
AlignSequence();
}
}
void WhitespaceManager::alignConsecutiveAssignments(unsigned Start,
unsigned End,
unsigned Column) {
bool AlignedAssignment = false;
int PreviousShift = 0;
for (unsigned i = Start; i != End; ++i) {
int Shift = 0;
if (Changes[i].NewlinesBefore > 0)
AlignedAssignment = false;
if (!AlignedAssignment && Changes[i].Kind == tok::equal) {
Shift = Column - Changes[i].StartOfTokenColumn;
AlignedAssignment = true;
PreviousShift = Shift;
}
assert(Shift >= 0);
Changes[i].Spaces += Shift;
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += Shift;
Changes[i].StartOfTokenColumn += Shift;
if (AlignedAssignment) {
Changes[i].StartOfTokenColumn += PreviousShift;
if (i + 1 != Changes.size())
Changes[i + 1].PreviousEndOfTokenColumn += PreviousShift;
}
}
}
void WhitespaceManager::alignTrailingComments() {
unsigned MinColumn = 0;
unsigned MaxColumn = UINT_MAX;

View File

@ -164,6 +164,13 @@ private:
/// \c EscapedNewlineColumn for the first tokens or token parts in a line.
void calculateLineBreakInformation();
/// \brief Align consecutive assignments over all \c Changes.
void alignConsecutiveAssignments();
/// \brief Align consecutive assignments from change \p Start to change \p End at
/// the specified \p Column.
void alignConsecutiveAssignments(unsigned Start, unsigned End, unsigned Column);
/// \brief Align trailing comments over all \c Changes.
void alignTrailingComments();

File diff suppressed because it is too large Load Diff