From 03828ab295aeae0b1d1221289f45c7e845d339f1 Mon Sep 17 00:00:00 2001 From: Beakona Date: Sun, 3 Dec 2023 23:26:56 +0100 Subject: [PATCH] bug with namespaces --- .github/workflows/nuget.yml | 2 +- .../BeaKona.AutoAsGenerator.csproj | 2 +- .../CSharpCodeTextWriter.cs | 52 +++++++++---------- .../GenerateAutoAsSourceGenerator.cs | 12 ++--- BeaKona.AutoAsGenerator/ICodeTextWriter.cs | 2 +- .../INamespaceSymbolExtensions.cs | 12 ++++- ...Extensions.cs => ITypeSymbolExtensions.cs} | 14 ++++- 7 files changed, 57 insertions(+), 39 deletions(-) rename BeaKona.AutoAsGenerator/{INamedTypeSymbolExtensions.cs => ITypeSymbolExtensions.cs} (59%) diff --git a/.github/workflows/nuget.yml b/.github/workflows/nuget.yml index 01f52fa..e137992 100644 --- a/.github/workflows/nuget.yml +++ b/.github/workflows/nuget.yml @@ -65,7 +65,7 @@ jobs: with: dotnet-version: ${{ env.DOTNET_VERSION }} - name: Download Artifact - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v1 with: name: nupkg - name: Push to nuget.org diff --git a/BeaKona.AutoAsGenerator/BeaKona.AutoAsGenerator.csproj b/BeaKona.AutoAsGenerator/BeaKona.AutoAsGenerator.csproj index 0621e65..ab30972 100644 --- a/BeaKona.AutoAsGenerator/BeaKona.AutoAsGenerator.csproj +++ b/BeaKona.AutoAsGenerator/BeaKona.AutoAsGenerator.csproj @@ -13,7 +13,7 @@ https://github.com/beakona/AutoAs git $(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput - 1.0.0 + 1.0.1 true true diff --git a/BeaKona.AutoAsGenerator/CSharpCodeTextWriter.cs b/BeaKona.AutoAsGenerator/CSharpCodeTextWriter.cs index 27de71a..4c5eae6 100644 --- a/BeaKona.AutoAsGenerator/CSharpCodeTextWriter.cs +++ b/BeaKona.AutoAsGenerator/CSharpCodeTextWriter.cs @@ -31,24 +31,24 @@ public void WriteTypeReference(SourceBuilder builder, ITypeSymbol type, ScopeInf processed = true; switch (type.SpecialType) { - default: processed = false; break; - case SpecialType.System_Object: builder.Append("object"); break; - case SpecialType.System_Void: builder.Append("void"); break; - case SpecialType.System_Boolean: builder.Append("bool"); break; - case SpecialType.System_Char: builder.Append("char"); break; - case SpecialType.System_SByte: builder.Append("sbyte"); break; - case SpecialType.System_Byte: builder.Append("byte"); break; - case SpecialType.System_Int16: builder.Append("short"); break; - case SpecialType.System_UInt16: builder.Append("ushort"); break; - case SpecialType.System_Int32: builder.Append("int"); break; - case SpecialType.System_UInt32: builder.Append("uint"); break; - case SpecialType.System_Int64: builder.Append("long"); break; - case SpecialType.System_UInt64: builder.Append("ulong"); break; - case SpecialType.System_Decimal: builder.Append("decimal"); break; - case SpecialType.System_Single: builder.Append("float"); break; - case SpecialType.System_Double: builder.Append("double"); break; - //case SpecialType.System_Half: builder.Append("half"); break; - case SpecialType.System_String: builder.Append("string"); break; + default: processed = false; break; + case SpecialType.System_Object: builder.Append("object"); break; + case SpecialType.System_Void: builder.Append("void"); break; + case SpecialType.System_Boolean: builder.Append("bool"); break; + case SpecialType.System_Char: builder.Append("char"); break; + case SpecialType.System_SByte: builder.Append("sbyte"); break; + case SpecialType.System_Byte: builder.Append("byte"); break; + case SpecialType.System_Int16: builder.Append("short"); break; + case SpecialType.System_UInt16: builder.Append("ushort"); break; + case SpecialType.System_Int32: builder.Append("int"); break; + case SpecialType.System_UInt32: builder.Append("uint"); break; + case SpecialType.System_Int64: builder.Append("long"); break; + case SpecialType.System_UInt64: builder.Append("ulong"); break; + case SpecialType.System_Decimal: builder.Append("decimal"); break; + case SpecialType.System_Single: builder.Append("float"); break; + case SpecialType.System_Double: builder.Append("double"); break; + //case SpecialType.System_Half: builder.Append("half"); break; + case SpecialType.System_String: builder.Append("string"); break; } } @@ -194,17 +194,13 @@ public void WriteTypeDeclarationBeginning(SourceBuilder builder, INamedTypeSymbo this.WriteTypeReference(builder, type, scope); } - public void WriteNamespaceBeginning(SourceBuilder builder, INamespaceSymbol @namespace) + public bool WriteNamespaceBeginning(SourceBuilder builder, INamespaceSymbol @namespace) { - if (@namespace != null && @namespace.ConstituentNamespaces.Length > 0) + if (@namespace != null) { - List containingNamespaces = []; - for (INamespaceSymbol? ct = @namespace; ct != null && ct.IsGlobalNamespace == false; ct = ct.ContainingNamespace) - { - containingNamespaces.Insert(0, ct); - } + INamespaceSymbol[] containingNamespaces = @namespace.GetNamespaceElements(); - if (containingNamespaces.Count > 0) + if (containingNamespaces.Length > 0) { builder.AppendIndentation(); builder.Append("namespace"); @@ -213,8 +209,12 @@ public void WriteNamespaceBeginning(SourceBuilder builder, INamespaceSymbol @nam builder.AppendIndentation(); builder.AppendLine('{'); builder.IncrementIndentation(); + + return true; } } + + return false; } public void WriteHolderReference(SourceBuilder builder, ISymbol member, ScopeInfo scope) diff --git a/BeaKona.AutoAsGenerator/GenerateAutoAsSourceGenerator.cs b/BeaKona.AutoAsGenerator/GenerateAutoAsSourceGenerator.cs index 989d42e..3e0c234 100644 --- a/BeaKona.AutoAsGenerator/GenerateAutoAsSourceGenerator.cs +++ b/BeaKona.AutoAsGenerator/GenerateAutoAsSourceGenerator.cs @@ -190,13 +190,9 @@ private static void GeneratePreview(GeneratorExecutionContext context, string na //bool isNullable = compilation.Options.NullableContextOptions == NullableContextOptions.Enable; builder.AppendLine("#nullable enable"); builder.AppendLine(); - writer.WriteNamespaceBeginning(builder, type.ContainingNamespace); + bool namespaceGenerated = writer.WriteNamespaceBeginning(builder, type.ContainingNamespace); - List containingTypes = []; - for (INamedTypeSymbol? ct = type.ContainingType; ct != null; ct = ct.ContainingType) - { - containingTypes.Insert(0, ct); - } + INamedTypeSymbol[] containingTypes = type.GetContainingTypes(); foreach (INamedTypeSymbol ct in containingTypes) { @@ -274,7 +270,7 @@ void WriteMethod(INamedTypeSymbol @interface, int? index) builder.AppendIndentation(); builder.Append('}'); - for (int i = 0; i < containingTypes.Count; i++) + for (int i = 0; i < containingTypes.Length; i++) { builder.AppendLine(); builder.DecrementIndentation(); @@ -282,7 +278,7 @@ void WriteMethod(INamedTypeSymbol @interface, int? index) builder.Append('}'); } - if (type.ContainingNamespace != null && type.ContainingNamespace.ConstituentNamespaces.Length > 0) + if (namespaceGenerated) { builder.AppendLine(); builder.DecrementIndentation(); diff --git a/BeaKona.AutoAsGenerator/ICodeTextWriter.cs b/BeaKona.AutoAsGenerator/ICodeTextWriter.cs index b67e581..bb0ae5d 100644 --- a/BeaKona.AutoAsGenerator/ICodeTextWriter.cs +++ b/BeaKona.AutoAsGenerator/ICodeTextWriter.cs @@ -9,7 +9,7 @@ internal interface ICodeTextWriter void WriteTypeArgumentsDefinition(SourceBuilder builder, IEnumerable typeArguments, ScopeInfo scope); void WriteTypeDeclarationBeginning(SourceBuilder builder, INamedTypeSymbol type, ScopeInfo scope); - void WriteNamespaceBeginning(SourceBuilder builder, INamespaceSymbol @namespace); + bool WriteNamespaceBeginning(SourceBuilder builder, INamespaceSymbol @namespace); void WriteHolderReference(SourceBuilder builder, ISymbol member, ScopeInfo scope); diff --git a/BeaKona.AutoAsGenerator/INamespaceSymbolExtensions.cs b/BeaKona.AutoAsGenerator/INamespaceSymbolExtensions.cs index b39e248..5a46264 100644 --- a/BeaKona.AutoAsGenerator/INamespaceSymbolExtensions.cs +++ b/BeaKona.AutoAsGenerator/INamespaceSymbolExtensions.cs @@ -6,11 +6,21 @@ internal static class INamespaceSymbolExtensions { INamespaceSymbol? last = null; - for (var n = @this; n.IsGlobalNamespace == false; n = n.ContainingNamespace) + for (var n = @this; n != null && n.IsGlobalNamespace == false; n = n.ContainingNamespace) { last = n; } return last; } + + public static INamespaceSymbol[] GetNamespaceElements(this INamespaceSymbol @this) + { + List containingNamespaces = []; + for (INamespaceSymbol? n = @this; n != null && n.IsGlobalNamespace == false; n = n.ContainingNamespace) + { + containingNamespaces.Insert(0, n); + } + return [.. containingNamespaces]; + } } diff --git a/BeaKona.AutoAsGenerator/INamedTypeSymbolExtensions.cs b/BeaKona.AutoAsGenerator/ITypeSymbolExtensions.cs similarity index 59% rename from BeaKona.AutoAsGenerator/INamedTypeSymbolExtensions.cs rename to BeaKona.AutoAsGenerator/ITypeSymbolExtensions.cs index 05eed70..6a403ad 100644 --- a/BeaKona.AutoAsGenerator/INamedTypeSymbolExtensions.cs +++ b/BeaKona.AutoAsGenerator/ITypeSymbolExtensions.cs @@ -3,7 +3,7 @@ namespace BeaKona.AutoAsGenerator; -internal static class INamedTypeSymbolExtensions +internal static class ITypeSymbolExtensions { public static bool IsPartial(this ITypeSymbol @this) { @@ -20,4 +20,16 @@ public static bool IsPartial(this ITypeSymbol @this) return false; } + + public static INamedTypeSymbol[] GetContainingTypes(this ITypeSymbol @this) + { + List containingTypes = []; + + for (INamedTypeSymbol? ct = @this.ContainingType; ct != null; ct = ct.ContainingType) + { + containingTypes.Insert(0, ct); + } + + return [.. containingTypes]; + } }