format: Fix single line list suffix comments being dropped

Sequence() was only adding comments in multiline list cases, not in the
single element/line case.

Bug: gn:141
Change-Id: I913558706bf0913c432bea258f263400ee1d59fc
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/7140
Commit-Queue: Scott Graham <scottmg@chromium.org>
Reviewed-by: Nico Weber <thakis@chromium.org>
diff --git a/src/gn/command_format.cc b/src/gn/command_format.cc
index 3cc4218..623d8e2 100644
--- a/src/gn/command_format.cc
+++ b/src/gn/command_format.cc
@@ -137,6 +137,8 @@
 
   void PrintTrailingCommentsWrapped(const std::vector<Token>& comments);
 
+  void FlushComments();
+
   void PrintSuffixComments(const ParseNode* node);
 
   // End the current line, flushing end of line comments.
@@ -341,7 +343,7 @@
   }
 }
 
-void Printer::Newline() {
+void Printer::FlushComments() {
   if (!comments_.empty()) {
     Print("  ");
     // Save the margin, and temporarily set it to where the first comment
@@ -351,6 +353,10 @@
     stack_.pop_back();
     comments_.clear();
   }
+}
+
+void Printer::Newline() {
+  FlushComments();
   Trim();
   Print("\n");
   PrintMargin();
@@ -698,7 +704,7 @@
         sub1.Expr(binop->right(), prec_right, std::string());
     sub1.Print(suffix);
     sub1.PrintSuffixComments(root);
-    sub1.PrintSuffixComments(binop->right());
+    sub1.FlushComments();
     penalty_current_line += AssessPenalty(sub1.String());
     if (!is_assignment && left_is_multiline) {
       // In e.g. xxx + yyy, if xxx is already multiline, then we want a penalty
@@ -715,7 +721,7 @@
         sub2.Expr(binop->right(), prec_right, std::string());
     sub2.Print(suffix);
     sub2.PrintSuffixComments(root);
-    sub2.PrintSuffixComments(binop->right());
+    sub2.FlushComments();
     penalty_next_line += AssessPenalty(sub2.String());
 
     // Force a list on the RHS that would normally be a single line into
@@ -732,7 +738,7 @@
       sub3.Sequence(kSequenceStyleList, rhs_list->contents(), rhs_list->End(),
                     true);
       sub3.PrintSuffixComments(root);
-      sub3.PrintSuffixComments(binop->right());
+      sub3.FlushComments();
       sub3.stack_.pop_back();
       penalty_multiline_rhs_list = AssessPenalty(sub3.String());
       tried_rhs_multiline = true;
@@ -892,12 +898,12 @@
 
     stack_.pop_back();
     Newline();
+  }
 
-    // Defer any end of line comment until we reach the newline.
-    if (end->comments() && !end->comments()->suffix().empty()) {
-      std::copy(end->comments()->suffix().begin(),
-                end->comments()->suffix().end(), std::back_inserter(comments_));
-    }
+  // Defer any end of line comment until we reach the newline.
+  if (end->comments() && !end->comments()->suffix().empty()) {
+    std::copy(end->comments()->suffix().begin(),
+              end->comments()->suffix().end(), std::back_inserter(comments_));
   }
 
   if (style == kSequenceStyleList)
diff --git a/src/gn/command_format_unittest.cc b/src/gn/command_format_unittest.cc
index 503e6cf..4dcf3bb 100644
--- a/src/gn/command_format_unittest.cc
+++ b/src/gn/command_format_unittest.cc
@@ -124,3 +124,4 @@
 FORMAT_TEST(077)
 FORMAT_TEST(078)
 FORMAT_TEST(079)
+FORMAT_TEST(080)
diff --git a/src/gn/format_test_data/080.gn b/src/gn/format_test_data/080.gn
new file mode 100644
index 0000000..3e3fdba
--- /dev/null
+++ b/src/gn/format_test_data/080.gn
@@ -0,0 +1,33 @@
+# https://crbug.com/gn/141. 80 col ---------------------------------------------
+
+a =
+    [ "b" ]  # comment1
+
+a =
+    [ "b", "c" ]  # comment1b
+
+a =
+    b  # comment2
+
+a =
+    { b = 3 }  # comment3
+
+a =
+    { # comment4
+
+      b = 3 }  # comment5
+
+a =
+    { b = 3 # comment6
+    }
+
+if (true) {
+  if (true) {
+    if (true) {
+      something_longer_on_the_lhs =
+          [ "something that will exceed 80 col if dewrapped" ]  # comment7
+
+      something_longer_on_the_lhs = [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ]  # comment8
+    }
+  }
+}
diff --git a/src/gn/format_test_data/080.golden b/src/gn/format_test_data/080.golden
new file mode 100644
index 0000000..9f18a5e
--- /dev/null
+++ b/src/gn/format_test_data/080.golden
@@ -0,0 +1,34 @@
+# https://crbug.com/gn/141. 80 col ---------------------------------------------
+
+a = [ "b" ]  # comment1
+
+a = [
+  "b",
+  "c",
+]  # comment1b
+
+a = b  # comment2
+
+a = {
+  b = 3
+}  # comment3
+
+a = {  # comment4
+  b = 3
+}  # comment5
+
+a = {
+  b = 3  # comment6
+}
+
+if (true) {
+  if (true) {
+    if (true) {
+      something_longer_on_the_lhs =
+          [ "something that will exceed 80 col if dewrapped" ]  # comment7
+
+      something_longer_on_the_lhs =
+          [ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ]  # comment8
+    }
+  }
+}