aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Klaus Aehlig <aehlig@google.com>2018-06-14 08:17:33 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-06-14 08:18:48 -0700
commit3695591d4b40497380640d7886c55a00d4d3fd70 (patch)
treed546e70bc02e582a1508e5093a153c8c26da03c2
parent09222fac7c8851d26e44e087a6beb4ce2ed5dc39 (diff)
Skylark Pretty Printer: shorten empty list
When pretty printing a Skylark value, lists are presented as the opening bracket on a line by itself, each entry on its own line, and the closing bracket again on its own line. While this generally improves readability, for the empty list this is not the case, as the expression [] can easily be understood at a glance. In fact, the additional line even makes the outer structure harder to see, as it is spread over even more lines. Therefore, shorten the printing of the empty list to be on a single line. Change-Id: I032d1550b1f99bce47dbec7e77a4d5c6656d78a1 PiperOrigin-RevId: 200558784
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/Printer.java7
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/PrinterTest.java15
2 files changed, 22 insertions, 0 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/Printer.java b/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
index 27ded6721d..a45d8bfec1 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/Printer.java
@@ -672,6 +672,13 @@ public class Printer {
String after,
@Nullable String singletonTerminator) {
+ // If the list is empty, do not split the presentation over
+ // several lines.
+ if (!list.iterator().hasNext()) {
+ this.append(before + after);
+ return this;
+ }
+
String separator = untrimmedSeparator.trim();
this.append(before + "\n");
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/PrinterTest.java b/src/test/java/com/google/devtools/build/lib/syntax/PrinterTest.java
index 37ff47a0bf..d6c165931b 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/PrinterTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/PrinterTest.java
@@ -148,6 +148,10 @@ public class PrinterTest {
" 2,\n" +
" 3\n" +
"]");
+ assertThat(Printer.getPrettyPrinter().repr(ImmutableList.<String>of()).toString())
+ .isEqualTo("[]");
+ assertThat(Printer.getPrettyPrinter().repr(ImmutableList.of("foo")).toString())
+ .isEqualTo("[\n \"foo\"\n]");
assertThat(
Printer.getPrettyPrinter()
.repr(ImmutableMap.<Object, Object>of("foo", "bar", "baz", ImmutableList.of(1, 2)))
@@ -160,6 +164,17 @@ public class PrinterTest {
" 2\n" +
" ]\n" +
"}");
+ assertThat(
+ Printer.getPrettyPrinter()
+ .repr(ImmutableMap.<Object, Object>of(
+ "foo", "bar", "empty", ImmutableList.of(), "a", "b"))
+ .toString())
+ .isEqualTo(
+ "{\n" +
+ " \"foo\": \"bar\",\n" +
+ " \"empty\": [],\n" +
+ " \"a\": \"b\"\n" +
+ "}");
}
private SkylarkPrinter makeSimplifiedFormatPrinter() {