From 302ac27294a1ebba56b4bbff271a22d88557c744 Mon Sep 17 00:00:00 2001 From: OhFlowi Date: Fri, 15 Mar 2024 15:32:17 +0100 Subject: [PATCH] BREAKING CHANGE: Changed the namespace of the generated extension methods to the same as the enum. Moved the generated attribute namespace to the generator namespace. --- .../EnumSourceGenerator.cs | 6 ++- .../Extensions/NamespaceSymbolExtensions.cs | 40 +++++++++++++++++++ test/Console.Test.Benchmark/Program.cs | 2 +- test/UnitTests/EnumGeneratorTest.cs | 4 +- test/UnitTests/InternalEnumGeneratorTest.cs | 4 +- 5 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 Supernova.Enum.Generators/Extensions/NamespaceSymbolExtensions.cs diff --git a/Supernova.Enum.Generators/EnumSourceGenerator.cs b/Supernova.Enum.Generators/EnumSourceGenerator.cs index a567f3e..644a698 100644 --- a/Supernova.Enum.Generators/EnumSourceGenerator.cs +++ b/Supernova.Enum.Generators/EnumSourceGenerator.cs @@ -5,6 +5,7 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; +using Supernova.Enum.Generators.Extensions; namespace Supernova.Enum.Generators; @@ -24,7 +25,7 @@ public void Execute(GeneratorExecutionContext context) // } //#endif context.AddSource($"{SourceGeneratorHelper.AttributeName}Attribute.g.cs", SourceText.From($@"using System; -namespace {SourceGeneratorHelper.NameSpace} +namespace Supernova.Enum.Generators {{ [AttributeUsage(AttributeTargets.Enum)] public sealed class {SourceGeneratorHelper.AttributeName}Attribute : Attribute @@ -104,7 +105,8 @@ public sealed class {SourceGeneratorHelper.AttributeName}Attribute : Attribute var sourceBuilder = new StringBuilder($@"using System; using System.Collections.Generic; using System.Collections.Immutable; -namespace {SourceGeneratorHelper.NameSpace} + +namespace {symbol.ContainingNamespace.FullName()} {{ {symbol.DeclaredAccessibility.ToString("G").ToLower()} static class {symbol.Name}EnumExtensions {{"); diff --git a/Supernova.Enum.Generators/Extensions/NamespaceSymbolExtensions.cs b/Supernova.Enum.Generators/Extensions/NamespaceSymbolExtensions.cs new file mode 100644 index 0000000..49202bf --- /dev/null +++ b/Supernova.Enum.Generators/Extensions/NamespaceSymbolExtensions.cs @@ -0,0 +1,40 @@ +using System; +using Microsoft.CodeAnalysis; + +namespace Supernova.Enum.Generators.Extensions; + +/// +/// Provides extension methods for objects. +/// +public static class NamespaceSymbolExtensions +{ + /// + /// Gets the full name of the namespace, including parent namespaces. + /// + /// The namespace symbol. + /// Optional. The initial full name to start with. + /// The full name of the namespace. + public static string FullName(this INamespaceSymbol namespaceSymbol, string fullName = null) + { + fullName ??= string.Empty; + + if (namespaceSymbol == null) + { + return fullName; + } + + if (namespaceSymbol.ContainingNamespace != null) + { + fullName = namespaceSymbol.ContainingNamespace.FullName(fullName); + } + + if (!fullName.Equals(string.Empty, StringComparison.OrdinalIgnoreCase)) + { + fullName += "."; + } + + fullName += namespaceSymbol.Name; + + return fullName; + } +} diff --git a/test/Console.Test.Benchmark/Program.cs b/test/Console.Test.Benchmark/Program.cs index 4985a18..2b8585e 100644 --- a/test/Console.Test.Benchmark/Program.cs +++ b/test/Console.Test.Benchmark/Program.cs @@ -8,13 +8,13 @@ using BenchmarkDotNet.Jobs; using BenchmarkDotNet.Order; using BenchmarkDotNet.Running; -using EnumFastToStringGenerated; using Perfolizer.Horology; using System; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; +using Supernova.Enum.Generators; namespace Console.Test.Benchmark; diff --git a/test/UnitTests/EnumGeneratorTest.cs b/test/UnitTests/EnumGeneratorTest.cs index c0e50bb..981343a 100644 --- a/test/UnitTests/EnumGeneratorTest.cs +++ b/test/UnitTests/EnumGeneratorTest.cs @@ -1,9 +1,9 @@ -using EnumFastToStringGenerated; -using FluentAssertions; +using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using Supernova.Enum.Generators; namespace UnitTests; diff --git a/test/UnitTests/InternalEnumGeneratorTest.cs b/test/UnitTests/InternalEnumGeneratorTest.cs index 84a5a5a..01edcb0 100644 --- a/test/UnitTests/InternalEnumGeneratorTest.cs +++ b/test/UnitTests/InternalEnumGeneratorTest.cs @@ -1,9 +1,9 @@ -using EnumFastToStringGenerated; -using FluentAssertions; +using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using Supernova.Enum.Generators; namespace UnitTests;