mirror of
https://github.com/OMGeeky/UnityCodeGenerators.git
synced 2025-12-28 15:28:09 +01:00
cleanup (not really semantic change)
This commit is contained in:
@@ -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() );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ internal class GetComponentAttribute : Attribute
|
|||||||
|
|
||||||
foreach ( IGrouping<INamedTypeSymbol , IFieldSymbol> group in receiver.Fields
|
foreach ( IGrouping<INamedTypeSymbol , IFieldSymbol> group in receiver.Fields
|
||||||
.GroupBy<IFieldSymbol , INamedTypeSymbol>( f => f.ContainingType
|
.GroupBy<IFieldSymbol , INamedTypeSymbol>( f => f.ContainingType
|
||||||
, SymbolEqualityComparer.Default ) )
|
, SymbolEqualityComparer.Default ) )
|
||||||
{
|
{
|
||||||
var classSource = ProcessClass( group.Key , group , attributeSymbol );
|
var classSource = ProcessClass( group.Key , group , attributeSymbol );
|
||||||
context.AddSource( $"{group.Key.Name}_Components_g.cs" , SourceText.From( classSource , Encoding.UTF8 ) );
|
context.AddSource( $"{group.Key.Name}_Components_g.cs" , SourceText.From( classSource , Encoding.UTF8 ) );
|
||||||
@@ -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 )
|
||||||
stringBuilder.Append( "InParent" );
|
{
|
||||||
|
case 1:
|
||||||
if ( enumValue == 2 )
|
stringBuilder.Append( "InParent" );
|
||||||
stringBuilder.Append( "InChildren" );
|
break;
|
||||||
|
case 2:
|
||||||
|
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 )
|
|
||||||
{
|
|
||||||
IFieldSymbol fieldSymbol = context.SemanticModel.GetDeclaredSymbol( variable ) as IFieldSymbol;
|
|
||||||
|
|
||||||
if ( IsDerivedFrom( fieldSymbol?.ContainingType.BaseType , "MonoBehaviour" )
|
foreach ( VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables )
|
||||||
&& IsDerivedFrom( fieldSymbol?.Type.BaseType , "Component" )
|
{
|
||||||
&& fieldSymbol.GetAttributes()
|
if ( context.SemanticModel.GetDeclaredSymbol( variable ) is IFieldSymbol fieldSymbol
|
||||||
.Any( ad => ad.AttributeClass.ToDisplayString() == "GetComponentAttribute" ) )
|
&& IsDerivedFrom( fieldSymbol.ContainingType.BaseType , "MonoBehaviour" )
|
||||||
{
|
&& IsDerivedFrom( fieldSymbol.Type.BaseType , "Component" )
|
||||||
Fields.Add( fieldSymbol );
|
&& 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 )
|
while ( baseType != null )
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -66,7 +65,7 @@ internal class {Helpers.UiElementAttribute} : Attribute
|
|||||||
INamedTypeSymbol uiElementAttributeSymbol = context.Compilation.GetTypeByMetadataName( Helpers.UiElementAttribute );
|
INamedTypeSymbol uiElementAttributeSymbol = context.Compilation.GetTypeByMetadataName( Helpers.UiElementAttribute );
|
||||||
foreach ( IGrouping<INamedTypeSymbol , ISymbol> group in receiver.Fields
|
foreach ( IGrouping<INamedTypeSymbol , ISymbol> group in receiver.Fields
|
||||||
.GroupBy<ISymbol , INamedTypeSymbol>( f => f.ContainingType
|
.GroupBy<ISymbol , INamedTypeSymbol>( f => f.ContainingType
|
||||||
, SymbolEqualityComparer.Default ) )
|
, SymbolEqualityComparer.Default ) )
|
||||||
{
|
{
|
||||||
var classSource = ProcessClass( group.Key , group , uiElementAttributeSymbol );
|
var classSource = ProcessClass( group.Key , group , uiElementAttributeSymbol );
|
||||||
if ( classSource == null )
|
if ( classSource == null )
|
||||||
@@ -85,7 +84,7 @@ internal class {Helpers.UiElementAttribute} : Attribute
|
|||||||
return null;
|
return null;
|
||||||
|
|
||||||
List<ISymbol> elementFields = fieldsList.Where( f => GetUiElementAttributeData( f , uiElementAttributeSymbol ) != null ).ToList();
|
List<ISymbol> elementFields = fieldsList.Where( f => GetUiElementAttributeData( f , uiElementAttributeSymbol ) != null ).ToList();
|
||||||
|
|
||||||
var source = new StringBuilder( $@"// <auto-generated/>
|
var source = new StringBuilder( $@"// <auto-generated/>
|
||||||
|
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
@@ -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 )
|
||||||
{
|
{
|
||||||
foreach ( VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables )
|
case FieldDeclarationSyntax fieldDeclarationSyntax when fieldDeclarationSyntax.AttributeLists.Count > 0:
|
||||||
{
|
|
||||||
ISymbol symbol = context.SemanticModel.GetDeclaredSymbol( variable ) as IFieldSymbol;
|
|
||||||
|
|
||||||
if ( Helpers.IsDerivedFrom( symbol?.ContainingType.BaseType , "AtVisualElement" )
|
|
||||||
&& symbol.GetAttributes()
|
|
||||||
.Any( ad => ad.AttributeClass?.ToDisplayString() == Helpers.UiElementAttribute ) )
|
|
||||||
{
|
{
|
||||||
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 )
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user