Skip to content

Add AttributeUsage. Fix UxmlElement issue. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.chebanovdd.unityuxmlgenerator",
"displayName": "Unity Uxml Generator",
"author": { "name": "ChebanovDD", "url": "https://github.com/ChebanovDD" },
"version": "0.0.3",
"version": "0.0.4",
"unity": "2018.4",
"description": "The Unity Uxml Generator allows you to generate 'UxmlFactory' and 'UxmlTraits' source code for a custom 'VisualElement'. Just mark elements with [UxmlElement] and [UxmlAttribute] attributes.",
"keywords": [ "uxml", "source", "generator" ]
Expand Down
4 changes: 2 additions & 2 deletions src/UnityUxmlGenerator/Captures/UxmlFactoryCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace UnityUxmlGenerator.Captures;

internal sealed class UxmlFactoryCapture : BaseCapture
{
public UxmlFactoryCapture(ClassDeclarationSyntax @class, AttributeSyntax attribute) : base(@class)
public UxmlFactoryCapture((ClassDeclarationSyntax Class, AttributeSyntax Attribute) data) : base(data.Class)
{
Attribute = attribute;
Attribute = data.Attribute;
}

public override string ClassTag => "UxmlFactory";
Expand Down
34 changes: 23 additions & 11 deletions src/UnityUxmlGenerator/Extensions/SyntaxNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,38 @@ namespace UnityUxmlGenerator.Extensions;

internal static class SyntaxNodeExtensions
{
public static bool IsAttributeWithName(this SyntaxNode syntaxNode, string attributeName,
out AttributeSyntax? attribute)
public static bool IsMemberHasAttribute<TMember>(this SyntaxNode syntaxNode, string attributeName,
out (TMember Member, AttributeSyntax Attribute) result) where TMember : MemberDeclarationSyntax
{
if (syntaxNode is not AttributeSyntax attributeSyntax)
if (syntaxNode is not TMember memberSyntax)
{
attribute = default;
result = default;
return false;
}

attribute = attributeSyntax;
result.Member = memberSyntax;

switch (attributeSyntax.Name)
for (var i = 0; i < memberSyntax.AttributeLists.Count; i++)
{
case IdentifierNameSyntax identifierNameSyntax
when identifierNameSyntax.Identifier.Text.Contains(attributeName):
case QualifiedNameSyntax qualifiedNameSyntax
when qualifiedNameSyntax.Right.Identifier.Text.Contains(attributeName):
return true;
var attributeList = memberSyntax.AttributeLists[i];
for (var j = 0; j < attributeList.Attributes.Count; j++)
{
var attributeSyntax = attributeList.Attributes[j];
switch (attributeSyntax.Name)
{
case IdentifierNameSyntax identifierNameSyntax
when identifierNameSyntax.Identifier.Text.Contains(attributeName):
case QualifiedNameSyntax qualifiedNameSyntax
when qualifiedNameSyntax.Right.Identifier.Text.Contains(attributeName):
{
result.Attribute = attributeSyntax;
return true;
}
}
}
}

result = default;
return false;
}

Expand Down
13 changes: 6 additions & 7 deletions src/UnityUxmlGenerator/SyntaxReceivers/UxmlFactoryReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@ internal sealed class UxmlFactoryReceiver : BaseReceiver

public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode.IsAttributeWithName(AttributeName, out var attribute) == false)
if (syntaxNode.IsMemberHasAttribute<ClassDeclarationSyntax>(AttributeName,
out (ClassDeclarationSyntax Class, AttributeSyntax Attribute) result) == false)
{
return;
}

var @class = attribute!.GetParent<ClassDeclarationSyntax>();

if (@class.InheritsFromAnyType())
if (result.Class.InheritsFromAnyType())
{
_captures.Add(new UxmlFactoryCapture(@class!, attribute!));
_captures.Add(new UxmlFactoryCapture(result));
}
else if (@class is not null)
else if (result.Class is not null)
{
RegisterDiagnostic(ClassHasNoBaseClassError, @class.GetLocation(), @class.Identifier.Text);
RegisterDiagnostic(ClassHasNoBaseClassError, result.Class.GetLocation(), result.Class.Identifier.Text);
}
}
}
13 changes: 5 additions & 8 deletions src/UnityUxmlGenerator/SyntaxReceivers/UxmlTraitsReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ internal sealed class UxmlTraitsReceiver : BaseReceiver

public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode.IsAttributeWithName(AttributeName, out var attribute) == false)
if (syntaxNode.IsMemberHasAttribute<PropertyDeclarationSyntax>(AttributeName,
out (PropertyDeclarationSyntax Property, AttributeSyntax Attribute) result) == false)
{
return;
}

var property = attribute!.GetParent<PropertyDeclarationSyntax>();
if (property is null)
{
return;
}
var (property, attribute) = result;

if (attribute!.ArgumentList is not null && attribute.ArgumentList.Arguments.Any())
if (attribute.ArgumentList is not null && attribute.ArgumentList.Arguments.Any())
{
if (HasSameType(property, attribute.ArgumentList.Arguments.First()) == false)
{
Expand Down Expand Up @@ -58,7 +55,7 @@ public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
_captures.Add(uxmlTraits.ClassName, uxmlTraits);
}

uxmlTraits.Properties.Add((property, attribute));
uxmlTraits.Properties.Add(result);
}

private static bool HasSameType(BasePropertyDeclarationSyntax property, AttributeArgumentSyntax attributeArgument)
Expand Down
23 changes: 20 additions & 3 deletions src/UnityUxmlGenerator/UxmlGenerator.Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ internal sealed partial class UxmlGenerator
{
private const string AttributeBaseType = "global::System.Attribute";

private const string AttributeClassTarget = "Class";
private const string AttributePropertyTarget = "Property";

private const string UxmlElementClassName = "UxmlElementAttribute";
private const string UxmlAttributeClassName = "UxmlAttributeAttribute";

private static SourceText GenerateUxmlElementAttribute()
{
return GenerateAttributeClass(UxmlElementClassName);
return GenerateAttributeClass(UxmlElementClassName, AttributeClassTarget);
}

private static SourceText GenerateUxmlAttributeAttribute()
{
return GenerateAttributeClass(UxmlAttributeClassName, GetUxmlAttributeMembers());
return GenerateAttributeClass(UxmlAttributeClassName, AttributePropertyTarget, GetUxmlAttributeMembers());
}

private static SourceText GenerateAttributeClass(string attributeClassIdentifier,
private static SourceText GenerateAttributeClass(string attributeClassIdentifier, string attributeTarget,
IEnumerable<MemberDeclarationSyntax>? members = null)
{
return CompilationUnitWidget(
Expand All @@ -31,6 +34,20 @@ private static SourceText GenerateAttributeClass(string attributeClassIdentifier
identifier: attributeClassIdentifier,
modifiers: new[] { SyntaxKind.InternalKeyword, SyntaxKind.SealedKeyword },
baseType: SimpleBaseType(IdentifierName(AttributeBaseType)),
attribute: AttributeWidget(
identifier: "global::System.AttributeUsage",
arguments: new[]
{
AttributeArgument(MemberAccessWidget(
identifier: "global::System.AttributeTargets",
memberName: attributeTarget)),
AttributeArgument(AssignmentWidget(
left: IdentifierName("AllowMultiple"),
right: LiteralExpression(SyntaxKind.FalseLiteralExpression))),
AttributeArgument(AssignmentWidget(
left: IdentifierName("Inherited"),
right: LiteralExpression(SyntaxKind.FalseLiteralExpression)))
}),
members: members,
addGeneratedCodeAttributes: true),
normalizeWhitespace: true)
Expand Down
Loading