From f74c01c6b46627ff2b96c66b83ec019e530cdb4a Mon Sep 17 00:00:00 2001 From: Bela VanderVoort Date: Tue, 22 Aug 2023 21:20:18 -0500 Subject: [PATCH] Fix issue with eating comments on raw literals with c# that has new lines closes #937 --- .../TestFiles/cs/StringLiterals.test | 13 +++++++++++++ .../InterpolatedStringExpression.cs | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/StringLiterals.test b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/StringLiterals.test index 4fbb6276b..ba810dedd 100644 --- a/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/StringLiterals.test +++ b/Src/CSharpier.Tests/FormattingTests/TestFiles/cs/StringLiterals.test @@ -161,5 +161,18 @@ four } "; ReadOnlySpan utf8StringLiteral = "hello"u8; + + var rawLiteralWithExpressionThatWeDontFormat = new StringContent( + // this comment shouldn't go away + $$""" + { + "params": "{{searchFilter switch + { + SearchFilter.Video => "EgIQAQ%3D%3D", + _ => null + }}}" + } + """ + ); } } diff --git a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InterpolatedStringExpression.cs b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InterpolatedStringExpression.cs index 966fe72bf..943be4084 100644 --- a/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InterpolatedStringExpression.cs +++ b/Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/InterpolatedStringExpression.cs @@ -9,7 +9,10 @@ public static Doc Print(InterpolatedStringExpressionSyntax node, FormattingConte // and new lines in expressions in them are rare. if (node.Contents.Any(o => o is InterpolationSyntax && o.ToString().Contains('\n'))) { - return node.ToString(); + return Doc.Concat( + Token.PrintLeadingTrivia(node.GetLeadingTrivia(), context), + node.ToString() + ); } var docs = new List