-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up a new method for generating source files for named types.
- Loading branch information
1 parent
e18ba6f
commit 87048de
Showing
4 changed files
with
56 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
|
||
namespace schema.util.generators { | ||
internal abstract class BNamedTypeGenerator : ISourceGenerator { | ||
private readonly SourceFileDictionary sourceFileDictionary_ = new(); | ||
|
||
internal abstract bool Generate( | ||
TypeDeclarationSyntax syntax, | ||
INamedTypeSymbol typeSymbol, | ||
ISourceFileDictionary sourceFileDictionary); | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
=> context.RegisterForSyntaxNotifications(() => new CustomReceiver(this)); | ||
|
||
private class CustomReceiver : ISyntaxContextReceiver { | ||
private readonly BNamedTypeGenerator g_; | ||
|
||
public CustomReceiver(BNamedTypeGenerator g) { | ||
this.g_ = g; | ||
} | ||
|
||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context) { | ||
TypeDeclarationSyntax syntax; | ||
ISymbol symbol; | ||
if (context.Node is TypeDeclarationSyntax classDeclarationSyntax) { | ||
syntax = classDeclarationSyntax; | ||
} else if (context.Node is StructDeclarationSyntax | ||
structDeclarationSyntax) { | ||
syntax = structDeclarationSyntax; | ||
} else { | ||
return; | ||
} | ||
|
||
symbol = context.SemanticModel.GetDeclaredSymbol(syntax); | ||
if (symbol is not INamedTypeSymbol namedTypeSymbol) { | ||
return; | ||
} | ||
|
||
this.g_.Generate(syntax, | ||
namedTypeSymbol, | ||
this.g_.sourceFileDictionary_); | ||
} | ||
} | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
=> this.sourceFileDictionary_.SetHandler(context.AddSource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters