Skip to content

Commit bb6a168

Browse files
authored
Fix indentation when printing arguments (#312)
1 parent f1cb807 commit bb6a168

File tree

3 files changed

+162
-23
lines changed

3 files changed

+162
-23
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22

33
<PropertyGroup>
4-
<VersionPrefix>9.0.1-preview</VersionPrefix>
4+
<VersionPrefix>9.0.2-preview</VersionPrefix>
55
<LangVersion>latest</LangVersion>
66
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
77
<NoWarn>$(NoWarn);CA1707</NoWarn>

src/GraphQLParser.Tests/Visitors/SDLPrinterFromParsedTextTests.cs

Lines changed: 140 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ type Dog implements Animal
409409
#arguments comment
410410
#multilined
411411
(x: 10, y: {
412-
#comment on object field
413-
z: 1})
412+
#comment on object field
413+
z: 1})
414414
}")]
415415
[InlineData(33,
416416
@"{
@@ -519,8 +519,8 @@ extend enum Color
519519
}",
520520
@"type T {
521521
data(
522-
#comment
523-
rendered: Boolean): String
522+
#comment
523+
rendered: Boolean): String
524524
}", true)]
525525
[InlineData(40, """
526526
"This is a Foo object type"
@@ -654,6 +654,142 @@ query q(
654654
schema {
655655
query: Query
656656
}
657+
""")]
658+
[InlineData(50,
659+
"""
660+
type Query {
661+
"Fetches an object given its ID."
662+
node(
663+
"ID of the object."
664+
id: ID!): Node
665+
"Lookup nodes by a list of IDs."
666+
nodes(
667+
"The list of node IDs."
668+
ids: [ID!]!): [Node]!
669+
"Search for workspaces associated with this account."
670+
desWorkspaces(where: DesWorkspaceFilterInput): [DesWorkspace!]!
671+
"Search a specific workspace by its unique identifier."
672+
desWorkspaceById(
673+
"The node identifier for a workspace." id: ID!): DesWorkspace
674+
}
675+
""",
676+
"""
677+
type Query {
678+
"Fetches an object given its ID."
679+
node(
680+
"ID of the object."
681+
id: ID!): Node
682+
"Lookup nodes by a list of IDs."
683+
nodes(
684+
"The list of node IDs."
685+
ids: [ID!]!): [Node]!
686+
"Search for workspaces associated with this account."
687+
desWorkspaces(where: DesWorkspaceFilterInput): [DesWorkspace!]!
688+
"Search a specific workspace by its unique identifier."
689+
desWorkspaceById(
690+
"The node identifier for a workspace."
691+
id: ID!): DesWorkspace
692+
}
693+
""")]
694+
[InlineData(51,
695+
"""
696+
type Query {
697+
user
698+
# comment 1
699+
(
700+
# comment 2
701+
id: ID!
702+
name: Name!): Node
703+
}
704+
""",
705+
"""
706+
type Query {
707+
user
708+
# comment 1
709+
(
710+
# comment 2
711+
id: ID!, name: Name!): Node
712+
}
713+
""")]
714+
[InlineData(52,
715+
"""
716+
directive @my
717+
# comment 1
718+
(
719+
# comment 2
720+
arg: Boolean!) on FIELD
721+
""",
722+
"""
723+
directive @my
724+
# comment 1
725+
(
726+
# comment 2
727+
arg: Boolean!) on FIELD
728+
""")]
729+
[InlineData(53,
730+
"""
731+
query Q {
732+
field1(arg1: 1) {
733+
field2(arg2: 2) {
734+
field3(arg3: 3)
735+
}
736+
}
737+
}
738+
""",
739+
"""
740+
query Q {
741+
field1(arg1: 1) {
742+
field2(arg2: 2) {
743+
field3(arg3: 3)
744+
}
745+
}
746+
}
747+
""")]
748+
[InlineData(54,
749+
"""
750+
query Q {
751+
field1
752+
#comment
753+
(
754+
#comment
755+
arg1: 1
756+
) {
757+
field2
758+
#comment
759+
(
760+
#comment
761+
arg2: 2
762+
) {
763+
field3
764+
#comment
765+
(
766+
#comment
767+
arg3: 3
768+
)
769+
}
770+
}
771+
}
772+
""",
773+
"""
774+
query Q {
775+
field1
776+
#comment
777+
(
778+
#comment
779+
arg1: 1) {
780+
field2
781+
#comment
782+
(
783+
#comment
784+
arg2: 2) {
785+
field3
786+
#comment
787+
(
788+
#comment
789+
arg3: 3)
790+
}
791+
}
792+
}
657793
""")]
658794
public async Task SDLPrinter_Should_Print_Document(
659795
int number,

src/GraphQLParser/Visitors/SDLPrinter.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ protected override async ValueTask VisitArgumentAsync(GraphQLArgument argument,
721721
protected override async ValueTask VisitArgumentsDefinitionAsync(GraphQLArgumentsDefinition argumentsDefinition, TContext context)
722722
{
723723
await VisitAsync(argumentsDefinition.Comments, context).ConfigureAwait(false);
724-
await context.WriteAsync("(").ConfigureAwait(false);
724+
await VisitAsync(LiteralNode.Wrap("("), context).ConfigureAwait(false);
725725

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

871871
int prevLevel = context.IndentLevel;
872872

873-
if (
874-
(
875-
node is GraphQLInputValueDefinition ||
876-
node is GraphQLFieldDefinition ||
877-
node is GraphQLEnumValueDefinition ||
878-
node is GraphQLRootOperationTypeDefinition ||
879-
node is GraphQLDirectiveLocations && Options.EachDirectiveLocationOnNewLine ||
880-
node is GraphQLUnionMemberTypes && Options.EachUnionMemberOnNewLine
881-
) && context.Parents.Count > 0
882-
)
883-
context.IndentLevel = 1; // fixed indentation of 1
884-
else if (
885-
node is GraphQLField ||
886-
node is GraphQLFragmentSpread ||
887-
node is GraphQLInlineFragment
888-
)
889-
++context.IndentLevel; // nested indentation
873+
if (context.Parents.Count > 0)
874+
{
875+
if (
876+
node is GraphQLFieldDefinition ||
877+
node is GraphQLEnumValueDefinition ||
878+
node is GraphQLRootOperationTypeDefinition ||
879+
node is GraphQLDirectiveLocations && Options.EachDirectiveLocationOnNewLine ||
880+
node is GraphQLUnionMemberTypes && Options.EachUnionMemberOnNewLine ||
881+
node is GraphQLArgumentsDefinition && node.Comments?.Count > 0
882+
)
883+
context.IndentLevel = 1; // fixed indentation of 1
884+
else if (
885+
node is GraphQLField ||
886+
node is GraphQLFragmentSpread ||
887+
node is GraphQLInlineFragment ||
888+
node is GraphQLArgument ||
889+
node is GraphQLInputValueDefinition
890+
)
891+
++context.IndentLevel; // nested indentation
892+
}
890893

891894
context.Parents.Push(node);
892895

0 commit comments

Comments
 (0)