Skip to content

Commit

Permalink
Improve formatting for known enum members
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Dec 13, 2024
1 parent 7d65ea1 commit e48226c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static bool TryGetDefaultValueForEnumType(this ITypeSymbol symbol, [NotNu
{
if (symbol.TypeKind is not TypeKind.Enum)
{
value = default;
value = null;

return false;
}
Expand All @@ -50,7 +50,43 @@ public static bool TryGetDefaultValueForEnumType(this ITypeSymbol symbol, [NotNu
}
}

value = default;
value = null;

return false;
}

/// <summary>
/// Tries to get the name of the enum field matching a given value.
/// </summary>
/// <param name="symbol">The input <see cref="ITypeSymbol"/> instance to check.</param>
/// <param name="value">The value for to try to get the field for.</param>
/// <param name="fieldName">The name of the field with the specified value, if found.</param>
/// <returns>Whether <paramref name="fieldName"/> was successfully retrieved.</returns>
public static bool TryGetEnumFieldName(this ITypeSymbol symbol, object value, [NotNullWhen(true)] out string? fieldName)
{
if (symbol.TypeKind is not TypeKind.Enum)
{
fieldName = null;

return false;
}

// The default value of the enum is the value of its first constant field
foreach (ISymbol memberSymbol in symbol.GetMembers())
{
if (memberSymbol is not IFieldSymbol { IsConst: true, ConstantValue: object fieldValue } fieldSymbol)
{
continue;
}

if (fieldValue == value)
{
fieldName = fieldSymbol.Name;

return true;
}
}
fieldName = null;

return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using CommunityToolkit.GeneratedDependencyProperty.Extensions;
using Microsoft.CodeAnalysis;

namespace CommunityToolkit.GeneratedDependencyProperty.Models;
Expand Down Expand Up @@ -53,8 +54,12 @@ public static TypedConstantInfo Create(TypedConstant arg)
ushort ush => new Primitive.Of<ushort>(ush),
_ => throw new ArgumentException("Invalid primitive type")
},
(TypedConstantKind.Type, ITypeSymbol type) => new Type(type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)),
(TypedConstantKind.Enum, object value) => new Enum(arg.Type!.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), value),
(TypedConstantKind.Type, ITypeSymbol type)
=> new Type(type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)),
(TypedConstantKind.Enum, object value) when arg.Type!.TryGetEnumFieldName(value, out string? fieldName)
=> new KnownEnum(arg.Type!.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), fieldName),
(TypedConstantKind.Enum, object value)
=> new Enum(arg.Type!.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), value),
_ => throw new ArgumentException("Invalid typed constant type"),
};
}
Expand Down Expand Up @@ -87,7 +92,10 @@ public static bool TryCreate(IOperation operation, [NotNullWhen(true)] out Typed
{
({ SpecialType: SpecialType.System_String }, string text) => new Primitive.String(text),
({ SpecialType: SpecialType.System_Boolean}, bool flag) => new Primitive.Boolean(flag),
(INamedTypeSymbol { TypeKind: TypeKind.Enum }, object value) => new Enum(operationType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), value),
(INamedTypeSymbol { TypeKind: TypeKind.Enum }, object value) when (operationType.TryGetEnumFieldName(value, out string? fieldName))
=> new KnownEnum(operationType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), fieldName),
(INamedTypeSymbol { TypeKind: TypeKind.Enum }, object value)
=> new Enum(operationType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat), value),
(_, byte b) => new Primitive.Of<byte>(b),
(_, char c) => new Primitive.Of<char>(c),
(_, double d) => new Primitive.Of<double>(d),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ public override string ToString()
}
}

/// <summary>
/// A <see cref="TypedConstantInfo"/> type representing a known enum value.
/// </summary>
/// <param name="TypeName">The enum type name.</param>
/// <param name="FieldName">The enum field name.</param>
public sealed record KnownEnum(string TypeName, string FieldName) : TypedConstantInfo
{
/// <inheritdoc/>
public override string ToString()
{
return $"{TypeName}.{FieldName}";
}
}

/// <summary>
/// A <see cref="TypedConstantInfo"/> type representing an enum value.
/// </summary>
Expand Down

0 comments on commit e48226c

Please sign in to comment.