Skip to content

Commit 9439f37

Browse files
committed
Add 'SyntaxKind' extensions
1 parent c100c0e commit 9439f37

File tree

1 file changed

+39
-0
lines changed
  • components/DependencyPropertyGenerator/CommunityToolkit.DependencyPropertyGenerator.SourceGenerators/Extensions

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Immutable;
6+
using System.Runtime.CompilerServices;
7+
using Microsoft.CodeAnalysis.CSharp;
8+
9+
namespace CommunityToolkit.GeneratedDependencyProperty.Extensions;
10+
11+
/// <summary>
12+
/// A <see langword="class"/> with some extension methods for C# syntax kinds.
13+
/// </summary>
14+
internal static partial class SyntaxKindExtensions
15+
{
16+
/// <summary>
17+
/// Converts an <see cref="ImmutableArray{T}"/> of <see cref="SyntaxKind"/> values to one of their underlying type.
18+
/// </summary>
19+
/// <param name="array">The input <see cref="ImmutableArray{T}"/> value.</param>
20+
/// <returns>The resulting <see cref="ImmutableArray{T}"/> of <see cref="ushort"/> values.</returns>
21+
public static ImmutableArray<ushort> AsUnderlyingType(this ImmutableArray<SyntaxKind> array)
22+
{
23+
ushort[]? underlyingArray = (ushort[]?)(object?)Unsafe.As<ImmutableArray<SyntaxKind>, SyntaxKind[]?>(ref array);
24+
25+
return Unsafe.As<ushort[]?, ImmutableArray<ushort>>(ref underlyingArray);
26+
}
27+
28+
/// <summary>
29+
/// Converts an <see cref="ImmutableArray{T}"/> of <see cref="ushort"/> values to one of their real type.
30+
/// </summary>
31+
/// <param name="array">The input <see cref="ImmutableArray{T}"/> value.</param>
32+
/// <returns>The resulting <see cref="ImmutableArray{T}"/> of <see cref="SyntaxKind"/> values.</returns>
33+
public static ImmutableArray<SyntaxKind> AsSyntaxKindArray(this ImmutableArray<ushort> array)
34+
{
35+
SyntaxKind[]? typedArray = (SyntaxKind[]?)(object?)Unsafe.As<ImmutableArray<ushort>, ushort[]?>(ref array);
36+
37+
return Unsafe.As<SyntaxKind[]?, ImmutableArray<SyntaxKind>>(ref typedArray);
38+
}
39+
}

0 commit comments

Comments
 (0)