From bd1b6ad9c3c92eb26849a593fe5749a1d844f7ae Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 23 Nov 2023 02:44:51 +0100 Subject: [PATCH] Fix extra whitespaces when writing multilines --- .../Helpers/IndentedTextWriter.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs b/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs index 84498903..9bfdb248 100644 --- a/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs +++ b/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs @@ -155,8 +155,12 @@ internal sealed class IndentedTextWriter : IDisposable } else { - // Write the current line - WriteRawText(content[..newLineIndex]); + ReadOnlySpan line = content[..newLineIndex]; + + // Write the current line (if it's empty, we can skip writing the text entirely). + // This ensures that raw multiline string literals with blank lines don't have + // extra whitespace at the start of those lines, which would otherwise happen. + WriteIf(!line.IsEmpty, line); WriteLine(); // Move past the new line character (the result could be an empty span)