GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - kant2002/SourceGeneratorsKit: Utility library for faster writing source generators

BranchesTags
Open more actions menu

Latest commit

 

History

3 Commits
3 Commits

Folders and files

NameName
Last commit message
Last commit date
.vscode
.vscode
 
 
SourceGeneratorsKit.ExampleApp
SourceGeneratorsKit.ExampleApp
 
 
SourceGeneratorsKit.ExampleGenerator
SourceGeneratorsKit.ExampleGenerator
 
 
SourceGeneratorsKit
SourceGeneratorsKit
 
 
.gitignore
.gitignore
 
 
Directory.Build.props
Directory.Build.props
 
 
README.md
README.md
 
 
sourcegeneratorskit.sln
sourcegeneratorskit.sln
 
 

Repository files navigation

Source Generators Toolkit

Nuget

Toolkit for writing source generators, and specifically collect important information about project to aid generation itself.

Getting started

[Generator]
public class MagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver = new MethodsWithAttributeReceiver("MagicAttribute");

    /// <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => syntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (IMethodSymbol methodSymbol in this.syntaxReceiver.Methods)
        {
            // process your method here.
        }
    }
}

Find all classes which implements specific interface

[Generator]
public class MagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver = new ClassesWithInterfacesReceiver("IEnumerable");

    /// <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => syntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver.Classes)
        {
            // process your class here.
        }
    }
}

Find all classes which derived from well-known type

[Generator]
public class MagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver = new DerivedClassesReceiver("DbConnection");

    /// <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => syntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver.Classes)
        {
            // process your class here.
        }
    }
}

Find all members with given attribute

[Generator]
public class MagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver = new MethodsWithAttributeReceiver("MagicAttribute");

    /// <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => syntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (IMethodSymbol methodSymbol in this.syntaxReceiver.Methods)
        {
            // process your method here.
            var parameters = methodSymbol.Parameters;
            foreach (var parameter in parameters)
            {
                // Do your parameter magic here.
            }
        }
    }
}

Work with multiple receivers

[Generator]
public class AggregateMagicGenerator : ISourceGenerator
{
    SyntaxReceiver syntaxReceiver1 = new MethodsWithAttributeReceiver("MagicAttribute");
    SyntaxReceiver syntaxReceiver2 = new DerivedClassesReceiver("DbConnection");
    SyntaxReceiver agregateSyntaxReceiver;

    public MagicGenerator()
    {
        this.agregateSyntaxReceiver = new AggregateSyntaxContextReceiver(this.syntaxReceiver1, this.syntaxReceiver2);
    }

    /// <inheritdoc/>
    public void Initialize(GeneratorInitializationContext context)
    {
        context.RegisterForSyntaxNotifications(() => agregateSyntaxReceiver);
    }

    public void Execute(GeneratorExecutionContext context)
    {
        // Retrieve the populated receiver
        if (!(context.SyntaxContextReceiver is SyntaxReceiver receiver))
        {
            return;
        }

        foreach (IMethodSymbol methodSymbol in this.syntaxReceiver1.Methods)
        {
            // process your method here.
            var parameters = methodSymbol.Parameters;
            foreach (var parameter in parameters)
            {
                // Do your parameter magic here.
            }
        }

        foreach (INamedTypeSymbol classSymbol in this.syntaxReceiver2.Classes)
        {
            // process your class here.
        }
    }
}

About

Utility library for faster writing source generators

Topics

source-generators

Resources

Readme
Activity

Stars

65 stars

Watchers

3 watching

Forks

7 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.