Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Unified Diff: tools/gn/command_format.cc

Issue 606123002: gn format: make sure there's a blank line before comments, except at block start (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@gn-format-crash-no-block
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/gn/command_format_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gn/command_format.cc
diff --git a/tools/gn/command_format.cc b/tools/gn/command_format.cc
index d96e6a330fa39b1b2fc235d3d11ca990badf7ad9..96170b298e0c4b4639877b3a19f75b4d8165bb55 100644
--- a/tools/gn/command_format.cc
+++ b/tools/gn/command_format.cc
@@ -72,6 +72,9 @@ class Printer {
// Remove trailing spaces from the current line.
void Trim();
+ // Whether there's a blank separator line at the current position.
+ bool HaveBlankLine();
+
// Get the 0-based x position on the current line.
int CurrentColumn();
@@ -141,6 +144,13 @@ void Printer::Trim() {
output_.resize(n);
}
+bool Printer::HaveBlankLine() {
+ size_t n = output_.size();
+ while (n > 0 && output_[n - 1] == ' ')
+ --n;
+ return n > 2 && output_[n - 1] == '\n' && output_[n - 2] == '\n';
+}
+
int Printer::CurrentColumn() {
int n = 0;
while (n < static_cast<int>(output_.size()) &&
@@ -315,6 +325,15 @@ void Printer::Sequence(SequenceStyle style,
size_t i = 0;
for (const auto& x : list) {
Newline();
+ // If:
+ // - we're going to output some comments, and;
+ // - we haven't just started this multiline list, and;
+ // - there isn't already a blank line here;
+ // Then: insert one.
+ if (i != 0 && x->comments() && !x->comments()->before().empty() &&
+ !HaveBlankLine()) {
+ Newline();
+ }
ExprStyle expr_style = Expr(x);
CHECK(!x->comments() || x->comments()->after().empty());
if (i < list.size() - 1 || style == kSequenceStyleList) {
« no previous file with comments | « no previous file | tools/gn/command_format_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698