]> granicus.if.org Git - clang/commitdiff
clang-format: Allow "single column" list layout even if that violates the
authorDaniel Jasper <djasper@google.com>
Mon, 19 Dec 2016 07:26:11 +0000 (07:26 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 19 Dec 2016 07:26:11 +0000 (07:26 +0000)
column limit.

Single-column layout basically means that we format the list with one
element per line. Not doing that when there is a column limit violation
doesn't change the fact that there is an item that doesn't fit within
the column limit.

Before (with a column limit of 30):
  std::vector<int> a = {
      aaaaaaaaaaaaaaaa,
      aaaaaaaaaaaaaaaa,
      aaaaaaaaaaaaaaaaaa,
      aaaaaaaaaaaaaaaaaaaaaaaaaaa};

After:
  std::vector<int> a = {
      aaaaaaaa,
      aaaaaaaa,
      aaaaaaaa,
      aaaaaaaa,
      aaaaaaaaaa,
      aaaaaaaa,
      aaaaaaaaaaaaaaaaaaaaaaaaaaa};

(and previously we would have formatted like "After" it wasn't for the one
item that is too long)

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@290084 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/FormatToken.cpp
unittests/Format/FormatTest.cpp

index 180e537ce06e87744a61b15f6cc2948b35860c1e..e0dfac7d4e109c661d255299ccca5e7a45827bf7 100644 (file)
@@ -273,7 +273,7 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
       continue;
 
     // Ignore layouts that are bound to violate the column limit.
-    if (Format.TotalWidth > Style.ColumnLimit)
+    if (Format.TotalWidth > Style.ColumnLimit && Columns > 1)
       continue;
 
     Formats.push_back(Format);
@@ -287,7 +287,7 @@ CommaSeparatedList::getColumnFormat(unsigned RemainingCharacters) const {
            I = Formats.rbegin(),
            E = Formats.rend();
        I != E; ++I) {
-    if (I->TotalWidth <= RemainingCharacters) {
+    if (I->TotalWidth <= RemainingCharacters || I->Columns == 1) {
       if (BestFormat && I->LineCount > BestFormat->LineCount)
         break;
       BestFormat = &*I;
index adf529cfedacd538bb58d0f30729c4d4f55b6dbf..70abf3af7d7c7d7cc19324b1f3c403cbffb150f8 100644 (file)
@@ -6779,6 +6779,18 @@ TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
+
+  // Allow "single-column" layout even if that violates the column limit. There
+  // isn't going to be a better way.
+  verifyFormat("std::vector<int> a = {\n"
+               "    aaaaaaaa,\n"
+               "    aaaaaaaa,\n"
+               "    aaaaaaaa,\n"
+               "    aaaaaaaa,\n"
+               "    aaaaaaaaaa,\n"
+               "    aaaaaaaa,\n"
+               "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
+               getLLVMStyleWithColumns(30));
 }
 
 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {