Skip to content

Commit 923a034

Browse files
committed
Rename to PrettyAstPrinter; improve tests; add javadoc
1 parent 05c229d commit 923a034

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

src/main/java/graphql/language/PrettyPrinter.java renamed to src/main/java/graphql/language/PrettyAstPrinter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
* @see AstPrinter
2424
*/
2525
@PublicApi
26-
public class PrettyPrinter extends AstPrinter {
26+
public class PrettyAstPrinter extends AstPrinter {
2727
private final CommentParser commentParser;
2828
private final PrettyPrinterOptions options;
2929

30-
public PrettyPrinter(NodeToRuleCapturingParser.ParserContext parserContext) {
30+
public PrettyAstPrinter(NodeToRuleCapturingParser.ParserContext parserContext) {
3131
this(parserContext, PrettyPrinterOptions.defaultOptions);
3232
}
3333

34-
public PrettyPrinter(NodeToRuleCapturingParser.ParserContext parserContext, PrettyPrinterOptions options) {
34+
public PrettyAstPrinter(NodeToRuleCapturingParser.ParserContext parserContext, PrettyPrinterOptions options) {
3535
super(false);
3636
this.commentParser = new CommentParser(parserContext);
3737
this.options = options;
@@ -68,7 +68,7 @@ public static String print(String schemaDefinition, PrettyPrinterOptions options
6868
NodeToRuleCapturingParser parser = new NodeToRuleCapturingParser();
6969
Document document = parser.parseDocument(schemaDefinition);
7070

71-
return new PrettyPrinter(parser.getParserContext(), options).print(document);
71+
return new PrettyAstPrinter(parser.getParserContext(), options).print(document);
7272
}
7373

7474
private NodePrinter<Document> document() {

src/main/java/graphql/parser/GraphqlAntlrToLanguage.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,37 @@ public class GraphqlAntlrToLanguage {
9494

9595
private final Map<Node<?>, ParserRuleContext> nodeToRuleMap;
9696

97+
/**
98+
* @param tokens the token stream
99+
* @param multiSourceReader the source of the query document
100+
*/
97101
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader) {
98102
this(tokens, multiSourceReader, null);
99103
}
100104

105+
/**
106+
* @param tokens the token stream
107+
* @param multiSourceReader the source of the query document
108+
* @param parserOptions the parser options
109+
*/
101110
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions) {
102111
this(tokens, multiSourceReader, parserOptions, null);
103112
}
104113

105-
public GraphqlAntlrToLanguage(CommonTokenStream tokens, MultiSourceReader multiSourceReader, ParserOptions parserOptions, @Nullable Map<Node<?>, ParserRuleContext> nodeToRuleMap) {
114+
/**
115+
* @param tokens the token stream
116+
* @param multiSourceReader the source of the query document
117+
* @param parserOptions the parser options
118+
* @param nodeToRuleMap a map that will be used to accumulate the ParserRuleContext associated with each node.
119+
* This information can be used after the parsing process is done to access some elements
120+
* that are usually lost during parsing. If the map is "null", no accumulation will be performed.
121+
*/
122+
public GraphqlAntlrToLanguage(
123+
CommonTokenStream tokens,
124+
MultiSourceReader multiSourceReader,
125+
ParserOptions parserOptions,
126+
@Nullable Map<Node<?>, ParserRuleContext> nodeToRuleMap
127+
) {
106128
this.tokens = tokens;
107129
this.multiSourceReader = multiSourceReader;
108130
this.parserOptions = ofNullable(parserOptions).orElse(ParserOptions.getDefaultParserOptions());

src/test/groovy/graphql/language/PrettyPrinterTest.groovy renamed to src/test/groovy/graphql/language/PrettyAstPrinterTest.groovy

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package graphql.language
22

3-
import graphql.parser.NodeToRuleCapturingParser
43
import spock.lang.Specification
54

6-
class PrettyPrinterTest extends Specification {
5+
class PrettyAstPrinterTest extends Specification {
76

87
def "can print type with comments"() {
98
given:
@@ -504,26 +503,19 @@ a: A, b: B): Type
504503
}
505504
'''
506505
when:
507-
def options = PrettyPrinter.PrettyPrinterOptions
506+
def options = PrettyAstPrinter.PrettyPrinterOptions
508507
.builder()
509-
.indentType(PrettyPrinter.PrettyPrinterOptions.IndentType.TAB)
508+
.indentType(PrettyAstPrinter.PrettyPrinterOptions.IndentType.TAB)
510509
.indentWith(1)
511510
.build()
512511

513-
def parser = new NodeToRuleCapturingParser()
514-
def document = parser.parseDocument(input)
515-
516-
def result = new PrettyPrinter(parser.parserContext, options).print(document)
512+
def result = PrettyAstPrinter.print(input, options)
517513

518514
then:
519515
result == expected
520516
}
521517

522518
private static String print(String input) {
523-
def parser = new NodeToRuleCapturingParser()
524-
def document = parser.parseDocument(input)
525-
def parserContext = parser.parserContext
526-
527-
return new PrettyPrinter(parserContext).print(document)
519+
return PrettyAstPrinter.print(input, PrettyAstPrinter.PrettyPrinterOptions.defaultOptions())
528520
}
529521
}

0 commit comments

Comments
 (0)