Skip to content

Commit e48226c

Browse files
committed
Improve formatting for known enum members
1 parent 7d65ea1 commit e48226c

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Extensions/ITypeSymbolExtensions.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static bool TryGetDefaultValueForEnumType(this ITypeSymbol symbol, [NotNu
3434
{
3535
if (symbol.TypeKind is not TypeKind.Enum)
3636
{
37-
value = default;
37+
value = null;
3838

3939
return false;
4040
}
@@ -50,7 +50,43 @@ public static bool TryGetDefaultValueForEnumType(this ITypeSymbol symbol, [NotNu
5050
}
5151
}
5252

53-
value = default;
53+
value = null;
54+
55+
return false;
56+
}
57+
58+
/// <summary>
59+
/// Tries to get the name of the enum field matching a given value.
60+
/// </summary>
61+
/// <param name="symbol">The input <see cref="ITypeSymbol"/> instance to check.</param>
62+
/// <param name="value">The value for to try to get the field for.</param>
63+
/// <param name="fieldName">The name of the field with the specified value, if found.</param>
64+
/// <returns>Whether <paramref name="fieldName"/> was successfully retrieved.</returns>
65+
public static bool TryGetEnumFieldName(this ITypeSymbol symbol, object value, [NotNullWhen(true)] out string? fieldName)
66+
{
67+
if (symbol.TypeKind is not TypeKind.Enum)
68+
{
69+
fieldName = null;
70+
71+
return false;
72+
}
73+
74+
// The default value of the enum is the value of its first constant field
75+
foreach (ISymbol memberSymbol in symbol.GetMembers())
76+
{
77+
if (memberSymbol is not IFieldSymbol { IsConst: true, ConstantValue: object fieldValue } fieldSymbol)
78+
{
79+
continue;
80+
}
81+
82+
if (fieldValue == value)
83+
{
84+
fieldName = fieldSymbol.Name;
85+
86+
return true;
87+
}
88+
}
89+
fieldName = null;
5490

5591
return false;
5692
}

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Models/TypedConstantInfo.Factory.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Immutable;
77
using System.Diagnostics.CodeAnalysis;
88
using System.Linq;
9+
using CommunityToolkit.GeneratedDependencyProperty.Extensions;
910
using Microsoft.CodeAnalysis;
1011

1112
namespace CommunityToolkit.GeneratedDependencyProperty.Models;
@@ -53,8 +54,12 @@ public static TypedConstantInfo Create(TypedConstant arg)
5354
ushort ush => new Primitive.Of<ushort>(ush),
5455
_ => throw new ArgumentException("Invalid primitive type")
5556
},
56-
(TypedConstantKind.Type, ITypeSymbol type) => new Type(type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)),
57-
(TypedConstantKind.Enum, object value) => new Enum(arg.Type!.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), value),
57+
(TypedConstantKind.Type, ITypeSymbol type)
58+
=> new Type(type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)),
59+
(TypedConstantKind.Enum, object value) when arg.Type!.TryGetEnumFieldName(value, out string? fieldName)
60+
=> new KnownEnum(arg.Type!.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), fieldName),
61+
(TypedConstantKind.Enum, object value)
62+
=> new Enum(arg.Type!.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), value),
5863
_ => throw new ArgumentException("Invalid typed constant type"),
5964
};
6065
}
@@ -87,7 +92,10 @@ public static bool TryCreate(IOperation operation, [NotNullWhen(true)] out Typed
8792
{
8893
({ SpecialType: SpecialType.System_String }, string text) => new Primitive.String(text),
8994
({ SpecialType: SpecialType.System_Boolean}, bool flag) => new Primitive.Boolean(flag),
90-
(INamedTypeSymbol { TypeKind: TypeKind.Enum }, object value) => new Enum(operationType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), value),
95+
(INamedTypeSymbol { TypeKind: TypeKind.Enum }, object value) when (operationType.TryGetEnumFieldName(value, out string? fieldName))
96+
=> new KnownEnum(operationType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), fieldName),
97+
(INamedTypeSymbol { TypeKind: TypeKind.Enum }, object value)
98+
=> new Enum(operationType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), value),
9199
(_, byte b) => new Primitive.Of<byte>(b),
92100
(_, char c) => new Primitive.Of<char>(c),
93101
(_, double d) => new Primitive.Of<double>(d),

components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Models/TypedConstantInfo.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ public override string ToString()
138138
}
139139
}
140140

141+
/// <summary>
142+
/// A <see cref="TypedConstantInfo"/> type representing a known enum value.
143+
/// </summary>
144+
/// <param name="TypeName">The enum type name.</param>
145+
/// <param name="FieldName">The enum field name.</param>
146+
public sealed record KnownEnum(string TypeName, string FieldName) : TypedConstantInfo
147+
{
148+
/// <inheritdoc/>
149+
public override string ToString()
150+
{
151+
return $"{TypeName}.{FieldName}";
152+
}
153+
}
154+
141155
/// <summary>
142156
/// A <see cref="TypedConstantInfo"/> type representing an enum value.
143157
/// </summary>

0 commit comments

Comments
 (0)