-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement pretty printer #2894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement pretty printer #2894
Conversation
| .indentWith(1) | ||
| .build() | ||
|
|
||
| def parser = new NodeToRuleCapturingParser() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can reflect this in the test then please
bbakerman
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.