Skip to content
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

Refactor SDLPrinter for better indentation #304

Merged
merged 7 commits into from
Apr 20, 2023
Merged

Refactor SDLPrinter for better indentation #304

merged 7 commits into from
Apr 20, 2023

Conversation

sungam3r
Copy link
Member

@sungam3r sungam3r commented Apr 19, 2023

fixes #287
waits #303 to be merged into

The core idea of this PR - manage indentation more centrally. Side note - I tried to get rid of trailing NewLine when printing AST.

@sungam3r sungam3r requested a review from Shane32 April 19, 2023 16:08
@sungam3r sungam3r added bugfix Pull request that fixes a bug BREAKING Breaking changes in either public API or runtime behavior labels Apr 19, 2023
@github-actions github-actions bot added the test Pull request that adds new or changes existing tests label Apr 19, 2023
@@ -852,7 +856,11 @@ namespace GraphQLParser.Visitors
public interface IPrintContext : GraphQLParser.Visitors.IASTVisitorContext
{
int IndentLevel { get; set; }
bool IndentPrinted { get; set; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shane32 Formally, this is breaking changes, but I doubt that there are a sufficient number of people for whom it will be a real problem

Comment on lines +879 to +886
(
node is GraphQLInputValueDefinition ||
node is GraphQLFieldDefinition ||
node is GraphQLEnumValueDefinition ||
node is GraphQLRootOperationTypeDefinition ||
node is GraphQLDirectiveLocations && Options.EachDirectiveLocationOnNewLine ||
node is GraphQLUnionMemberTypes && Options.EachUnionMemberOnNewLine
) && context.Parents.Count > 0

Check notice

Code scanning / CodeQL

Complex condition

Complex condition: too many logical operations in this expression.
public bool EachDirectiveLocationOnNewLine { get; init; }
public bool EachUnionMemberOnNewLine { get; init; }
public int IndentSize { get; init; }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To align with other modifiers. Changes from #294 were not published yet.

@@ -697,15 +764,13 @@ public async Task SelectionSet_Without_Parent_Should_Be_Printed_On_New_Line()
var printer = new SDLPrinter(new SDLPrinterOptions { PrintComments = true });
await printer.PrintAsync(selectionSet, writer);
writer.ToString().ShouldBe(@"{
}
");
}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better handling for NewLine at the end of printed AST.

@@ -906,6 +918,7 @@ namespace GraphQLParser.Visitors
public SDLPrinter() { }
public SDLPrinter(GraphQLParser.Visitors.SDLPrinterOptions options) { }
public GraphQLParser.Visitors.SDLPrinterOptions Options { get; }
protected virtual System.Threading.Tasks.ValueTask MakeVerticalIndentationBetweenTopLevelDefinitions(GraphQLParser.AST.ASTNode node, TContext context) { }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New method is needed for descendants that may skip nodes, see PrintOnlyStartsWithA. Without it SDLPrinter prints large vertical indentation in place of skipped nodes.

@@ -575,6 +575,73 @@ type Query
subscription: S
}
""", true, false, false, 5)]
[InlineData(45,
"""
"A component contains the parametric details of a PCB part."
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nightroman your test now works

if (value.Length == 0)
return;

context.NewLinePrinted = value.Span[value.Length - 1] == '\n';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That approach is much easier than trying to calculate the flag in each place where WriteAsync is called.

if (CommentedNodeShouldBeCloseToPreviousNode(context, comment, false))
await WriteIndentAsync(context).ConfigureAwait(false);

static bool CommentedNodeShouldBeCloseToPreviousNode(TContext context, GraphQLComment comment, bool start)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was veeery awkward condition.

@sungam3r
Copy link
Member Author

I'm sure there are more issues to fix but for now I finished. I simplified working with indents, especially for comments and descriptions and added more tests to verify edge cases.

# Conflicts:
#	src/GraphQLParser.ApiTests/GraphQLParser.approved.txt
#	src/GraphQLParser/Visitors/StructurePrinter.cs
@codecov-commenter
Copy link

Codecov Report

Merging #304 (bca08ee) into master (6587b6f) will decrease coverage by 0.05%.
The diff coverage is 99.23%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master     #304      +/-   ##
==========================================
- Coverage   99.82%   99.77%   -0.05%     
==========================================
  Files          85       85              
  Lines        4556     4467      -89     
  Branches      462      448      -14     
==========================================
- Hits         4548     4457      -91     
- Misses          6        8       +2     
  Partials        2        2              
Impacted Files Coverage Δ
src/GraphQLParser/Visitors/SDLPrinter.cs 99.73% <99.15%> (-0.27%) ⬇️
...rser/AST/Definitions/GraphQLEnumValueDefinition.cs 100.00% <100.00%> (ø)
...QLParser/AST/Definitions/GraphQLFieldDefinition.cs 100.00% <100.00%> (ø)
...ser/AST/Definitions/GraphQLInputValueDefinition.cs 100.00% <100.00%> (ø)
...hQLParser/AST/Definitions/GraphQLTypeDefinition.cs 100.00% <100.00%> (ø)
...c/GraphQLParser/Visitors/PrintContextExtensions.cs 100.00% <100.00%> (ø)
src/GraphQLParser/Visitors/StructurePrinter.cs 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@sungam3r
Copy link
Member Author

So what about version? 8.3 or 9.0? I lean to 8.3 since printer is auxiliary code and not too many people should use custom contexts for printing.

@sungam3r
Copy link
Member Author

More argument against 9.0 - it will require to bump version of core report and server.

@Shane32
Copy link
Member

Shane32 commented Apr 19, 2023

Your call. If it’s source compatible then I’m okay with 8.3.

The core repo and server specify 8.2+ probably and don’t need to bump to 9.0 if they don’t rely on any breaking API.

@Shane32
Copy link
Member

Shane32 commented Apr 19, 2023

Whatever you think I guess. It really only matters to you if the features are not used by GraphQL. I’d just publish as 9.0 and leave GraphQL 7.x using parser 8.x until we bump GraphQL to 8.0. Again your call.

@sungam3r
Copy link
Member Author

It is not source compatible in case of custom printer context implemented in user code. I added new members in public interface.

@Shane32
Copy link
Member

Shane32 commented Apr 19, 2023

I’m okay with default implementation of new interface members. That reduces breaking changes to .NET Framework users. (Or older obsolete Core users)

@sungam3r
Copy link
Member Author

I have never used that feature before, will look tomorrow.

@sungam3r
Copy link
Member Author

Instance state is not allowed for default interfaces so I'll bump to 9.

@sungam3r sungam3r merged commit c3852ad into master Apr 20, 2023
@sungam3r sungam3r deleted the printer branch April 20, 2023 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING Breaking changes in either public API or runtime behavior bugfix Pull request that fixes a bug test Pull request that adds new or changes existing tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strange formatting of input types with descriptions
3 participants