Implement pretty printer#2894
Conversation
| .indentWith(1) | ||
| .build() | ||
|
|
||
| def parser = new NodeToRuleCapturingParser() |
There was a problem hiding this comment.
I think it would be good that the pretty printer would be self contained and not requre the user to parse anything themself.
There was a problem hiding this comment.
It actually is. I created this static print method that callers can use just passing the schemaDef and options:
public static String print(String schemaDefinition, PrettyPrinterOptions options) {
NodeToRuleCapturingParser parser = new NodeToRuleCapturingParser();
Document document = parser.parseDocument(schemaDefinition);
return new PrettyPrinter(parser.getParserContext(), options).print(document);
}
I had created the tests before creating the method, and never got around to change the call in the tests :-)
There was a problem hiding this comment.
can reflect this in the test then please
bbakerman
left a comment
There was a problem hiding this comment.
Consider the naming of pretty printer as mentioned inside
| return implementz; | ||
| } | ||
|
|
||
| private <T extends Node<?>> T captureRuleContext(T node, ParserRuleContext ctx) { |
There was a problem hiding this comment.
Maybe a comment somewhere that says if the map is null it means its not used. For future devs.
| * @see AstPrinter | ||
| */ | ||
| @PublicApi | ||
| public class PrettyPrinter extends AstPrinter { |
There was a problem hiding this comment.
What about PrettyAstPrinter as a name
We have today an AstPrinter and a SchemaPrinter - so this printer is a PrettyAstPrinter or AstPrettyPrinter
There was a problem hiding this comment.
100%!
Just pushed a commit with the change.
| * @param nodeClass the class of the {@link Node} | ||
| * @param nodePrinter the custom {@link NodePrinter} | ||
| */ | ||
| protected void replacePrinter(Class<? extends Node> nodeClass, NodePrinter<? extends Node> nodePrinter) { |
There was a problem hiding this comment.
can mark this either either as Internal or make it package protected instead of protected
| * @param <T> the type of node | ||
| */ | ||
| private interface NodePrinter<T extends Node> { | ||
| protected interface NodePrinter<T extends Node> { |
There was a problem hiding this comment.
can mark this either either as Internal or make it package protected instead of protected
Create a PrettyAstPrinter class to enable printing schemas for linting purposes.
This printer will maintain comments and expose options for controlling the formatting of the printed schema.