-
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.
feat: Implement IIncrementalGenerator instead of ISourceGenerator
Resolve #52
- Loading branch information
1 parent
44ca619
commit e704aa1
Showing
15 changed files
with
188 additions
and
89 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 |
---|---|---|
@@ -1,22 +1,102 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using StackMemoryCollections.Helpers; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
|
||
namespace StackMemoryCollections | ||
{ | ||
[Generator] | ||
public partial class Generator : ISourceGenerator | ||
public partial class Generator : IIncrementalGenerator | ||
{ | ||
public void Execute(GeneratorExecutionContext context) | ||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
var c = (CSharpCompilation)context.Compilation; | ||
var processor = new AttributeProcessor(); | ||
processor.FillAllTypes(c.Assembly.GlobalNamespace); | ||
processor.Generate(context); | ||
//System.Diagnostics.Debugger.Launch(); | ||
var classDeclarations = context.SyntaxProvider | ||
.CreateSyntaxProvider( | ||
predicate: (s, _) => IsSyntaxTargetForGeneration(s), | ||
transform: (ctx, _) => GetSemanticTargetForGeneration(ctx)) | ||
.Where(m => m != null); | ||
|
||
var compilationAndClasses = context.CompilationProvider.Combine(classDeclarations.Collect()); | ||
|
||
context.RegisterSourceOutput(compilationAndClasses, | ||
(spc, source) => Execute(source.Item1, source.Item2, spc)); | ||
} | ||
|
||
static bool IsSyntaxTargetForGeneration(SyntaxNode node) | ||
{ | ||
if(!(node is ClassDeclarationSyntax) && !(node is StructDeclarationSyntax)) | ||
{ | ||
return false; | ||
} | ||
|
||
var typeDeclar = (TypeDeclarationSyntax)node; | ||
if (typeDeclar.Modifiers.Any(wh => wh.IsKind(SyntaxKind.StaticKeyword))) | ||
{ | ||
return false; | ||
} | ||
|
||
if (typeDeclar.TypeParameterList?.Parameters.Any() == true) | ||
{ | ||
return false; | ||
} | ||
|
||
if (typeDeclar.Modifiers.Any(wh => wh.IsKind(SyntaxKind.AbstractKeyword))) | ||
{ | ||
return false; | ||
} | ||
|
||
if (typeDeclar.AttributeLists.Count == 0) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
static TypeDeclarationSyntax GetSemanticTargetForGeneration(GeneratorSyntaxContext context) | ||
{ | ||
// No initialization required for this one | ||
return GetSemanticClassOrStruct(context); | ||
} | ||
|
||
static TypeDeclarationSyntax GetSemanticClassOrStruct(GeneratorSyntaxContext context) | ||
{ | ||
if (!(context.Node is ClassDeclarationSyntax) && !(context.Node is StructDeclarationSyntax)) | ||
{ | ||
return null; | ||
} | ||
|
||
var typeDeclarationSyntax = (TypeDeclarationSyntax)context.Node; | ||
foreach (var attributeListSyntax in typeDeclarationSyntax.AttributeLists) | ||
{ | ||
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes) | ||
{ | ||
IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol as IMethodSymbol; | ||
if (attributeSymbol == null) | ||
{ | ||
continue; | ||
} | ||
|
||
INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType; | ||
|
||
if (attributeContainingTypeSymbol.ContainingNamespace.GetFullNamespace().StartsWith("StackMemoryCollections.")) | ||
{ | ||
return typeDeclarationSyntax; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private void Execute(Compilation compilation, ImmutableArray<TypeDeclarationSyntax> types, SourceProductionContext context) | ||
{ | ||
//System.Diagnostics.Debugger.Launch(); | ||
var processor = new AttributeProcessor(); | ||
processor.FillAllTypes(compilation, types); | ||
processor.Generate(context); | ||
} | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.