diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs
index 59765261..575c1c9d 100644
--- a/src/ComputeSharp.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs
+++ b/src/ComputeSharp.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using System.Runtime.CompilerServices;
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.SourceGeneration.Extensions;
@@ -66,50 +65,6 @@ internal static class IndentedTextWriterExtensions
writer.WriteLineIf(usingDirectives.Any());
}
- ///
- /// Writes a new line into the target writer depending on an input condition.
- ///
- /// The instance to write into.
- /// The condition to use to decide whether or not to write a new line.
- public static void WriteLineIf(this IndentedTextWriter writer, bool condition)
- {
- if (condition)
- {
- writer.WriteLine();
- }
- }
-
- ///
- /// Writes a new line into the target writer depending on an input condition.
- ///
- /// The instance to write into.
- /// The content to write.
- /// The condition to use to decide whether or not to write a new line.
- public static void WriteLineIf(this IndentedTextWriter writer, string content, bool condition)
- {
- if (condition)
- {
- writer.WriteLine(content);
- }
- }
-
- ///
- /// Writes a new line into the target writer depending on an input condition.
- ///
- /// The instance to write into.
- /// The interpolated string handler with content to write.
- /// The condition to use to decide whether or not to write a new line.
- public static void WriteLineIf(
- this IndentedTextWriter writer,
- [InterpolatedStringHandlerArgument(nameof(writer))] ref IndentedTextWriter.WriteInterpolatedStringHandler handler,
- bool condition)
- {
- if (condition)
- {
- writer.WriteLine(ref handler);
- }
- }
-
///
/// Writes a series of members separated by one line between each of them.
///
diff --git a/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs b/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs
index 29ba93c2..d095a16c 100644
--- a/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs
+++ b/src/ComputeSharp.SourceGeneration/Helpers/IndentedTextWriter.cs
@@ -177,6 +177,44 @@ internal sealed class IndentedTextWriter : IDisposable
_ = this;
}
+ ///
+ /// Writes content to the underlying buffer depending on an input condition.
+ ///
+ /// The condition to use to decide whether or not to write content.
+ /// The content to write.
+ /// Whether the input content is multiline.
+ public void WriteIf(bool condition, string content, bool isMultiline = false)
+ {
+ if (condition)
+ {
+ Write(content.AsSpan(), isMultiline);
+ }
+ }
+
+ ///
+ /// Writes content to the underlying buffer depending on an input condition.
+ ///
+ /// The condition to use to decide whether or not to write content.
+ /// The content to write.
+ /// Whether the input content is multiline.
+ public void WriteIf(bool condition, ReadOnlySpan content, bool isMultiline = false)
+ {
+ if (condition)
+ {
+ Write(content, isMultiline);
+ }
+ }
+
+ ///
+ /// Writes content to the underlying buffer depending on an input condition.
+ ///
+ /// The condition to use to decide whether or not to write content.
+ /// The interpolated string handler with content to write.
+ public void WriteIf(bool condition, [InterpolatedStringHandlerArgument("", nameof(condition))] ref WriteIfInterpolatedStringHandler handler)
+ {
+ _ = this;
+ }
+
///
/// Writes a line to the underlying buffer.
///
@@ -221,6 +259,61 @@ internal sealed class IndentedTextWriter : IDisposable
WriteLine();
}
+ ///
+ /// Writes a line to the underlying buffer depending on an input condition.
+ ///
+ /// The condition to use to decide whether or not to write content.
+ /// Indicates whether to skip adding the line if there already is one.
+ public void WriteLineIf(bool condition, bool skipIfPresent = false)
+ {
+ if (condition)
+ {
+ WriteLine(skipIfPresent);
+ }
+ }
+
+ ///
+ /// Writes content to the underlying buffer and appends a trailing new line depending on an input condition.
+ ///
+ /// The condition to use to decide whether or not to write content.
+ /// The content to write.
+ /// Whether the input content is multiline.
+ public void WriteLineIf(bool condition, string content, bool isMultiline = false)
+ {
+ if (condition)
+ {
+ WriteLine(content.AsSpan(), isMultiline);
+ }
+ }
+
+ ///
+ /// Writes content to the underlying buffer and appends a trailing new line depending on an input condition.
+ ///
+ /// The condition to use to decide whether or not to write content.
+ /// The content to write.
+ /// Whether the input content is multiline.
+ public void WriteLineIf(bool condition, ReadOnlySpan content, bool isMultiline = false)
+ {
+ if (condition)
+ {
+ Write(content, isMultiline);
+ WriteLine();
+ }
+ }
+
+ ///
+ /// Writes content to the underlying buffer and appends a trailing new line depending on an input condition.
+ ///
+ /// The condition to use to decide whether or not to write content.
+ /// The interpolated string handler with content to write.
+ public void WriteLineIf(bool condition, [InterpolatedStringHandlerArgument("", nameof(condition))] ref WriteIfInterpolatedStringHandler handler)
+ {
+ if (condition)
+ {
+ WriteLine();
+ }
+ }
+
///
public override string ToString()
{
@@ -357,4 +450,68 @@ internal sealed class IndentedTextWriter : IDisposable
}
}
}
+
+ ///
+ /// Provides a handler used by the language compiler to conditionally append interpolated strings into instances.
+ ///
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ [InterpolatedStringHandler]
+ public readonly ref struct WriteIfInterpolatedStringHandler
+ {
+ /// The associated to use.
+ private readonly WriteInterpolatedStringHandler handler;
+
+ /// Creates a handler used to append an interpolated string into a .
+ /// The number of constant characters outside of interpolation expressions in the interpolated string.
+ /// The number of interpolation expressions in the interpolated string.
+ /// The associated to which to append.
+ /// The condition to use to decide whether or not to write content.
+ /// A value indicating whether formatting should proceed.
+ /// 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.
+ public WriteIfInterpolatedStringHandler(int literalLength, int formattedCount, IndentedTextWriter writer, bool condition, out bool shouldAppend)
+ {
+ if (condition)
+ {
+ this.handler = new WriteInterpolatedStringHandler(literalLength, formattedCount, writer);
+
+ shouldAppend = true;
+ }
+ else
+ {
+ this.handler = default;
+
+ shouldAppend = false;
+ }
+ }
+
+ ///
+ public void AppendLiteral(string value)
+ {
+ this.handler.AppendLiteral(value);
+ }
+
+ ///
+ public void AppendFormatted(string? value)
+ {
+ this.handler.AppendFormatted(value);
+ }
+
+ ///
+ public void AppendFormatted(ReadOnlySpan value)
+ {
+ this.handler.AppendFormatted(value);
+ }
+
+ ///
+ public void AppendFormatted(T value)
+ {
+ this.handler.AppendFormatted(value);
+ }
+
+ ///
+ public void AppendFormatted(T value, string? format)
+ {
+ this.handler.AppendFormatted(value, format);
+ }
+ }
}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs
index b3300ffd..ccdd6076 100644
--- a/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs
+++ b/src/ComputeSharp.SourceGenerators/ComputeShaderDescriptorGenerator.DispatchDataLoading.Syntax.cs
@@ -33,7 +33,7 @@ partial class ComputeShaderDescriptorGenerator
// Append the statements for the dispatch ranges
writer.WriteLine("global::System.Runtime.CompilerServices.Unsafe.As(ref span[0]) = (uint)x;");
writer.WriteLine("global::System.Runtime.CompilerServices.Unsafe.As(ref span[4]) = (uint)y;");
- writer.WriteLineIf("global::System.Runtime.CompilerServices.Unsafe.As(ref span[8]) = (uint)z;", !info.IsPixelShaderLike);
+ writer.WriteLineIf(!info.IsPixelShaderLike, "global::System.Runtime.CompilerServices.Unsafe.As(ref span[8]) = (uint)z;");
// Generate loading statements for each captured field
foreach (FieldInfo fieldInfo in info.Fields)