Skip to content

Commit

Permalink
Fixed namespace generation issue MarimerLLC#4455
Browse files Browse the repository at this point in the history
  • Loading branch information
jensbrand committed Jan 23, 2025
1 parent 6d4798a commit f62a733
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,46 @@ private static string GetBaseClassTypeName(DefinitionExtractionContext extractio
/// <returns>The namespace of the type for which generation is being performed</returns>
private static string GetNamespace(TypeDeclarationSyntax targetTypeDeclaration)
{
string namespaceName = string.Empty;
NamespaceDeclarationSyntax namespaceDeclaration;
TypeDeclarationSyntax containingTypeDeclaration;

// Iterate through the containing types should the target type be nested inside other types
containingTypeDeclaration = targetTypeDeclaration;
while (containingTypeDeclaration.Parent is TypeDeclarationSyntax syntax)
// If we don't have a namespace at all we'll return an empty string
// This accounts for the "default namespace" case
string nameSpace = string.Empty;

// Get the containing syntax node for the type declaration
// (could be a nested type, for example)
SyntaxNode potentialNamespaceParent = targetTypeDeclaration.Parent;

// Keep moving "out" of nested classes etc until we get to a namespace
// or until we run out of parents
while (potentialNamespaceParent != null &&
potentialNamespaceParent is not NamespaceDeclarationSyntax
&& potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax)
{
containingTypeDeclaration = syntax;
potentialNamespaceParent = potentialNamespaceParent.Parent;
}

namespaceDeclaration = containingTypeDeclaration.Parent as NamespaceDeclarationSyntax;
if (namespaceDeclaration is not null)
// Build up the final namespace by looping until we no longer have a namespace declaration
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
{
namespaceName = namespaceDeclaration.Name.ToString();
// We have a namespace. Use that as the type
nameSpace = namespaceParent.Name.ToString();

// Keep moving "out" of the namespace declarations until we
// run out of nested namespace declarations
while (true)
{
if (namespaceParent.Parent is not NamespaceDeclarationSyntax parent)
{
break;
}

// Add the outer namespace as a prefix to the final namespace
nameSpace = $"{namespaceParent.Name}.{nameSpace}";
namespaceParent = parent;
}
}

return namespaceName;
// return the final namespace
return nameSpace;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,46 @@ public static ExtractedTypeDefinition ExtractTypeDefinition(DefinitionExtraction
/// <returns>The namespace of the type for which generation is being performed</returns>
private static string GetNamespace(DefinitionExtractionContext extractionContext, TypeDeclarationSyntax targetTypeDeclaration)
{
string namespaceName = string.Empty;
NamespaceDeclarationSyntax namespaceDeclaration;
TypeDeclarationSyntax containingTypeDeclaration;

// Iterate through the containing types should the target type be nested inside other types
containingTypeDeclaration = targetTypeDeclaration;
while (containingTypeDeclaration.Parent is TypeDeclarationSyntax syntax)
// If we don't have a namespace at all we'll return an empty string
// This accounts for the "default namespace" case
string nameSpace = string.Empty;

// Get the containing syntax node for the type declaration
// (could be a nested type, for example)
SyntaxNode potentialNamespaceParent = targetTypeDeclaration.Parent;

// Keep moving "out" of nested classes etc until we get to a namespace
// or until we run out of parents
while (potentialNamespaceParent != null &&
potentialNamespaceParent is not NamespaceDeclarationSyntax
&& potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax)
{
containingTypeDeclaration = syntax;
potentialNamespaceParent = potentialNamespaceParent.Parent;
}

namespaceDeclaration = containingTypeDeclaration.Parent as NamespaceDeclarationSyntax;
if (namespaceDeclaration is not null)
// Build up the final namespace by looping until we no longer have a namespace declaration
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
{
namespaceName = namespaceDeclaration.Name.ToString();
// We have a namespace. Use that as the type
nameSpace = namespaceParent.Name.ToString();

// Keep moving "out" of the namespace declarations until we
// run out of nested namespace declarations
while (true)
{
if (namespaceParent.Parent is not NamespaceDeclarationSyntax parent)
{
break;
}

// Add the outer namespace as a prefix to the final namespace
nameSpace = $"{namespaceParent.Name}.{nameSpace}";
namespaceParent = parent;
}
}

return namespaceName;
// return the final namespace
return nameSpace;
}

/// <summary>
Expand Down

0 comments on commit f62a733

Please sign in to comment.