aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java
diff options
context:
space:
mode:
authorGravatar Klaus Aehlig <aehlig@google.com>2018-05-29 06:57:03 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-29 06:58:23 -0700
commit26c86f88be0ddd9d59f4fd3f3afdd769af4ecefc (patch)
tree557b8d614ba1daaaeda02a68f6d8331d815e6800 /src/main/java
parent77b159728eb15d186c12ada2c33c46235e1e28e9 (diff)
Add a pretty-printer class to Skylark
With the recording of the results of repository rules (that eventually will lead to an implementation of the WORKSPACE.resolved proposal) bazel started writing out lengthy Skylark values. To make this file more readable for humans, add a Skylkark printer that does at least some basic line breaking and indenting. Change-Id: I469d029421df9212b43747509dd17bd6c64da4a8 PiperOrigin-RevId: 198389112
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/google/devtools/build/lib/syntax/Printer.java54
1 files changed, 54 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 dfbc651a11..27ded6721d 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
@@ -13,6 +13,7 @@
// limitations under the License.
package com.google.devtools.build.lib.syntax;
+import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.events.Location;
@@ -71,6 +72,15 @@ public class Printer {
}
/**
+ * Creates an instance of {@link PrettyPrinter} with an empty buffer.
+ *
+ * @return new {@link PrettyPrinter}
+ */
+ public static PrettyPrinter getPrettyPrinter() {
+ return new PrettyPrinter(new StringBuilder());
+ }
+
+ /**
* Creates an instance of {@link BasePrinter} with an empty buffer and whose format strings allow
* only %s and %%.
*/
@@ -643,6 +653,50 @@ public class Printer {
}
}
+
+ /** A printer that breaks lines beteen the entries of lists, with proper indenting. */
+ public static class PrettyPrinter extends BasePrinter {
+ static final int BASE_INDENT = 4;
+ private int indent;
+
+ protected PrettyPrinter(Appendable buffer) {
+ super(buffer);
+ indent = 0;
+ }
+
+ @Override
+ public BasePrinter printList(
+ Iterable<?> list,
+ String before,
+ String untrimmedSeparator,
+ String after,
+ @Nullable String singletonTerminator) {
+
+ String separator = untrimmedSeparator.trim();
+
+ this.append(before + "\n");
+ indent += BASE_INDENT;
+ boolean printSeparator = false; // don't print the separator before the first element
+ int len = 0;
+ for (Object o : list) {
+ if (printSeparator) {
+ this.append(separator + "\n");
+ }
+ this.append(Strings.repeat(" ", indent));
+ this.repr(o);
+ printSeparator = true;
+ len++;
+ }
+ if (singletonTerminator != null && len == 1) {
+ this.append(singletonTerminator);
+ }
+ this.append("\n");
+ indent -= BASE_INDENT;
+ this.append(Strings.repeat(" ", indent) + after);
+ return this;
+ }
+ }
+
/** A version of {@code BasePrinter} that is able to print abbreviated lists. */
public static final class LengthLimitedPrinter extends BasePrinter {