- tweak: Some view change

This commit is contained in:
2025-01-15 19:47:27 +03:00
parent 08e518602b
commit 5d7f443532
10 changed files with 143 additions and 64 deletions

View File

@@ -11,18 +11,15 @@ namespace Nebula.SourceGenerators;
[Generator]
public class DependencyAutoGenerator : IIncrementalGenerator
{
private static readonly string ConstructGeneratorAttributeName = "ConstructGeneratorAttribute";
private static readonly string GeneratePropertyAttributeName = "GeneratePropertyAttribute";
private static readonly string ConstructGeneratorAttributeName = "Nebula.Shared.Attributes.ConstructGeneratorAttribute";
private static readonly string GeneratePropertyAttributeName = "Nebula.Shared.Attributes.GeneratePropertyAttribute";
public void Initialize(IncrementalGeneratorInitializationContext context)
{
GenerateAttribute(ConstructGeneratorAttributeName, "Class", context);
GenerateAttribute(GeneratePropertyAttributeName, "Property", context);
var provider = context.SyntaxProvider
.CreateSyntaxProvider(
(s, _) => s is ClassDeclarationSyntax,
(ctx, _) => GetClassDeclarationForSourceGen(ctx))
(ctx, _) => SourceHelper.GetClassDeclarationForSourceGen(ctx,ConstructGeneratorAttributeName))
.Where(t => t.reportAttributeFound)
.Select((t, _) => t.Item1);
@@ -30,44 +27,6 @@ public class DependencyAutoGenerator : IIncrementalGenerator
(ctx, t) => GenerateCode(ctx, t.Left, t.Right));
}
private static void GenerateAttribute(string attributeName, string usage,
IncrementalGeneratorInitializationContext context)
{
var attributeSourceCode = $@"// <auto-generated/>
namespace SourceGen
{{
[System.AttributeUsage(System.AttributeTargets.{usage})]
public class {attributeName} : System.Attribute
{{
}}
}}";
context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
$"{attributeName}.g.cs",
SourceText.From(attributeSourceCode, Encoding.UTF8)));
}
private static (ClassDeclarationSyntax, bool reportAttributeFound) GetClassDeclarationForSourceGen(
GeneratorSyntaxContext context)
{
var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node;
foreach (var attributeListSyntax in classDeclarationSyntax.AttributeLists)
foreach (var attributeSyntax in attributeListSyntax.Attributes)
{
if (context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol is not IMethodSymbol attributeSymbol)
continue;
var attributeName = attributeSymbol.ContainingType.ToDisplayString();
if (attributeName == $"SourceGen.{ConstructGeneratorAttributeName}")
return (classDeclarationSyntax, true);
}
return (classDeclarationSyntax, false);
}
private void GenerateCode(SourceProductionContext context, Compilation compilation,
ImmutableArray<ClassDeclarationSyntax> classDeclarations)
{
@@ -118,15 +77,6 @@ partial class {className}
private static IEnumerable<IPropertySymbol> GetProperties(INamedTypeSymbol classSymbol)
{
return classSymbol.GetMembers().OfType<IPropertySymbol>().Where(a =>
HasAttribute(a, $"SourceGen.{GeneratePropertyAttributeName}"));
}
private static bool HasAttribute(ISymbol type, string attributeName)
{
foreach (var attribute in type.GetAttributes())
if (attribute.AttributeClass?.ToDisplayString() == attributeName)
return true;
return false;
SourceHelper.HasAttribute(a, GeneratePropertyAttributeName));
}
}