Skip to content

Commit

Permalink
Pull trailing trivia out of forced flat portion of interpolated string
Browse files Browse the repository at this point in the history
closes #1258
  • Loading branch information
belav committed May 17, 2024
1 parent 831e5cc commit e28fb1d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
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

0 comments on commit e28fb1d

Please sign in to comment.