using System; using System.Collections.Generic; using System.Linq; using ComputeSharp.SourceGeneration.Helpers; namespace ComputeSharp.SourceGeneration.Extensions; /// /// Extension methods for the type. /// internal static class IndentedTextWriterExtensions { /// /// Writes the following attributes into a target writer: /// /// [global::System.CodeDom.Compiler.GeneratedCode("...", "...")] /// [global::System.Diagnostics.DebuggerNonUserCode] /// [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] /// /// /// The instance to write into. /// The name of the generator. /// Whether to use fully qualified type names or not. public static void WriteGeneratedAttributes(this IndentedTextWriter writer, string generatorName, bool useFullyQualifiedTypeNames = true) { // We can use this class to get the assembly, as all files for generators are just included // via shared projects. As such, the assembly will be the same as the generator type itself. Version assemblyVersion = typeof(IndentedTextWriterExtensions).Assembly.GetName().Version; if (useFullyQualifiedTypeNames) { writer.WriteLine($$"""[global::System.CodeDom.Compiler.GeneratedCode("{{generatorName}}", "{{assemblyVersion}}")]"""); writer.WriteLine($$"""[global::System.Diagnostics.DebuggerNonUserCode]"""); writer.WriteLine($$"""[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]"""); } else { writer.WriteLine($$"""[GeneratedCode("{{generatorName}}", "{{assemblyVersion}}")]"""); writer.WriteLine($$"""[DebuggerNonUserCode]"""); writer.WriteLine($$"""[ExcludeFromCodeCoverage]"""); } } /// /// Writes a sequence of using directives, sorted correctly. /// /// The instance to write into. /// The sequence of using directives to write. public static void WriteSortedUsingDirectives(this IndentedTextWriter writer, IEnumerable usingDirectives) { // Add the System directives first, in the correct order foreach (string usingDirective in usingDirectives.Where(static name => name.StartsWith("global::System", StringComparison.InvariantCulture)).OrderBy(static name => name)) { writer.WriteLine($"using {usingDirective};"); } // Add the other directives, also sorted in the correct order foreach (string usingDirective in usingDirectives.Where(static name => !name.StartsWith("global::System", StringComparison.InvariantCulture)).OrderBy(static name => name)) { writer.WriteLine($"using {usingDirective};"); } // Leave a trailing blank line if at least one using directive has been written. // This is so that any members will correctly have a leading blank line before. writer.WriteLineIf(usingDirectives.Any()); } /// /// 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(","); } } } }