Skip to content

Commit

Permalink
Fix indentation when printing arguments (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r authored Apr 21, 2023
1 parent f1cb807 commit bb6a168
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<VersionPrefix>9.0.1-preview</VersionPrefix>
<VersionPrefix>9.0.2-preview</VersionPrefix>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);CA1707</NoWarn>
Expand Down
144 changes: 140 additions & 4 deletions src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ type Dog implements Animal
#arguments comment
#multilined
(x: 10, y: {
#comment on object field
z: 1})
#comment on object field
z: 1})
}")]
[InlineData(33,
@"{
Expand Down Expand Up @@ -519,8 +519,8 @@ extend enum Color
}",
@"type T {
data(
#comment
rendered: Boolean): String
#comment
rendered: Boolean): String
}", true)]
[InlineData(40, """
"This is a Foo object type"
Expand Down Expand Up @@ -654,6 +654,142 @@ query q(
schema {
query: Query
}
""")]
[InlineData(50,
"""
type Query {
"Fetches an object given its ID."
node(
"ID of the object."
id: ID!): Node
"Lookup nodes by a list of IDs."
nodes(
"The list of node IDs."
ids: [ID!]!): [Node]!
"Search for workspaces associated with this account."
desWorkspaces(where: DesWorkspaceFilterInput): [DesWorkspace!]!
"Search a specific workspace by its unique identifier."
desWorkspaceById(
"The node identifier for a workspace." id: ID!): DesWorkspace
}
""",
"""
type Query {
"Fetches an object given its ID."
node(
"ID of the object."
id: ID!): Node
"Lookup nodes by a list of IDs."
nodes(
"The list of node IDs."
ids: [ID!]!): [Node]!
"Search for workspaces associated with this account."
desWorkspaces(where: DesWorkspaceFilterInput): [DesWorkspace!]!
"Search a specific workspace by its unique identifier."
desWorkspaceById(
"The node identifier for a workspace."
id: ID!): DesWorkspace
}
""")]
[InlineData(51,
"""
type Query {
user
# comment 1
(
# comment 2
id: ID!
name: Name!): Node
}
""",
"""
type Query {
user
# comment 1
(
# comment 2
id: ID!, name: Name!): Node
}
""")]
[InlineData(52,
"""
directive @my
# comment 1
(
# comment 2
arg: Boolean!) on FIELD
""",
"""
directive @my
# comment 1
(
# comment 2
arg: Boolean!) on FIELD
""")]
[InlineData(53,
"""
query Q {
field1(arg1: 1) {
field2(arg2: 2) {
field3(arg3: 3)
}
}
}
""",
"""
query Q {
field1(arg1: 1) {
field2(arg2: 2) {
field3(arg3: 3)
}
}
}
""")]
[InlineData(54,
"""
query Q {
field1
#comment
(
#comment
arg1: 1
) {
field2
#comment
(
#comment
arg2: 2
) {
field3
#comment
(
#comment
arg3: 3
)
}
}
}
""",
"""
query Q {
field1
#comment
(
#comment
arg1: 1) {
field2
#comment
(
#comment
arg2: 2) {
field3
#comment
(
#comment
arg3: 3)
}
}
}
""")]
public async Task SDLPrinter_Should_Print_Document(
int number,
Expand Down
39 changes: 21 additions & 18 deletions src/GraphQLParser/Visitors/SDLPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ protected override async ValueTask VisitArgumentAsync(GraphQLArgument argument,
protected override async ValueTask VisitArgumentsDefinitionAsync(GraphQLArgumentsDefinition argumentsDefinition, TContext context)
{
await VisitAsync(argumentsDefinition.Comments, context).ConfigureAwait(false);
await context.WriteAsync("(").ConfigureAwait(false);
await VisitAsync(LiteralNode.Wrap("("), context).ConfigureAwait(false);

foreach (var argumentDefinition in argumentsDefinition.Items)
await VisitAsync(argumentDefinition, context).ConfigureAwait(false);
Expand Down Expand Up @@ -870,23 +870,26 @@ public override async ValueTask VisitAsync(ASTNode? node, TContext context)

int prevLevel = context.IndentLevel;

if (
(
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
)
context.IndentLevel = 1; // fixed indentation of 1
else if (
node is GraphQLField ||
node is GraphQLFragmentSpread ||
node is GraphQLInlineFragment
)
++context.IndentLevel; // nested indentation
if (context.Parents.Count > 0)
{
if (
node is GraphQLFieldDefinition ||
node is GraphQLEnumValueDefinition ||
node is GraphQLRootOperationTypeDefinition ||
node is GraphQLDirectiveLocations && Options.EachDirectiveLocationOnNewLine ||
node is GraphQLUnionMemberTypes && Options.EachUnionMemberOnNewLine ||
node is GraphQLArgumentsDefinition && node.Comments?.Count > 0
)
context.IndentLevel = 1; // fixed indentation of 1
else if (
node is GraphQLField ||
node is GraphQLFragmentSpread ||
node is GraphQLInlineFragment ||
node is GraphQLArgument ||
node is GraphQLInputValueDefinition
)
++context.IndentLevel; // nested indentation
}

context.Parents.Push(node);

Expand Down

0 comments on commit bb6a168

Please sign in to comment.