cleanup (not really semantic change)

This commit is contained in:
OMGeeky
2024-06-09 18:39:29 +02:00
parent 72c9f8feb1
commit f5b2c6c5f6
2 changed files with 52 additions and 49 deletions

View File

@@ -12,7 +12,7 @@ namespace ExampleGenerator.Unity.Components
[Generator] [Generator]
public class GetComponentGenerator : ISourceGenerator public class GetComponentGenerator : ISourceGenerator
{ {
private const string _attributeText = @" private const string AttributeText = @"
using System; using System;
[AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
@@ -33,7 +33,7 @@ internal class GetComponentAttribute : Attribute
public void Initialize( GeneratorInitializationContext context ) public void Initialize( GeneratorInitializationContext context )
{ {
context.RegisterForPostInitialization( i => i.AddSource( "GetComponentAttribute_g.cs" , _attributeText ) ); context.RegisterForPostInitialization( i => i.AddSource( "GetComponentAttribute_g.cs" , AttributeText ) );
context.RegisterForSyntaxNotifications( () => new SyntaxReceiver() ); context.RegisterForSyntaxNotifications( () => new SyntaxReceiver() );
} }
@@ -79,7 +79,7 @@ private void t()
AttributeData attributeData = fieldSymbol.GetAttributes() AttributeData attributeData = fieldSymbol.GetAttributes()
.Single( ad => .Single( ad =>
ad.AttributeClass.Equals( attributeSymbol , SymbolEqualityComparer.Default ) ); ad.AttributeClass?.Equals( attributeSymbol , SymbolEqualityComparer.Default ) ?? false );
var methodType = ProcessAttribute( attributeData ); var methodType = ProcessAttribute( attributeData );
@@ -89,14 +89,18 @@ private void t()
private string ProcessAttribute( AttributeData attributeData ) private string ProcessAttribute( AttributeData attributeData )
{ {
var stringBuilder = new StringBuilder( "GetComponent" ); var stringBuilder = new StringBuilder( "GetComponent" );
if ( attributeData.ConstructorArguments.Length > 0 var args = attributeData.ConstructorArguments;
&& int.TryParse( attributeData.ConstructorArguments[0].Value.ToString() , out var enumValue ) ) if ( args.Length > 0 && int.TryParse( args[0].Value?.ToString() , out var enumValue ) )
{ {
if ( enumValue == 1 ) switch ( enumValue )
{
case 1:
stringBuilder.Append( "InParent" ); stringBuilder.Append( "InParent" );
break;
if ( enumValue == 2 ) case 2:
stringBuilder.Append( "InChildren" ); stringBuilder.Append( "InChildren" );
break;
}
} }
return stringBuilder.ToString(); return stringBuilder.ToString();
@@ -109,24 +113,22 @@ private void t()
public void OnVisitSyntaxNode( GeneratorSyntaxContext context ) public void OnVisitSyntaxNode( GeneratorSyntaxContext context )
{ {
if ( context.Node is FieldDeclarationSyntax fieldDeclarationSyntax && fieldDeclarationSyntax.AttributeLists.Count > 0 ) if ( !(context.Node is FieldDeclarationSyntax fieldDeclarationSyntax) || fieldDeclarationSyntax.AttributeLists.Count <= 0 )
{ return;
foreach ( VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables ) foreach ( VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables )
{ {
IFieldSymbol fieldSymbol = context.SemanticModel.GetDeclaredSymbol( variable ) as IFieldSymbol; if ( context.SemanticModel.GetDeclaredSymbol( variable ) is IFieldSymbol fieldSymbol
&& IsDerivedFrom( fieldSymbol.ContainingType.BaseType , "MonoBehaviour" )
if ( IsDerivedFrom( fieldSymbol?.ContainingType.BaseType , "MonoBehaviour" ) && IsDerivedFrom( fieldSymbol.Type.BaseType , "Component" )
&& IsDerivedFrom( fieldSymbol?.Type.BaseType , "Component" ) && fieldSymbol.GetAttributes().Any( ad => ad.AttributeClass?.ToDisplayString() == "GetComponentAttribute" ) )
&& fieldSymbol.GetAttributes()
.Any( ad => ad.AttributeClass.ToDisplayString() == "GetComponentAttribute" ) )
{ {
Fields.Add( fieldSymbol ); Fields.Add( fieldSymbol );
} }
} }
} }
}
private bool IsDerivedFrom( INamedTypeSymbol baseType , string targetType ) private static bool IsDerivedFrom( INamedTypeSymbol baseType , string targetType )
{ {
while ( baseType != null ) while ( baseType != null )
{ {

View File

@@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
@@ -109,7 +108,6 @@ public partial class {classSymbol.Name}
}} }}
" ); " );
return source.ToString(); return source.ToString();
} }
@@ -188,31 +186,34 @@ public partial class {classSymbol.Name}
public void OnVisitSyntaxNode( GeneratorSyntaxContext context ) public void OnVisitSyntaxNode( GeneratorSyntaxContext context )
{ {
if ( context.Node is FieldDeclarationSyntax fieldDeclarationSyntax && fieldDeclarationSyntax.AttributeLists.Count > 0 ) switch ( context.Node )
{
case FieldDeclarationSyntax fieldDeclarationSyntax when fieldDeclarationSyntax.AttributeLists.Count > 0:
{ {
foreach ( VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables ) foreach ( VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables )
{ {
ISymbol symbol = context.SemanticModel.GetDeclaredSymbol( variable ) as IFieldSymbol; if ( context.SemanticModel.GetDeclaredSymbol( variable ) is IFieldSymbol symbol
&& Helpers.IsDerivedFrom( symbol.ContainingType.BaseType , "AtVisualElement" )
if ( Helpers.IsDerivedFrom( symbol?.ContainingType.BaseType , "AtVisualElement" )
&& symbol.GetAttributes() && symbol.GetAttributes()
.Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) ) .Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
{ {
Fields.Add( symbol ); Fields.Add( symbol );
} }
} }
break;
} }
if ( context.Node is PropertyDeclarationSyntax propertyDeclarationSyntax && propertyDeclarationSyntax.AttributeLists.Count > 0 ) case PropertyDeclarationSyntax propertyDeclarationSyntax when propertyDeclarationSyntax.AttributeLists.Count > 0:
{ {
ISymbol symbol = context.SemanticModel.GetDeclaredSymbol( propertyDeclarationSyntax ) as IPropertySymbol; if ( context.SemanticModel.GetDeclaredSymbol( propertyDeclarationSyntax ) is IPropertySymbol symbol
&& Helpers.IsDerivedFrom( symbol.ContainingType.BaseType , "AtVisualElement" )
if ( Helpers.IsDerivedFrom( symbol?.ContainingType.BaseType , "AtVisualElement" )
&& symbol.GetAttributes() && symbol.GetAttributes()
.Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) ) .Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
{ {
Fields.Add( symbol ); Fields.Add( symbol );
} }
break;
}
} }
} }