Skip to content

Commit

Permalink
Use ForAttributeWithMetadataName to improve performance and streamlin…
Browse files Browse the repository at this point in the history
…e code.
  • Loading branch information
Colin Wilmans committed Aug 5, 2024
1 parent 8e3b32f commit 61bcacb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<Version>3.0.0</Version>
<Version>2.5.0</Version>
<Version>3.0.1</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
<PackageReleaseNotes>Use ForAttributeWithMetadataName to improve performance</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugGenerator|AnyCPU'">
Expand All @@ -42,8 +42,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Immutable;
using System.Text;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace AutomaticInterface;

Expand All @@ -19,41 +16,20 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
context.RegisterIgnoreAttribute();

var classes = context
.SyntaxProvider.CreateSyntaxProvider(CouldBeClassWithInterfaceAttribute, Transform)
.SyntaxProvider.ForAttributeWithMetadataName(
$"AutomaticInterface.{DefaultAttributeName}Attribute",
(_, _) => true,
(context, _) => (ITypeSymbol)context.TargetSymbol
)
.Where(type => type is not null)
.Collect();

context.RegisterSourceOutput(classes, GenerateCode);
}

private static bool CouldBeClassWithInterfaceAttribute(
SyntaxNode syntaxNode,
CancellationToken _
)
{
if (syntaxNode is not AttributeSyntax attribute)
{
return false;
}

var name = ExtractName(attribute.Name);

return name is DefaultAttributeName;
}

private static string? ExtractName(NameSyntax? name)
{
return name switch
{
SimpleNameSyntax ins => ins.Identifier.Text,
QualifiedNameSyntax qns => qns.Right.Identifier.Text,
_ => null
};
}

private static void GenerateCode(
SourceProductionContext context,
ImmutableArray<ITypeSymbol?> enumerations
ImmutableArray<ITypeSymbol> enumerations
)
{
if (enumerations.IsDefaultOrEmpty)
Expand All @@ -63,13 +39,8 @@ private static void GenerateCode(

foreach (var type in enumerations)
{
if (type is null)
{
continue;
}

var typeNamespace = type.ContainingNamespace.IsGlobalNamespace
? $"${Guid.NewGuid().ToString()}"
? $"${Guid.NewGuid()}"
: $"{type.ContainingNamespace}";

var code = Builder.BuildInterfaceFor(type);
Expand All @@ -78,23 +49,4 @@ private static void GenerateCode(
context.AddSource(hintName, code);
}
}

private static ITypeSymbol? Transform(
GeneratorSyntaxContext context,
CancellationToken cancellationToken
)
{
var attributeSyntax = (AttributeSyntax)context.Node;
if (attributeSyntax.Parent?.Parent is not ClassDeclarationSyntax classDeclaration)
{
return null;
}

var type =
context.SemanticModel.GetDeclaredSymbol(
classDeclaration,
cancellationToken: cancellationToken
) as ITypeSymbol;
return type;
}
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ Should be simply a build and run Tests

## Changelog

### 3.0.1

- Maintenance update. Use of `ForAttributeWithMetadataName` to improve performance. Thanks crwsolutions!

### 3.0.0

- You can remove the manually created `GenerateAutomaticInterfaceAttribute`, as it is generated automatically now. Thanks crwsolutions!
Expand Down

0 comments on commit 61bcacb

Please sign in to comment.