From e28fb1d87f4b96927632e096202d9b4ea31a306a Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Fri, 17 May 2024 12:39:46 -0500 Subject: [PATCH] Pull trailing trivia out of forced flat portion of interpolated string closes #1258 --- .../cs/InterpolatedStringExpressions.test | 4 ++++ .../InterpolatedStringExpression.cs | 7 ++++--- Src/CSharpier/SyntaxPrinter/Token.cs | 20 ++++++++++++++----- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterpolatedStringExpressions.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterpolatedStringExpressions.test index e00c2d846..308bf5e41 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterpolatedStringExpressions.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/InterpolatedStringExpressions.test @@ -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 = diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InterpolatedStringExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InterpolatedStringExpression.cs index bce5981fb..bcfb74baa 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InterpolatedStringExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InterpolatedStringExpression.cs @@ -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) ); } diff --git a/Src/CSharpier/SyntaxPrinter/Token.cs b/Src/CSharpier/SyntaxPrinter/Token.cs index 3262ecd8f..7c3fe4c45 100644 --- a/Src/CSharpier/SyntaxPrinter/Token.cs +++ b/Src/CSharpier/SyntaxPrinter/Token.cs @@ -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); @@ -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) @@ -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) @@ -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); }