Skip to content

Commit 6030b3a

Browse files
committed
Implement pretty printer
1 parent 2195538 commit 6030b3a

File tree

6 files changed

+1364
-56
lines changed

6 files changed

+1364
-56
lines changed

src/main/java/graphql/language/AstPrinter.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class AstPrinter {
2525

2626
private final boolean compactMode;
2727

28-
private AstPrinter(boolean compactMode) {
28+
protected AstPrinter(boolean compactMode) {
2929
this.compactMode = compactMode;
3030
printers.put(Argument.class, argument());
3131
printers.put(ArrayValue.class, value());
@@ -463,11 +463,11 @@ private String node(Node node, Class startClass) {
463463
}
464464

465465
@SuppressWarnings("unchecked")
466-
private <T extends Node> NodePrinter<T> _findPrinter(Node node) {
466+
protected <T extends Node> NodePrinter<T> _findPrinter(Node node) {
467467
return _findPrinter(node, null);
468468
}
469469

470-
private <T extends Node> NodePrinter<T> _findPrinter(Node node, Class startClass) {
470+
protected <T extends Node> NodePrinter<T> _findPrinter(Node node, Class startClass) {
471471
if (node == null) {
472472
return (out, type) -> {
473473
};
@@ -689,7 +689,7 @@ public static void printAst(Writer writer, Node node) {
689689

690690
/**
691691
* This will print the Ast node in graphql language format in a compact manner, with no new lines
692-
* and comments stripped out of the text.
692+
* and descriptions stripped out of the text.
693693
*
694694
* @param node the AST node to print
695695
*
@@ -712,7 +712,16 @@ private static void printImpl(StringBuilder writer, Node node, boolean compactMo
712712
*
713713
* @param <T> the type of node
714714
*/
715-
private interface NodePrinter<T extends Node> {
715+
protected interface NodePrinter<T extends Node> {
716716
void print(StringBuilder out, T node);
717717
}
718+
719+
/**
720+
* Allow subclasses to replace a printer for a specific {@link Node}
721+
* @param nodeClass the class of the {@link Node}
722+
* @param nodePrinter the custom {@link NodePrinter}
723+
*/
724+
protected void replacePrinter(Class<? extends Node> nodeClass, NodePrinter<? extends Node> nodePrinter) {
725+
this.printers.put(nodeClass, nodePrinter);
726+
}
718727
}

0 commit comments

Comments
 (0)