diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputDescriptionsProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputDescriptionsProperty.Syntax.cs
index 4f822624..0af11a60 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputDescriptionsProperty.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputDescriptionsProperty.Syntax.cs
@@ -1,6 +1,9 @@
using ComputeSharp.D2D1.SourceGenerators.Models;
+using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
+#pragma warning disable IDE0053
+
namespace ComputeSharp.D2D1.SourceGenerators;
///
@@ -99,17 +102,10 @@ partial class ID2D1ShaderGenerator
writer.IncreaseIndent();
// Initialize all input descriptions
- for (int i = 0; i < info.InputDescriptions.Length; i++)
+ writer.WriteInitializationExpressions(info.InputDescriptions.AsSpan(), static (description, writer) =>
{
- InputDescription description = info.InputDescriptions[i];
-
writer.Write($$"""new global::ComputeSharp.D2D1.Interop.D2D1InputDescription({{description.Index}}, global::ComputeSharp.D2D1.D2D1Filter.{{description.Filter}}) { LevelOfDetailCount = {{description.LevelOfDetailCount}} }""");
-
- if (i < info.InputDescriptions.Length - 1)
- {
- writer.WriteLine(",");
- }
- }
+ });
writer.DecreaseIndent();
writer.WriteLine();
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs
index 3a8a9e00..dc114166 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateInputTypesProperty.Syntax.cs
@@ -1,4 +1,5 @@
using ComputeSharp.D2D1.SourceGenerators.Models;
+using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
namespace ComputeSharp.D2D1.SourceGenerators;
@@ -68,16 +69,11 @@ partial class ID2D1ShaderGenerator
writer.IncreaseIndent();
// Input types, one per line in the RVA field initializer
- for (int i = 0; i < info.InputTypes.Length; i++)
+ writer.WriteInitializationExpressions(info.InputTypes.AsSpan(), static (type, writer) =>
{
writer.Write("global::ComputeSharp.D2D1.Interop.D2D1PixelShaderInputType.");
- writer.Write(info.InputTypes[i] == 0 ? "Simple" : "Complex");
-
- if (i < info.InputTypes.Length - 1)
- {
- writer.WriteLine(",");
- }
- }
+ writer.Write(type == 0 ? "Simple" : "Complex");
+ });
writer.DecreaseIndent();
writer.WriteLine();
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
index d22e2de0..5a9605fd 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateLoadDispatchDataMethod.Syntax.cs
@@ -1,4 +1,5 @@
using ComputeSharp.D2D1.SourceGenerators.Models;
+using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
using ComputeSharp.SourceGeneration.Mappings;
@@ -93,14 +94,9 @@ partial class ID2D1ShaderGenerator
using (writer.WriteBlock())
{
// Declare fields for every mapped item from the shader layout
- for (int i = 0; i < info.Fields.Length; i++)
+ writer.WriteLineSeparatedMembers(info.Fields.AsSpan(), (field, writer) =>
{
- if (i > 0)
- {
- writer.WriteLine();
- }
-
- switch (info.Fields[i])
+ switch (field)
{
case FieldInfo.Primitive { TypeName: "System.Boolean" } primitive:
@@ -131,7 +127,7 @@ partial class ID2D1ShaderGenerator
break;
}
- }
+ });
}
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateResourceTextureDescriptionsProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateResourceTextureDescriptionsProperty.Syntax.cs
index 76e4f735..4514e9f9 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateResourceTextureDescriptionsProperty.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateResourceTextureDescriptionsProperty.Syntax.cs
@@ -1,6 +1,9 @@
using ComputeSharp.D2D1.SourceGenerators.Models;
+using ComputeSharp.SourceGeneration.Extensions;
using ComputeSharp.SourceGeneration.Helpers;
+#pragma warning disable IDE0053
+
namespace ComputeSharp.D2D1.SourceGenerators;
///
@@ -52,17 +55,10 @@ partial class ID2D1ShaderGenerator
writer.IncreaseIndent();
// Initialize all resource texture descriptions
- for (int i = 0; i < info.ResourceTextureDescriptions.Length; i++)
+ writer.WriteInitializationExpressions(info.ResourceTextureDescriptions.AsSpan(), static (description, writer) =>
{
- ResourceTextureDescription description = info.ResourceTextureDescriptions[i];
-
writer.Write($"new global::ComputeSharp.D2D1.Interop.D2D1ResourceTextureDescription({description.Index}, {description.Rank})");
-
- if (i < info.ResourceTextureDescriptions.Length - 1)
- {
- writer.WriteLine(",");
- }
- }
+ });
writer.DecreaseIndent();
writer.WriteLine();
diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems
index a223225c..c258c5c8 100644
--- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems
+++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems
@@ -13,6 +13,7 @@
+
diff --git a/src/ComputeSharp.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs b/src/ComputeSharp.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs
new file mode 100644
index 00000000..f7b1f2a6
--- /dev/null
+++ b/src/ComputeSharp.SourceGeneration/Extensions/IndentedTextWriterExtensions.cs
@@ -0,0 +1,56 @@
+using System;
+using ComputeSharp.SourceGeneration.Helpers;
+
+namespace ComputeSharp.SourceGeneration.Extensions;
+
+///
+/// Extension methods for the type.
+///
+internal static class IndentedTextWriterExtensions
+{
+ ///
+ /// Writes a series of members separated by one line between each of them.
+ ///
+ /// The type of input items to process.
+ /// The instance to write into.
+ /// The input items to process.
+ /// The instance to invoke for each item.
+ public static void WriteLineSeparatedMembers(
+ this IndentedTextWriter writer,
+ ReadOnlySpan items,
+ IndentedTextWriter.Callback callback)
+ {
+ for (int i = 0; i < items.Length; i++)
+ {
+ if (i > 0)
+ {
+ writer.WriteLine();
+ }
+
+ callback(items[i], writer);
+ }
+ }
+
+ ///
+ /// Writes a series of initialization expressions separated by a comma between each of them.
+ ///
+ /// The type of input items to process.
+ /// The instance to write into.
+ /// The input items to process.
+ /// The instance to invoke for each item.
+ public static void WriteInitializationExpressions(
+ this IndentedTextWriter writer,
+ ReadOnlySpan items,
+ IndentedTextWriter.Callback callback)
+ {
+ for (int i = 0; i < items.Length; i++)
+ {
+ callback(items[i], writer);
+
+ if (i < items.Length - 1)
+ {
+ writer.WriteLine(",");
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs
index 6452144d..cd53fca9 100644
--- a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs
+++ b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs
@@ -73,15 +73,7 @@ internal sealed partial record HierarchyInfo(string FullyQualifiedMetadataName,
}
// Generate all nested members
- for (int i = 0; i < memberCallbacks.Length; i++)
- {
- if (i > 0)
- {
- writer.WriteLine();
- }
-
- memberCallbacks[i](state, writer);
- }
+ writer.WriteLineSeparatedMembers(memberCallbacks, (callback, writer) => callback(state, writer));
// Close all scopes and reduce the indentation
for (int i = 0; i < Hierarchy.Length; i++)