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

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Microsoft.CodeAnalysis;
@@ -66,7 +65,7 @@ internal class {Helpers.UiElementAttribute} : Attribute
INamedTypeSymbol uiElementAttributeSymbol = context.Compilation.GetTypeByMetadataName( Helpers.UiElementAttribute );
foreach ( IGrouping<INamedTypeSymbol , ISymbol> group in receiver.Fields
.GroupBy<ISymbol , INamedTypeSymbol>( f => f.ContainingType
, SymbolEqualityComparer.Default ) )
, SymbolEqualityComparer.Default ) )
{
var classSource = ProcessClass( group.Key , group , uiElementAttributeSymbol );
if ( classSource == null )
@@ -85,7 +84,7 @@ internal class {Helpers.UiElementAttribute} : Attribute
return null;
List<ISymbol> elementFields = fieldsList.Where( f => GetUiElementAttributeData( f , uiElementAttributeSymbol ) != null ).ToList();
var source = new StringBuilder( $@"// <auto-generated/>
using UnityEngine.UIElements;
@@ -109,7 +108,6 @@ public partial class {classSymbol.Name}
}}
" );
return source.ToString();
}
@@ -188,31 +186,34 @@ public partial class {classSymbol.Name}
public void OnVisitSyntaxNode( GeneratorSyntaxContext context )
{
if ( context.Node is FieldDeclarationSyntax fieldDeclarationSyntax && fieldDeclarationSyntax.AttributeLists.Count > 0 )
switch ( context.Node )
{
foreach ( VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables )
{
ISymbol symbol = context.SemanticModel.GetDeclaredSymbol( variable ) as IFieldSymbol;
if ( Helpers.IsDerivedFrom( symbol?.ContainingType.BaseType , "AtVisualElement" )
&& symbol.GetAttributes()
.Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
case FieldDeclarationSyntax fieldDeclarationSyntax when fieldDeclarationSyntax.AttributeLists.Count > 0:
{
Fields.Add( symbol );
foreach ( VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables )
{
if ( context.SemanticModel.GetDeclaredSymbol( variable ) is IFieldSymbol symbol
&& Helpers.IsDerivedFrom( symbol.ContainingType.BaseType , "AtVisualElement" )
&& symbol.GetAttributes()
.Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
{
Fields.Add( symbol );
}
}
break;
}
}
}
if ( context.Node is PropertyDeclarationSyntax propertyDeclarationSyntax && propertyDeclarationSyntax.AttributeLists.Count > 0 )
{
ISymbol symbol = context.SemanticModel.GetDeclaredSymbol( propertyDeclarationSyntax ) as IPropertySymbol;
if ( Helpers.IsDerivedFrom( symbol?.ContainingType.BaseType , "AtVisualElement" )
&& symbol.GetAttributes()
.Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
{
Fields.Add( symbol );
}
case PropertyDeclarationSyntax propertyDeclarationSyntax when propertyDeclarationSyntax.AttributeLists.Count > 0:
{
if ( context.SemanticModel.GetDeclaredSymbol( propertyDeclarationSyntax ) is IPropertySymbol symbol
&& Helpers.IsDerivedFrom( symbol.ContainingType.BaseType , "AtVisualElement" )
&& symbol.GetAttributes()
.Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
{
Fields.Add( symbol );
}
break;
}
}
}