Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4023,6 +4023,7 @@ void Goo()
}

[Theory, CombinatorialData]
[WorkItem("https://github.com/dotnet/roslyn/issues/68534")]
public async Task TestJson1(TestHost testHost)
{
await TestAsync(
Expand All @@ -4043,10 +4044,12 @@ void Goo()
Json.PropertyName("'goo'"),
Json.Punctuation(":"),
Json.Number("0"),
Json.Punctuation(","),
Json.PropertyName("bar"),
Json.Punctuation(":"),
Json.Operator("-"),
Json.Keyword("Infinity"),
Json.Punctuation(","),
Json.PropertyName(@"""""baz"""""),
Json.Punctuation(":"),
Json.Keyword("true"),
Expand Down Expand Up @@ -4090,6 +4093,7 @@ void Goo()
}

[Theory, CombinatorialData]
[WorkItem("https://github.com/dotnet/roslyn/issues/68534")]
public async Task TestMultiLineJson1(TestHost testHost)
{
await TestAsync(
Expand Down Expand Up @@ -4120,13 +4124,16 @@ void Goo()
Json.PropertyName("'goo'"),
Json.Punctuation(":"),
Json.Number("0"),
Json.Punctuation(","),
Json.PropertyName("bar"),
Json.Punctuation(":"),
Json.Operator("-"),
Json.Keyword("Infinity"),
Json.Punctuation(","),
Json.PropertyName(@"""""baz"""""),
Json.Punctuation(":"),
Json.Keyword("true"),
Json.Punctuation(","),
Json.PropertyName("0"),
Json.Punctuation(":"),
Json.Keyword("null"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,26 @@ private static void AddClassifications(JsonNode node, Visitor visitor, EmbeddedL
}
else
{
AddTriviaClassifications(child.Token, context);
AddTokenClassifications(child.Token, context);
}
}
}

private static void AddTriviaClassifications(JsonToken token, EmbeddedLanguageClassificationContext context)
private static void AddTokenClassifications(JsonToken token, EmbeddedLanguageClassificationContext context)
{
foreach (var trivia in token.LeadingTrivia)
AddTriviaClassifications(trivia, context);

if (!token.IsMissing)
{
switch (token.Kind)
{
case JsonKind.CommaToken:
context.AddClassification(ClassificationTypeNames.JsonPunctuation, token.GetSpan());
break;
}
}

foreach (var trivia in token.TrailingTrivia)
AddTriviaClassifications(trivia, context);
}
Expand Down Expand Up @@ -187,7 +197,7 @@ public void Visit(JsonTextNode node)

public void Visit(JsonCommaValueNode node)
{
AddClassification(node.CommaToken, ClassificationTypeNames.JsonPunctuation);
// already handled when we recurse in AddTokenClassifications
Copy link
Member

Choose a reason for hiding this comment

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

I don't think I understand how the logic in here works. Why are commas special versus other types of tokens handled by the other visit methods?

Copy link
Member Author

Choose a reason for hiding this comment

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

Commas are handled slightly specially in json (for error recovery purposes). They can both be a node (with a token in them), or just a token in separated list. We were only handling the first case.

Now we handle both cases by just checking the token case only.

Copy link
Member

Choose a reason for hiding this comment

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

Probably worth documenting that :)

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's what I thought that comment was :'(

Copy link
Member

Choose a reason for hiding this comment

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

This comment explains a bit of the why, but not the "commas are special for error recovery" part. That's what I needed to know to understand the code 🙂.

}
}
}
Expand Down