summaryrefslogtreecommitdiffstats
path: root/lib/Format/FormatToken.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2014-09-19 08:28:43 +0000
committerDaniel Jasper <djasper@google.com>2014-09-19 08:28:43 +0000
commit2cc82f4c7ecdfe3ddf6adc5a005f2e005d556c04 (patch)
treeef4f9edd203c588a41a6c219fd808905f4a2a424 /lib/Format/FormatToken.cpp
parentbd7dfd35d37f85eb22f6224e2b02c9e59358d691 (diff)
clang-format: Prevent column layout if elements aren't uniform enough.
This patch only considers the difference between the length of the shortest and longest element, but we might want to look at other features (token count, etc.) in future. Before: std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{ aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a, aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a}; After: std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{ aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a, aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a}; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@218111 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/FormatToken.cpp')
-rw-r--r--lib/Format/FormatToken.cpp12
1 files changed, 10 insertions, 2 deletions
diff --git a/lib/Format/FormatToken.cpp b/lib/Format/FormatToken.cpp
index c6f23a6c28..677f1a0bb1 100644
--- a/lib/Format/FormatToken.cpp
+++ b/lib/Format/FormatToken.cpp
@@ -145,6 +145,9 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
// trailing comments which are otherwise ignored for column alignment.
SmallVector<unsigned, 8> EndOfLineItemLength;
+ unsigned MinItemLength = Style.ColumnLimit;
+ unsigned MaxItemLength = 0;
+
for (unsigned i = 0, e = Commas.size() + 1; i != e; ++i) {
// Skip comments on their own line.
while (ItemBegin->HasUnescapedNewline && ItemBegin->isTrailingComment())
@@ -171,6 +174,9 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
ItemEnd = Commas[i];
// The comma is counted as part of the item when calculating the length.
ItemLengths.push_back(CodePointsBetween(ItemBegin, ItemEnd));
+ MinItemLength = std::min(MinItemLength, ItemLengths.back());
+ MaxItemLength = std::max(MaxItemLength, ItemLengths.back());
+
// Consume trailing comments so the are included in EndOfLineItemLength.
if (ItemEnd->Next && !ItemEnd->Next->HasUnescapedNewline &&
ItemEnd->Next->isTrailingComment())
@@ -186,8 +192,10 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
// If this doesn't have a nested list, we require at least 6 elements in order
// create a column layout. If it has a nested list, column layout ensures one
- // list element per line.
- if (HasNestedBracedList || Commas.size() < 5 || Token->NestingLevel != 0)
+ // list element per line. If the difference between the shortest and longest
+ // element is too large, column layout would create too much whitespace.
+ if (HasNestedBracedList || Commas.size() < 5 || Token->NestingLevel != 0 ||
+ MaxItemLength - MinItemLength > 10)
return;
// We can never place more than ColumnLimit / 3 items in a row (because of the