Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 18 additions & 2 deletions src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public override SyntaxToken VisitToken(SyntaxToken token)
isTrailing: false,
indentAfterLineBreak: NeedsIndentAfterLineBreak(token),
mustHaveSeparator: false,
lineBreaksAfter: 0));
lineBreaksAfter: lineBreaksAfterLeading(token)));

var nextToken = this.GetNextRelevantToken(token);

Expand All @@ -118,6 +118,22 @@ public override SyntaxToken VisitToken(SyntaxToken token)
lineBreaksAfter: lineBreaksAfter));

return tk;

static int lineBreaksAfterLeading(SyntaxToken syntaxToken)
{
if (syntaxToken.LeadingTrivia.Count < 2)
{
return 0;
}

if (syntaxToken.LeadingTrivia[^2].IsKind(SyntaxKind.MultiLineDocumentationCommentTrivia) &&
syntaxToken.LeadingTrivia[^1].IsKind(SyntaxKind.EndOfLineTrivia))
{
return 1;
}

return 0;
}
}
finally
{
Expand Down Expand Up @@ -1247,7 +1263,7 @@ private static bool EndsInLineBreak(SyntaxTrivia trivia)
}
else
{
return IsLineBreak(node.GetLastToken());
return !node.IsKind(SyntaxKind.MultiLineDocumentationCommentTrivia) && IsLineBreak(node.GetLastToken());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3790,6 +3790,26 @@ void goo()
"}");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/76856")]
public void TestNormalizeDocumentationMultiLineCommentsWithTrailingNewline()
{
TestNormalizeStatement("""
/**
*
* Escape XML special characters
*/
private String EscapeXML(String str)
{
""", """
///
///
/// Escape XML special characters
//////
Copy link
Member

Choose a reason for hiding this comment

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

Why do we generate so many slashes here?

Copy link
Member

Choose a reason for hiding this comment

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

I thought the same thing, but that generation is outside the scope of this request, imo. The current behavior has just as many slashes, but misses the newline after them so it ends up as //////private String EscapeXML(String str).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, I didn't modify the resulting slashes because I didn't know if that was the expected result from a normalization point of view.
For future reference, the reason these additional slashes exist has to do with the fact that the comment ending token is padded with a whitespace, and both the padding and the comment ending token get replaced by ///.

If the test instead had input of

/**
 * 
 * Escape XML special characters
*/

there would be 3 slashes only.

If you think this should be fixed and padding should be ignored when replacing with slashes at comment end, let me know. I can pick this up as a follow-up improvement PR.

private String EscapeXML(String str)
{
""");
}

[Fact]
public void TestNormalizeEOL()
{
Expand Down
Loading