Commit 0abd867e authored by Scott Graham's avatar Scott Graham

gn format: make sure there's a blank line before comments, except at block start

With this, gn format //BUILD.gn results in no changes, other than to
make 'deps += [ "a" ]' turn into one line instead of multiline.

(Child of https://codereview.chromium.org/608593006/)

R=brettw@chromium.org
BUG=348474

Review URL: https://codereview.chromium.org/606123002

Cr-Commit-Position: refs/heads/master@{#297231}
parent bc82d3c4
......@@ -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) {
......
......@@ -47,3 +47,4 @@ FORMAT_TEST(015)
// TODO(scottmg): Requires precedence/parentheses FORMAT_TEST(016)
FORMAT_TEST(017)
FORMAT_TEST(018)
FORMAT_TEST(019)
# Make sure blank lines are maintained before comments in lists.
deps = [
"//pdf", # Not compiled on Android in GYP yet, either.
"//ppapi:ppapi_c",
"//third_party/libusb",
"//ui/keyboard", # Blocked on content.
# Seems to not be compiled on Android. Otherwise it will need a config.h.
"//third_party/libxslt",
# Not relevant to Android.
"//ash",
"//tools/gn",
# Multiple line
# comment
# here.
"//ui/aura",
"//ui/display",
"//ui/views",
"//ui/views/controls/webview",
]
# Make sure blank lines are maintained before comments in lists.
deps = [
"//pdf", # Not compiled on Android in GYP yet, either.
"//ppapi:ppapi_c",
"//third_party/libusb",
"//ui/keyboard", # Blocked on content.
# Seems to not be compiled on Android. Otherwise it will need a config.h.
"//third_party/libxslt",
# Not relevant to Android.
"//ash",
"//tools/gn",
# Multiple line
# comment
# here.
"//ui/aura",
"//ui/display",
"//ui/views",
"//ui/views/controls/webview",
]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment