Skip to content

Commit

Permalink
Use FirstAncestorOrSelf
Browse files Browse the repository at this point in the history
  • Loading branch information
PieterjanDeClippel committed Jan 14, 2025
1 parent 81815b3 commit 93a4997
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,18 @@ public override void Initialize(IncrementalGeneratorInitializationContext contex
{
if (fieldDeclaration.Parent is ClassDeclarationSyntax classDeclaration)
{
var namespaceDeclaration = classDeclaration.FirstAncestorOrSelf<BaseNamespaceDeclarationSyntax>();
var classSymbol = context2.SemanticModel.GetDeclaredSymbol(classDeclaration);
switch (classDeclaration.Parent)

return new Models.FieldDeclaration
{
case NamespaceDeclarationSyntax namespaceDeclaration:
return new Models.FieldDeclaration
{
Namespace = namespaceDeclaration.Name.ToString(),
FullyQualifiedClassName = classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
ClassName = classSymbol.Name,
Name = symbol.Name,
FullyQualifiedTypeName = symbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
Type = symbol.Type.Name,
};
case FileScopedNamespaceDeclarationSyntax fileScopedNamespaceDeclaration:
return new Models.FieldDeclaration
{
Namespace = fileScopedNamespaceDeclaration.Name.ToString(),
FullyQualifiedClassName = classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
ClassName = classSymbol.Name,
Name = symbol.Name,
FullyQualifiedTypeName = symbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
Type = symbol.Type.Name,
};
default:
return new Models.FieldDeclaration
{
Namespace = null,
FullyQualifiedClassName = classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
ClassName = classSymbol.Name,
Name = symbol.Name,
FullyQualifiedTypeName = symbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
Type = symbol.Type.Name,
};
}
Namespace = namespaceDeclaration.Name.ToString(),
FullyQualifiedClassName = classSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
ClassName = classSymbol.Name,
Name = symbol.Name,
FullyQualifiedTypeName = symbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
Type = symbol.Type.Name,
};

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public override void Initialize(IncrementalGeneratorInitializationContext contex
currentTypeSymbol = currentTypeSymbol.BaseType;
}

var namespaceDeclaration = classDeclaration.FirstAncestorOrSelf<BaseNamespaceDeclarationSyntax>();

return new Models.ClassWithBaseDependenciesAndInjectFields
{
FileName = classDeclaration.Identifier.Text,
ClassName = className,
ClassNamespace = GetNamespace(classDeclaration),
ClassNamespace = namespaceDeclaration?.Name?.ToString(),
BaseDependencies = baseDependencies,
InjectFields = injectFields,
};
Expand Down Expand Up @@ -87,18 +89,5 @@ public override void Initialize(IncrementalGeneratorInitializationContext contex
})
.ToList();
}

private static string? GetNamespace(ClassDeclarationSyntax classDeclaration)
{
switch (classDeclaration.Parent)
{
case NamespaceDeclarationSyntax nsSyntax:
return nsSyntax.Name.ToString();
case FileScopedNamespaceDeclarationSyntax fsnsSyntax:
return fsnsSyntax.Name.ToString();
default:
return null;
}
}
}
}

0 comments on commit 93a4997

Please sign in to comment.