Skip to content

Commit

Permalink
Add WriteInterpolatedStringHandler for indented writer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Sep 26, 2023
1 parent e19766e commit 9c37a2a
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;

namespace ComputeSharp.SourceGeneration.Helpers;

Expand Down Expand Up @@ -152,6 +155,15 @@ public void Write(ReadOnlySpan<char> content, bool isMultiline = false)
}
}

/// <summary>
/// Writes content to the underlying buffer.
/// </summary>
/// <param name="handler">The interpolated string handler with content to write.</param>
public void Write([InterpolatedStringHandlerArgument("")] ref WriteInterpolatedStringHandler handler)
{
_ = this;
}

/// <summary>
/// Writes a line to the underlying buffer.
/// </summary>
Expand Down Expand Up @@ -181,6 +193,15 @@ public void WriteLine(ReadOnlySpan<char> content, bool isMultiline = false)
WriteLine();
}

/// <summary>
/// Writes content to the underlying buffer and appends a trailing new line.
/// </summary>
/// <param name="handler">The interpolated string handler with content to write.</param>
public void WriteLine([InterpolatedStringHandlerArgument("")] ref WriteInterpolatedStringHandler handler)
{
WriteLine();
}

/// <inheritdoc/>
public override string ToString()
{
Expand Down Expand Up @@ -240,4 +261,73 @@ public void Dispose()
}
}
}

/// <summary>
/// Provides a handler used by the language compiler to append interpolated strings into <see cref="IndentedTextWriter"/> instances.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
[InterpolatedStringHandler]
public readonly ref struct WriteInterpolatedStringHandler
{
/// <summary>The associated <see cref="IndentedTextWriter"/> to which to append.</summary>
private readonly IndentedTextWriter writer;

/// <summary>Creates a handler used to append an interpolated string into a <see cref="StringBuilder"/>.</summary>
/// <param name="literalLength">The number of constant characters outside of interpolation expressions in the interpolated string.</param>
/// <param name="formattedCount">The number of interpolation expressions in the interpolated string.</param>
/// <param name="writer">The associated <see cref="IndentedTextWriter"/> to which to append.</param>
/// <remarks>This is intended to be called only by compiler-generated code. Arguments are not validated as they'd otherwise be for members intended to be used directly.</remarks>
public WriteInterpolatedStringHandler(int literalLength, int formattedCount, IndentedTextWriter writer)
{
this.writer = writer;
}

/// <summary>Writes the specified string to the handler.</summary>
/// <param name="value">The string to write.</param>
public void AppendLiteral(string value)
{
this.writer.Write(value);
}

/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
public void AppendFormatted(string? value)
{
AppendFormatted<string?>(value);
}

/// <summary>Writes the specified character span to the handler.</summary>
/// <param name="value">The span to write.</param>
public void AppendFormatted(ReadOnlySpan<char> value)
{
this.writer.Write(value);
}

/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value)
{
if (value is not null)
{
this.writer.Write(value.ToString());
}
}

/// <summary>Writes the specified value to the handler.</summary>
/// <param name="value">The value to write.</param>
/// <param name="format">The format string.</param>
/// <typeparam name="T">The type of the value to write.</typeparam>
public void AppendFormatted<T>(T value, string? format)
{
if (value is IFormattable)
{
this.writer.Write(((IFormattable)value).ToString(format));
}
else if (value is not null)
{
this.writer.Write(value.ToString());
}
}
}
}

0 comments on commit 9c37a2a

Please sign in to comment.