Skip to content

Commit

Permalink
bug with namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
beakona committed Dec 3, 2023
1 parent 5cd511e commit 03828ab
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion BeaKona.AutoAsGenerator/BeaKona.AutoAsGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<RepositoryUrl>https://github.com/beakona/AutoAs</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);_AddAnalyzersToOutput</TargetsForTfmSpecificContentInPackage>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<IsRoslynComponent>true</IsRoslynComponent>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
Expand Down
52 changes: 26 additions & 26 deletions BeaKona.AutoAsGenerator/CSharpCodeTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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<INamespaceSymbol> 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");
Expand All @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions BeaKona.AutoAsGenerator/GenerateAutoAsSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<INamedTypeSymbol> containingTypes = [];
for (INamedTypeSymbol? ct = type.ContainingType; ct != null; ct = ct.ContainingType)
{
containingTypes.Insert(0, ct);
}
INamedTypeSymbol[] containingTypes = type.GetContainingTypes();

foreach (INamedTypeSymbol ct in containingTypes)
{
Expand Down Expand Up @@ -274,15 +270,15 @@ 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();
builder.AppendIndentation();
builder.Append('}');
}

if (type.ContainingNamespace != null && type.ContainingNamespace.ConstituentNamespaces.Length > 0)
if (namespaceGenerated)
{
builder.AppendLine();
builder.DecrementIndentation();
Expand Down
2 changes: 1 addition & 1 deletion BeaKona.AutoAsGenerator/ICodeTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal interface ICodeTextWriter
void WriteTypeArgumentsDefinition(SourceBuilder builder, IEnumerable<ITypeSymbol> 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);

Expand Down
12 changes: 11 additions & 1 deletion BeaKona.AutoAsGenerator/INamespaceSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<INamespaceSymbol> containingNamespaces = [];
for (INamespaceSymbol? n = @this; n != null && n.IsGlobalNamespace == false; n = n.ContainingNamespace)
{
containingNamespaces.Insert(0, n);
}
return [.. containingNamespaces];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace BeaKona.AutoAsGenerator;

internal static class INamedTypeSymbolExtensions
internal static class ITypeSymbolExtensions
{
public static bool IsPartial(this ITypeSymbol @this)
{
Expand All @@ -20,4 +20,16 @@ public static bool IsPartial(this ITypeSymbol @this)

return false;
}

public static INamedTypeSymbol[] GetContainingTypes(this ITypeSymbol @this)
{
List<INamedTypeSymbol> containingTypes = [];

for (INamedTypeSymbol? ct = @this.ContainingType; ct != null; ct = ct.ContainingType)
{
containingTypes.Insert(0, ct);
}

return [.. containingTypes];
}
}

0 comments on commit 03828ab

Please sign in to comment.