- add usings for returnValues and parameters

pull/62/head
Simon G. 3 days ago
parent 40e0260428
commit 07538852f2
Signed by: SimonG
GPG Key ID: 0B82B964BA536523
  1. 30
      LightweightIocContainer.FactoryGenerator/FactoryGenerator.cs

@ -169,6 +169,32 @@ public class FactoryGenerator : IIncrementalGenerator
stringBuilder.AppendLine(); stringBuilder.AppendLine();
stringBuilder.AppendLine("using LightweightIocContainer;"); stringBuilder.AppendLine("using LightweightIocContainer;");
ImmutableArray<ISymbol> members = typeSymbol.GetMembers();
List<string?> namespaces = [];
foreach (ISymbol? member in members)
{
if (member is not IMethodSymbol method)
continue;
if (!method.ReturnsVoid)
{
if (method.ReturnType.Name == "Task")
{
if (method.ReturnType is INamedTypeSymbol { IsGenericType: true } namedTypeSymbol)
namespaces.AddRange(namedTypeSymbol.TypeArguments.Select(GetNamespaceOfType));
}
else
namespaces.Add(GetNamespaceOfType(method.ReturnType));
}
namespaces.AddRange(method.Parameters.Select(p => GetNamespaceOfType(p.Type)));
}
foreach (string @namespace in namespaces.Distinct().OfType<string>().OrderBy(n => n))
stringBuilder.AppendLine($"using {@namespace};");
stringBuilder.AppendLine(); stringBuilder.AppendLine();
if (typeNamespace is not null) if (typeNamespace is not null)
@ -180,7 +206,6 @@ public class FactoryGenerator : IIncrementalGenerator
stringBuilder.AppendLine($"public class Generated{typeName}(IocContainer container) : {typeName}"); stringBuilder.AppendLine($"public class Generated{typeName}(IocContainer container) : {typeName}");
stringBuilder.AppendLine("{"); stringBuilder.AppendLine("{");
ImmutableArray<ISymbol> members = typeSymbol.GetMembers();
foreach (ISymbol? member in members) foreach (ISymbol? member in members)
{ {
if (member is not IMethodSymbol method) if (member is not IMethodSymbol method)
@ -268,9 +293,10 @@ public class FactoryGenerator : IIncrementalGenerator
return stringBuilder.ToString(); return stringBuilder.ToString();
} }
private string? GetNamespaceOfType(ITypeSymbol s) => s.ContainingNamespace.IsGlobalNamespace ? null : s.ContainingNamespace.ToString();
private IEnumerable<string> GetNamespacesOfTypes(ImmutableArray<ITypeSymbol?> types) => private IEnumerable<string> GetNamespacesOfTypes(ImmutableArray<ITypeSymbol?> types) =>
types.OfType<ITypeSymbol>() types.OfType<ITypeSymbol>()
.Select(s => s.ContainingNamespace.IsGlobalNamespace ? null : s.ContainingNamespace.ToString()) .Select(GetNamespaceOfType)
.OfType<string>() .OfType<string>()
.Distinct(); .Distinct();

Loading…
Cancel
Save