From b4cfad5554e8954250469be91fe61834e2b073bb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 25 Sep 2023 02:37:28 +0200 Subject: [PATCH] Add IndentedTextWriter APIs to write with no new lines --- .../Helpers/IndentedTextWriter.cs | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs b/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs index 07506ebf..1a83eb75 100644 --- a/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs +++ b/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs @@ -109,11 +109,13 @@ internal sealed class IndentedTextWriter : IDisposable } /// - /// Writes a line to the underlying buffer. + /// Writes content to the underlying buffer. /// - public void WriteLine() + /// The content to write. + /// Whether the input content is multiline. + public void Write(string content, bool isMultiline = false) { - this.builder.Add(DefaultNewLine); + Write(content.AsSpan(), isMultiline); } /// @@ -121,17 +123,7 @@ internal sealed class IndentedTextWriter : IDisposable /// /// The content to write. /// Whether the input content is multiline. - public void WriteLine(string content, bool isMultiline = false) - { - WriteLine(content.AsSpan(), isMultiline); - } - - /// - /// Writes content to the underlying buffer. - /// - /// The content to write. - /// Whether the input content is multiline. - public void WriteLine(ReadOnlySpan content, bool isMultiline = false) + public void Write(ReadOnlySpan content, bool isMultiline = false) { if (isMultiline) { @@ -142,12 +134,12 @@ internal sealed class IndentedTextWriter : IDisposable if (newLineIndex < 0) { // There are no new lines left, so the content can be written as a single line - WriteSingleLine(content); + WriteRawText(content); } else { // Write the current line - WriteSingleLine(content[..newLineIndex]); + WriteRawText(content[..newLineIndex]); // Move past the new line character (the result could be an empty span) content = content[(newLineIndex + 1)..]; @@ -156,10 +148,39 @@ internal sealed class IndentedTextWriter : IDisposable } else { - WriteSingleLine(content); + WriteRawText(content); } } + /// + /// Writes a line to the underlying buffer. + /// + public void WriteLine() + { + this.builder.Add(DefaultNewLine); + } + + /// + /// Writes content to the underlying buffer and appends a trailing new line. + /// + /// The content to write. + /// Whether the input content is multiline. + public void WriteLine(string content, bool isMultiline = false) + { + WriteLine(content.AsSpan(), isMultiline); + } + + /// + /// Writes content to the underlying buffer and appends a trailing new line. + /// + /// The content to write. + /// Whether the input content is multiline. + public void WriteLine(ReadOnlySpan content, bool isMultiline = false) + { + Write(content, isMultiline); + WriteLine(); + } + /// public override string ToString() { @@ -173,10 +194,10 @@ internal sealed class IndentedTextWriter : IDisposable } /// - /// Writes a single line to the underlying buffer. + /// Writes raw text to the underlying buffer, adding leading indentation if needed. /// - /// The content to write. - private void WriteSingleLine(ReadOnlySpan content) + /// The raw text to write. + private void WriteRawText(ReadOnlySpan content) { if (this.builder.Count == 0 || this.builder.WrittenSpan[^1] == DefaultNewLine) {