Skip to content

Commit

Permalink
Switch all incremental models to use EquatableArray<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Oct 19, 2022
1 parent c07d1c9 commit 24197cd
Show file tree
Hide file tree
Showing 20 changed files with 54 additions and 735 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
<Compile Include="$(MSBuildThisFileDirectory)ComponentModel\Models\INotifyPropertyChangedInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ComponentModel\Models\ObservableRecipientInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ComponentModel\Models\PropertyInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ComponentModel\Models\TypedConstantInfo.Comparer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ComponentModel\Models\TypedConstantInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ComponentModel\Models\TypedConstantInfo.Factory.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ComponentModel\Models\ValidationInfo.cs" />
Expand Down Expand Up @@ -68,7 +67,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensions\MemberDeclarationSyntaxExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\SyntaxNodeExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensions\TypeDeclarationSyntaxExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\Comparer{T,TSelf}.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\EquatableArray.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\HashCode.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Helpers\ImmutableArrayBuilder{T}.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
Expand All @@ -18,10 +16,13 @@ namespace CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
/// <summary>
/// A model representing an attribute declaration.
/// </summary>
/// <param name="TypeName">The type name of the attribute.</param>
/// <param name="ConstructorArgumentInfo">The <see cref="TypedConstantInfo"/> values for the constructor parameters.</param>
/// <param name="NamedArgumentInfo">The named arguments, if available.</param>
internal sealed record AttributeInfo(
string TypeName,
ImmutableArray<TypedConstantInfo> ConstructorArgumentInfo,
ImmutableArray<(string Name, TypedConstantInfo Value)> NamedArgumentInfo)
EquatableArray<TypedConstantInfo> ConstructorArgumentInfo,
EquatableArray<(string Name, TypedConstantInfo Value)> NamedArgumentInfo)
{
/// <summary>
/// Creates a new <see cref="AttributeInfo"/> instance from a given <see cref="AttributeData"/> value.
Expand Down Expand Up @@ -105,59 +106,15 @@ public AttributeSyntax GetSyntax()
// Gather the constructor arguments
IEnumerable<AttributeArgumentSyntax> arguments =
ConstructorArgumentInfo
.AsImmutableArray()
.Select(static arg => AttributeArgument(arg.GetSyntax()));

// Gather the named arguments
IEnumerable<AttributeArgumentSyntax> namedArguments =
NamedArgumentInfo.Select(static arg =>
NamedArgumentInfo.AsImmutableArray().Select(static arg =>
AttributeArgument(arg.Value.GetSyntax())
.WithNameEquals(NameEquals(IdentifierName(arg.Name))));

return Attribute(IdentifierName(TypeName), AttributeArgumentList(SeparatedList(arguments.Concat(namedArguments))));

}

/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="AttributeInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<AttributeInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, AttributeInfo obj)
{
hashCode.Add(obj.TypeName);
hashCode.AddRange(obj.ConstructorArgumentInfo, TypedConstantInfo.Comparer.Default);

foreach ((string key, TypedConstantInfo value) in obj.NamedArgumentInfo)
{
hashCode.Add(key);
hashCode.Add(value, TypedConstantInfo.Comparer.Default);
}
}

/// <inheritdoc/>
protected override bool AreEqual(AttributeInfo x, AttributeInfo y)
{
if (x.TypeName != y.TypeName ||
!x.ConstructorArgumentInfo.SequenceEqual(y.ConstructorArgumentInfo, TypedConstantInfo.Comparer.Default) ||
x.NamedArgumentInfo.Length != y.NamedArgumentInfo.Length)
{
return false;
}

for (int i = 0; i < x.NamedArgumentInfo.Length; i++)
{
(string Name, TypedConstantInfo Value) left = x.NamedArgumentInfo[i];
(string Name, TypedConstantInfo Value) right = y.NamedArgumentInfo[i];

if (left.Name != right.Name ||
!TypedConstantInfo.Comparer.Default.Equals(left.Value, right.Value))
{
return false;
}
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
using CommunityToolkit.Mvvm.SourceGenerators.Helpers;

namespace CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
Expand All @@ -27,45 +22,9 @@ internal sealed record PropertyInfo(
string TypeNameWithNullabilityAnnotations,
string FieldName,
string PropertyName,
ImmutableArray<string> PropertyChangingNames,
ImmutableArray<string> PropertyChangedNames,
ImmutableArray<string> NotifiedCommandNames,
EquatableArray<string> PropertyChangingNames,
EquatableArray<string> PropertyChangedNames,
EquatableArray<string> NotifiedCommandNames,
bool NotifyPropertyChangedRecipients,
bool NotifyDataErrorInfo,
ImmutableArray<AttributeInfo> ForwardedAttributes)
{
/// <summary>
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="PropertyInfo"/>.
/// </summary>
public sealed class Comparer : Comparer<PropertyInfo, Comparer>
{
/// <inheritdoc/>
protected override void AddToHashCode(ref HashCode hashCode, PropertyInfo obj)
{
hashCode.Add(obj.TypeNameWithNullabilityAnnotations);
hashCode.Add(obj.FieldName);
hashCode.Add(obj.PropertyName);
hashCode.AddRange(obj.PropertyChangingNames);
hashCode.AddRange(obj.PropertyChangedNames);
hashCode.AddRange(obj.NotifiedCommandNames);
hashCode.Add(obj.NotifyPropertyChangedRecipients);
hashCode.Add(obj.NotifyDataErrorInfo);
hashCode.AddRange(obj.ForwardedAttributes, AttributeInfo.Comparer.Default);
}

/// <inheritdoc/>
protected override bool AreEqual(PropertyInfo x, PropertyInfo y)
{
return
x.TypeNameWithNullabilityAnnotations == y.TypeNameWithNullabilityAnnotations &&
x.FieldName == y.FieldName &&
x.PropertyName == y.PropertyName &&
x.PropertyChangingNames.SequenceEqual(y.PropertyChangingNames) &&
x.PropertyChangedNames.SequenceEqual(y.PropertyChangedNames) &&
x.NotifiedCommandNames.SequenceEqual(y.NotifiedCommandNames) &&
x.NotifyPropertyChangedRecipients == y.NotifyPropertyChangedRecipients &&
x.NotifyDataErrorInfo == y.NotifyDataErrorInfo &&
x.ForwardedAttributes.SequenceEqual(y.ForwardedAttributes, AttributeInfo.Comparer.Default);
}
}
}
EquatableArray<AttributeInfo> ForwardedAttributes);

This file was deleted.

Loading

0 comments on commit 24197cd

Please # to comment.