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

Pull trailing trivia out of forced flat portion of interpolated string #1261

Merged
merged 1 commit into from
May 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class ClassName
// comment stays on this line
$"two";

public string TrailingComment = someCondition
? $"empty" // trailing comment
: someString;

public string TwoValues = $"{SomeValue}{SomeOtherValue}";

public string ForceFlatInString =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public static Doc Print(InterpolatedStringExpressionSyntax node, FormattingConte
};

docs.AddRange(node.Contents.Select(o => Node.Print(o, context)));
docs.Add(Token.Print(node.StringEndToken, context));
docs.Add(Token.PrintWithoutTrailingTrivia(node.StringEndToken, context));

return Doc.Concat(
// pull out the leading trivia so it doesn't get forced flat
// pull out the trivia so it doesn't get forced flat
Token.PrintLeadingTrivia(node.StringStartToken, context),
Doc.ForceFlat(docs)
Doc.ForceFlat(docs),
Token.PrintTrailingTrivia(node.StringEndToken)
);
}

Expand Down
20 changes: 15 additions & 5 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public static Doc PrintWithoutLeadingTrivia(SyntaxToken syntaxToken, FormattingC
return PrintSyntaxToken(syntaxToken, context, skipLeadingTrivia: true);
}

public static Doc PrintWithoutTrailingTrivia(SyntaxToken syntaxToken, FormattingContext context)
{
return PrintSyntaxToken(syntaxToken, context, skipTrailingTrivia: true);
}

public static Doc Print(SyntaxToken syntaxToken, FormattingContext context)
{
return PrintSyntaxToken(syntaxToken, context);
Expand All @@ -30,7 +35,8 @@ private static Doc PrintSyntaxToken(
SyntaxToken syntaxToken,
FormattingContext context,
Doc? suffixDoc = null,
bool skipLeadingTrivia = false
bool skipLeadingTrivia = false,
bool skipTrailingTrivia = false
)
{
if (syntaxToken.RawSyntaxKind() == SyntaxKind.None)
Expand Down Expand Up @@ -113,10 +119,14 @@ or SyntaxKind.InterpolatedRawStringEndToken
{
docs.Add(syntaxToken.Text);
}
var trailingTrivia = PrintTrailingTrivia(syntaxToken);
if (trailingTrivia != Doc.Null)

if (!skipTrailingTrivia)
{
docs.Add(trailingTrivia);
var trailingTrivia = PrintTrailingTrivia(syntaxToken);
if (trailingTrivia != Doc.Null)
{
docs.Add(trailingTrivia);
}
}

if (suffixDoc != null)
Expand Down Expand Up @@ -344,7 +354,7 @@ private static bool IsMultiLineComment(SyntaxKind kind) =>
private static bool IsRegion(SyntaxKind kind) =>
kind is SyntaxKind.RegionDirectiveTrivia or SyntaxKind.EndRegionDirectiveTrivia;

private static Doc PrintTrailingTrivia(SyntaxToken node)
public static Doc PrintTrailingTrivia(SyntaxToken node)
{
return PrintTrailingTrivia(node.TrailingTrivia);
}
Expand Down
Loading