Skip to content

Commit

Permalink
Merge pull request #674 from hadashiA/ku/refine-generator
Browse files Browse the repository at this point in the history
Optimization of the search for the target of SourceGenerator
  • Loading branch information
hadashiA authored Jun 16, 2024
2 parents 1f611aa + 5b52278 commit 85d48af
Show file tree
Hide file tree
Showing 11 changed files with 654 additions and 771 deletions.
48 changes: 27 additions & 21 deletions VContainer.SourceGenerator.Roslyn3/SyntaxCollector.cs
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
<DevelopmentDependency>true</DevelopmentDependency>
<IsRoslynComponent>true</IsRoslynComponent>
<DefineConstants>ROSLYN3</DefineConstants>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 85d48af

Please sign in to comment.