Skip to content

Commit 9defa26

Browse files
committed
Minor code refactoring
1 parent b173cd3 commit 9defa26

File tree

3 files changed

+41
-56
lines changed

3 files changed

+41
-56
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
7676
// Split and group by containing type
7777
IncrementalValuesProvider<(HierarchyInfo Hierarchy, ImmutableArray<PropertyInfo> Properties)> groupedPropertyInfo =
7878
propertyInfo
79-
.Collect()
8079
.GroupBy(HierarchyInfo.Comparer.Default)
8180
.WithComparers(HierarchyInfo.Comparer.Default, PropertyInfo.Comparer.Default.ForImmutableArray());
8281

CommunityToolkit.Mvvm.SourceGenerators/Extensions/IncrementalValueProviderExtensions.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

CommunityToolkit.Mvvm.SourceGenerators/Extensions/IncrementalValuesProviderExtensions.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System;
99
using System.Collections.Generic;
10+
using System.Collections.Immutable;
1011
using Microsoft.CodeAnalysis;
1112

1213
namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
@@ -16,6 +17,46 @@ namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
1617
/// </summary>
1718
internal static class IncrementalValuesProviderExtensions
1819
{
20+
/// <summary>
21+
/// Groups items in a given <see cref="IncrementalValuesProvider{TValue}"/> sequence by a specified key.
22+
/// </summary>
23+
/// <typeparam name="TLeft">The type of left items in each tuple.</typeparam>
24+
/// <typeparam name="TRight">The type of right items in each tuple.</typeparam>
25+
/// <param name="source">The input <see cref="IncrementalValuesProvider{TValues}"/> instance.</param>
26+
/// <param name="comparer">A <typeparamref name="TLeft"/> comparer.</param>
27+
/// <returns>An <see cref="IncrementalValuesProvider{TValues}"/> with the grouped results.</returns>
28+
public static IncrementalValuesProvider<(TLeft Left, ImmutableArray<TRight> Right)> GroupBy<TLeft, TRight>(
29+
this IncrementalValuesProvider<(TLeft Left, TRight Right)> source,
30+
IEqualityComparer<TLeft> comparer)
31+
{
32+
return source.Collect().SelectMany((item, _) =>
33+
{
34+
Dictionary<TLeft, ImmutableArray<TRight>.Builder> map = new(comparer);
35+
36+
foreach ((TLeft hierarchy, TRight info) in item)
37+
{
38+
if (!map.TryGetValue(hierarchy, out ImmutableArray<TRight>.Builder builder))
39+
{
40+
builder = ImmutableArray.CreateBuilder<TRight>();
41+
42+
map.Add(hierarchy, builder);
43+
}
44+
45+
builder.Add(info);
46+
}
47+
48+
ImmutableArray<(TLeft Hierarchy, ImmutableArray<TRight> Properties)>.Builder result =
49+
ImmutableArray.CreateBuilder<(TLeft, ImmutableArray<TRight>)>();
50+
51+
foreach (KeyValuePair<TLeft, ImmutableArray<TRight>.Builder> entry in map)
52+
{
53+
result.Add((entry.Key, entry.Value.ToImmutable()));
54+
}
55+
56+
return result;
57+
});
58+
}
59+
1960
/// <summary>
2061
/// Creates a new <see cref="IncrementalValuesProvider{TValues}"/> instance with a gven pair of comparers.
2162
/// </summary>

0 commit comments

Comments
 (0)