Skip to content

Commit e2791f1

Browse files
committed
Optimize syntax receivers.
1 parent e7cafe1 commit e2791f1

File tree

6 files changed

+34
-36
lines changed

6 files changed

+34
-36
lines changed

UnityUxmlGenerator.sln.DotSettings

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Uxml/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/UnityUxmlGenerator/Captures/UxmlFactoryCapture.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace UnityUxmlGenerator.Captures;
44

55
internal sealed class UxmlFactoryCapture : BaseCapture
66
{
7-
public UxmlFactoryCapture((ClassDeclarationSyntax Class, AttributeSyntax Attribute) data) : base(data.Class)
7+
public UxmlFactoryCapture(ClassDeclarationSyntax @class, AttributeSyntax attribute) : base(@class)
88
{
9-
Attribute = data.Attribute;
9+
Attribute = attribute;
1010
}
1111

1212
public override string ClassTag => "UxmlFactory";

src/UnityUxmlGenerator/Extensions/SyntaxNodeExtensions.cs

+11-23
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,26 @@ namespace UnityUxmlGenerator.Extensions;
55

66
internal static class SyntaxNodeExtensions
77
{
8-
public static bool IsMemberHasAttribute<TMember>(this SyntaxNode syntaxNode, string attributeName,
9-
out (TMember Member, AttributeSyntax Attribute) result) where TMember : MemberDeclarationSyntax
8+
public static bool IsAttributeWithName(this SyntaxNode syntaxNode, string attributeName,
9+
out AttributeSyntax? attribute)
1010
{
11-
if (syntaxNode is not TMember memberSyntax)
11+
if (syntaxNode is not AttributeSyntax attributeSyntax)
1212
{
13-
result = default;
13+
attribute = default;
1414
return false;
1515
}
1616

17-
result.Member = memberSyntax;
17+
attribute = attributeSyntax;
1818

19-
for (var i = 0; i < memberSyntax.AttributeLists.Count; i++)
19+
switch (attributeSyntax.Name)
2020
{
21-
var attributeList = memberSyntax.AttributeLists[i];
22-
for (var j = 0; j < attributeList.Attributes.Count; j++)
23-
{
24-
var attributeSyntax = attributeList.Attributes[j];
25-
switch (attributeSyntax.Name)
26-
{
27-
case IdentifierNameSyntax identifierNameSyntax
28-
when identifierNameSyntax.Identifier.Text.Contains(attributeName):
29-
case QualifiedNameSyntax qualifiedNameSyntax
30-
when qualifiedNameSyntax.Right.Identifier.Text.Contains(attributeName):
31-
{
32-
result.Attribute = attributeSyntax;
33-
return true;
34-
}
35-
}
36-
}
21+
case IdentifierNameSyntax identifierNameSyntax
22+
when identifierNameSyntax.Identifier.Text.Contains(attributeName):
23+
case QualifiedNameSyntax qualifiedNameSyntax
24+
when qualifiedNameSyntax.Right.Identifier.Text.Contains(attributeName):
25+
return true;
3726
}
3827

39-
result = default;
4028
return false;
4129
}
4230

src/UnityUxmlGenerator/SyntaxReceivers/UxmlFactoryReceiver.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ internal sealed class UxmlFactoryReceiver : BaseReceiver
1515

1616
public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
1717
{
18-
if (syntaxNode.IsMemberHasAttribute<ClassDeclarationSyntax>(AttributeName,
19-
out (ClassDeclarationSyntax Class, AttributeSyntax Attribute) result) == false)
18+
if (syntaxNode.IsAttributeWithName(AttributeName, out var attribute) == false)
2019
{
2120
return;
2221
}
2322

24-
if (result.Class.InheritsFromAnyType())
23+
var member = attribute!.GetParent<MemberDeclarationSyntax>();
24+
if (member is not ClassDeclarationSyntax @class)
2525
{
26-
_captures.Add(new UxmlFactoryCapture(result));
26+
return;
27+
}
28+
29+
if (@class.InheritsFromAnyType())
30+
{
31+
_captures.Add(new UxmlFactoryCapture(@class, attribute!));
2732
}
28-
else if (result.Class is not null)
33+
else
2934
{
30-
RegisterDiagnostic(ClassHasNoBaseClassError, result.Class.GetLocation(), result.Class.Identifier.Text);
35+
RegisterDiagnostic(ClassHasNoBaseClassError, @class.GetLocation(), @class.Identifier.Text);
3136
}
3237
}
3338
}

src/UnityUxmlGenerator/SyntaxReceivers/UxmlTraitsReceiver.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ internal sealed class UxmlTraitsReceiver : BaseReceiver
1616

1717
public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
1818
{
19-
if (syntaxNode.IsMemberHasAttribute<PropertyDeclarationSyntax>(AttributeName,
20-
out (PropertyDeclarationSyntax Property, AttributeSyntax Attribute) result) == false)
19+
if (syntaxNode.IsAttributeWithName(AttributeName, out var attribute) == false)
2120
{
2221
return;
2322
}
2423

25-
var (property, attribute) = result;
24+
var member = attribute!.GetParent<MemberDeclarationSyntax>();
25+
if (member is not PropertyDeclarationSyntax property)
26+
{
27+
return;
28+
}
2629

27-
if (attribute.ArgumentList is not null && attribute.ArgumentList.Arguments.Any())
30+
if (attribute!.ArgumentList is not null && attribute.ArgumentList.Arguments.Any())
2831
{
2932
if (HasSameType(property, attribute.ArgumentList.Arguments.First()) == false)
3033
{
@@ -55,7 +58,7 @@ public override void OnVisitSyntaxNode(SyntaxNode syntaxNode)
5558
_captures.Add(uxmlTraits.ClassName, uxmlTraits);
5659
}
5760

58-
uxmlTraits.Properties.Add(result);
61+
uxmlTraits.Properties.Add((property, attribute));
5962
}
6063

6164
private static bool HasSameType(BasePropertyDeclarationSyntax property, AttributeArgumentSyntax attributeArgument)

0 commit comments

Comments
 (0)