-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #674 from hadashiA/ku/refine-generator
Optimization of the search for the target of SourceGenerator
- Loading branch information
Showing
11 changed files
with
654 additions
and
771 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace VContainer.SourceGenerator | ||
namespace VContainer.SourceGenerator; | ||
|
||
class SyntaxCollector : ISyntaxReceiver | ||
{ | ||
class SyntaxCollector : ISyntaxReceiver | ||
{ | ||
public List<string> Log { get; } = new(); | ||
public List<WorkItem> WorkItems { get; } = new(); | ||
public List<string> Log { get; } = new(); | ||
public List<WorkItem> WorkItems { get; } = new(); | ||
|
||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
{ | ||
if (syntaxNode.IsKind(SyntaxKind.ClassDeclaration)) | ||
{ | ||
if (IsCandidateType(syntaxNode)) | ||
if (syntaxNode is ClassDeclarationSyntax classDeclarationSyntax) | ||
{ | ||
WorkItems.Add(new WorkItem((TypeDeclarationSyntax)syntaxNode)); | ||
if (!classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.AbstractKeyword) || | ||
modifier.IsKind(SyntaxKind.StaticKeyword))) | ||
{ | ||
WorkItems.Add(new WorkItem(classDeclarationSyntax)); | ||
} | ||
} | ||
} | ||
|
||
static bool IsCandidateType(SyntaxNode syntax) | ||
else if (syntaxNode.IsKind(SyntaxKind.InvocationExpression)) | ||
{ | ||
if (syntax is not ClassDeclarationSyntax classDeclarationSyntax) | ||
{ | ||
return false; | ||
} | ||
|
||
if (classDeclarationSyntax.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.AbstractKeyword) || | ||
modifier.IsKind(SyntaxKind.StaticKeyword))) | ||
if (syntaxNode is InvocationExpressionSyntax | ||
{ | ||
Expression: MemberAccessExpressionSyntax | ||
{ | ||
Expression: IdentifierNameSyntax | ||
} memberAccess | ||
} invocationExpressionSyntax) | ||
{ | ||
return false; | ||
if (memberAccess.Name.Identifier.Text.StartsWith("Register")) | ||
{ | ||
WorkItems.Add(new WorkItem(invocationExpressionSyntax)); | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
} | ||
} |
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.