using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Text; namespace MfGames.Nitride.Generators; /// /// Base class for classes marked with an attribute. /// public abstract class ClassAttributeSourceGeneratorBase : ISourceGenerator where TSyntaxReceiver : ClassAttributeSyntaxReceiverBase { public void Execute(GeneratorExecutionContext context) { // Get the generator infrastructure will create a receiver and // populate it we can retrieve the populated instance via the // context. if (context.SyntaxReceiver is not TSyntaxReceiver syntaxReceiver) { return; } // Report any messages. foreach (string? message in syntaxReceiver.Messages) { context.Warning( MessageCode.Debug, Location.Create( "Temporary.g.cs", TextSpan.FromBounds(0, 0), new LinePositionSpan( new LinePosition(0, 0), new LinePosition(0, 0))), "{0}: Syntax Message: {1}", this.GetType().Name, message); } // If we didn't find anything, then there is nothing to do. if (syntaxReceiver.ReferenceList.Count == 0) { return; } // Go through each one. foreach (ClassAttributeReference reference in syntaxReceiver .ReferenceList) { this.GenerateClassFile(context, reference); } } public void Initialize(GeneratorInitializationContext context) { // Register a factory that can create our custom syntax receiver context.RegisterForSyntaxNotifications( () => this.CreateSyntaxReceiver(context)); } protected abstract TSyntaxReceiver CreateSyntaxReceiver( GeneratorInitializationContext context); protected abstract void GenerateClassFile( GeneratorExecutionContext context, ClassAttributeReference reference); }