Skip to content

Commit 969fcb4

Browse files
committed
Remove extra projection and comparers from command generator
1 parent ce9b713 commit 969fcb4

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

CommunityToolkit.Mvvm.SourceGenerators/Input/Models/CommandInfo.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ internal sealed record CommandInfo(
4242
bool FlowExceptionsToTaskScheduler,
4343
bool IncludeCancelCommand)
4444
{
45+
/// <inheritdoc/>
46+
public bool Equals(CommandInfo? obj) => Comparer.Default.Equals(this, obj);
47+
48+
/// <inheritdoc/>
49+
public override int GetHashCode() => Comparer.Default.GetHashCode(this);
50+
4551
/// <summary>
4652
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="CommandInfo"/>.
4753
/// </summary>
48-
public sealed class Comparer : Comparer<CommandInfo, Comparer>
54+
private sealed class Comparer : Comparer<CommandInfo, Comparer>
4955
{
5056
/// <inheritdoc/>
5157
protected override void AddToHashCode(ref HashCode hashCode, CommandInfo obj)

CommunityToolkit.Mvvm.SourceGenerators/Input/RelayCommandGenerator.Execute.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ partial class RelayCommandGenerator
2727
internal static class Execute
2828
{
2929
/// <summary>
30-
/// Processes a given target method.
30+
/// Processes a given annotated methods and produces command info, if possible.
3131
/// </summary>
3232
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
3333
/// <param name="attributeData">The <see cref="AttributeData"/> instance the method was annotated with.</param>
34+
/// <param name="commandInfo">The resulting <see cref="CommandInfo"/> instance, if successfully generated.</param>
3435
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
35-
/// <returns>The resulting <see cref="CommandInfo"/> instance for <paramref name="methodSymbol"/>, if available.</returns>
36-
public static CommandInfo? GetInfo(IMethodSymbol methodSymbol, AttributeData attributeData, out ImmutableArray<DiagnosticInfo> diagnostics)
36+
/// <returns>Whether a <see cref="CommandInfo"/> instance could be generated successfully.</returns>
37+
public static bool TryGetInfo(
38+
IMethodSymbol methodSymbol,
39+
AttributeData attributeData,
40+
[NotNullWhen(true)] out CommandInfo? commandInfo,
41+
out ImmutableArray<DiagnosticInfo> diagnostics)
3742
{
3843
ImmutableArray<DiagnosticInfo>.Builder builder = ImmutableArray.CreateBuilder<DiagnosticInfo>();
3944

@@ -107,9 +112,7 @@ internal static class Execute
107112
goto Failure;
108113
}
109114

110-
diagnostics = builder.ToImmutable();
111-
112-
return new(
115+
commandInfo = new CommandInfo(
113116
methodSymbol.Name,
114117
fieldName,
115118
propertyName,
@@ -124,10 +127,15 @@ internal static class Execute
124127
flowExceptionsToTaskScheduler,
125128
generateCancelCommand);
126129

130+
diagnostics = builder.ToImmutable();
131+
132+
return true;
133+
127134
Failure:
135+
commandInfo = null;
128136
diagnostics = builder.ToImmutable();
129137

130-
return null;
138+
return false;
131139
}
132140

133141
/// <summary>

CommunityToolkit.Mvvm.SourceGenerators/Input/RelayCommandGenerator.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,30 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3838

3939
IMethodSymbol methodSymbol = (IMethodSymbol)context.TargetSymbol;
4040

41-
// Produce the incremental models
42-
HierarchyInfo hierarchy = HierarchyInfo.From(methodSymbol.ContainingType);
43-
CommandInfo? commandInfo = Execute.GetInfo(methodSymbol, context.Attributes[0], out ImmutableArray<DiagnosticInfo> diagnostics);
41+
// Get the hierarchy info for the target symbol, and try to gather the command info
42+
HierarchyInfo? hierarchy = HierarchyInfo.From(methodSymbol.ContainingType);
43+
44+
_ = Execute.TryGetInfo(methodSymbol, context.Attributes[0], out CommandInfo? commandInfo, out ImmutableArray<DiagnosticInfo> diagnostics);
4445

4546
return (Hierarchy: hierarchy, new Result<CommandInfo?>(commandInfo, diagnostics));
4647
})
47-
.Where(static item => item.Hierarchy is not null);
48+
.Where(static item => item.Hierarchy is not null)!;
4849

4950
// Output the diagnostics
5051
context.ReportDiagnostics(commandInfoWithErrors.Select(static (item, _) => item.Info.Errors));
5152

5253
// Get the filtered sequence to enable caching
53-
IncrementalValuesProvider<(HierarchyInfo Hierarchy, CommandInfo Info)> commandInfo =
54+
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<CommandInfo> Info)> commandInfo =
5455
commandInfoWithErrors
55-
.Where(static item => item.Info.Value is not null)
56-
.Select(static (item, _) => (item.Hierarchy, item.Info.Value!))
57-
.WithComparers(HierarchyInfo.Comparer.Default, CommandInfo.Comparer.Default);
56+
.Where(static item => item.Info.Value is not null)!;
5857

5958
// Generate the commands
6059
context.RegisterSourceOutput(commandInfo, static (context, item) =>
6160
{
62-
ImmutableArray<MemberDeclarationSyntax> memberDeclarations = Execute.GetSyntax(item.Info);
61+
ImmutableArray<MemberDeclarationSyntax> memberDeclarations = Execute.GetSyntax(item.Info.Value);
6362
CompilationUnitSyntax compilationUnit = item.Hierarchy.GetCompilationUnit(memberDeclarations);
6463

65-
context.AddSource($"{item.Hierarchy.FilenameHint}.{item.Info.MethodName}.g.cs", compilationUnit.GetText(Encoding.UTF8));
64+
context.AddSource($"{item.Hierarchy.FilenameHint}.{item.Info.Value.MethodName}.g.cs", compilationUnit.GetText(Encoding.UTF8));
6665
});
6766
}
6867
}

0 commit comments

Comments
 (0)