Skip to content

Commit 794cd2d

Browse files
committed
The name of the "state" parameter in Switch/Map is configurable.
1 parent c7e1b18 commit 794cd2d

File tree

284 files changed

+1893
-883
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

284 files changed

+1893
-883
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>8.7.0</VersionPrefix>
5+
<VersionPrefix>8.8.0</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionCodeGenerator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
385385
if (withState)
386386
{
387387
_sb.Append(@"
388-
/// <param name=""state"">State to be passed to the callbacks.</param>");
388+
/// <param name=""").Append(_state.Settings.SwitchMapStateParameterName).Append(@""">State to be passed to the callbacks.</param>");
389389
}
390390

391391
if (isPartially)
@@ -414,7 +414,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
414414
{
415415
_sb.Append(@"
416416
public void ").Append(methodName).Append(@"<TState>(
417-
TState state,");
417+
TState ").AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(",");
418418
}
419419
else
420420
{
@@ -508,7 +508,7 @@ private void GenerateIndexBasedActionSwitchBody(bool withState, bool isPartially
508508
").AppendEscaped(memberType.ArgumentName).Append("(");
509509

510510
if (withState)
511-
_sb.Append("state, ");
511+
_sb.AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(", ");
512512

513513
_sb.Append("this.").Append(memberType.BackingFieldName).Append(memberType.IsReferenceType && memberType.NullableAnnotation != NullableAnnotation.Annotated ? "!" : null).Append(@");
514514
return;");
@@ -526,7 +526,7 @@ private void GenerateIndexBasedActionSwitchBody(bool withState, bool isPartially
526526
@default?.Invoke(");
527527

528528
if (withState)
529-
_sb.Append("state, ");
529+
_sb.AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(", ");
530530

531531
_sb.Append("this.Value);");
532532
}
@@ -543,7 +543,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
543543
if (withState)
544544
{
545545
_sb.Append(@"
546-
/// <param name=""state"">State to be passed to the callbacks.</param>");
546+
/// <param name=""").Append(_state.Settings.SwitchMapStateParameterName).Append(@""">State to be passed to the callbacks.</param>");
547547
}
548548

549549
if (isPartially)
@@ -572,7 +572,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
572572
{
573573
_sb.Append(@"
574574
public TResult ").Append(methodName).Append(@"<TState, TResult>(
575-
TState state,");
575+
TState ").AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(",");
576576
}
577577
else
578578
{
@@ -669,7 +669,7 @@ private void GenerateIndexBasedFuncSwitchBody(bool withState, bool isPartially)
669669
return ").AppendEscaped(memberType.ArgumentName).Append("(");
670670

671671
if (withState)
672-
_sb.Append("state, ");
672+
_sb.AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(", ");
673673

674674
_sb.Append("this.").Append(memberType.BackingFieldName).Append(memberType is { IsReferenceType: true, Setting.IsNullableReferenceType: false } ? "!" : null).Append(");");
675675
}
@@ -686,7 +686,7 @@ private void GenerateIndexBasedFuncSwitchBody(bool withState, bool isPartially)
686686
return @default(");
687687

688688
if (withState)
689-
_sb.Append("state, ");
689+
_sb.AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(", ");
690690

691691
_sb.Append("this.Value);");
692692
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public sealed class AdHocUnionSettings : IEquatable<AdHocUnionSettings>
1212
public UnionConstructorAccessModifier ConstructorAccessModifier { get; }
1313
public ConversionOperatorsGeneration ConversionFromValue { get; }
1414
public ConversionOperatorsGeneration ConversionToValue { get; }
15+
public string SwitchMapStateParameterName { get; }
1516
public bool HasStructLayoutAttribute => _attributeInfo.HasStructLayoutAttribute;
1617

1718
public AdHocUnionSettings(
@@ -26,6 +27,7 @@ public AdHocUnionSettings(
2627
ConstructorAccessModifier = attribute.FindUnionConstructorAccessModifier();
2728
ConversionFromValue = attribute.FindConversionFromValue() ?? ConversionOperatorsGeneration.Implicit;
2829
ConversionToValue = attribute.FindConversionToValue() ?? ConversionOperatorsGeneration.Explicit;
30+
SwitchMapStateParameterName = attribute.FindSwitchMapStateParameterName();
2931
_attributeInfo = attributeInfo;
3032

3133
var memberTypeSettings = new AdHocUnionMemberTypeSetting[numberOfMemberTypes];
@@ -57,6 +59,7 @@ public bool Equals(AdHocUnionSettings? other)
5759
&& ConstructorAccessModifier == other.ConstructorAccessModifier
5860
&& ConversionFromValue == other.ConversionFromValue
5961
&& ConversionToValue == other.ConversionToValue
62+
&& SwitchMapStateParameterName == other.SwitchMapStateParameterName
6063
&& HasStructLayoutAttribute == other.HasStructLayoutAttribute
6164
&& MemberTypeSettings.SequenceEqual(other.MemberTypeSettings);
6265
}
@@ -72,6 +75,7 @@ public override int GetHashCode()
7275
hashCode = (hashCode * 397) ^ (int)ConstructorAccessModifier;
7376
hashCode = (hashCode * 397) ^ (int)ConversionFromValue;
7477
hashCode = (hashCode * 397) ^ (int)ConversionToValue;
78+
hashCode = (hashCode * 397) ^ SwitchMapStateParameterName.GetHashCode();
7579
hashCode = (hashCode * 397) ^ HasStructLayoutAttribute.GetHashCode();
7680
hashCode = (hashCode * 397) ^ MemberTypeSettings.ComputeHashCode();
7781

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionCodeGenerator.cs renamed to src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/RegularUnions/RegularUnionCodeGenerator.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
using System.Text;
22

3-
namespace Thinktecture.CodeAnalysis.Unions;
3+
namespace Thinktecture.CodeAnalysis.RegularUnions;
44

5-
public class UnionCodeGenerator : CodeGeneratorBase
5+
public class RegularUnionCodeGenerator : CodeGeneratorBase
66
{
77
private readonly record struct TypeMember(
8-
UnionTypeMemberState State,
8+
RegularUnionTypeMemberState State,
99
string ArgumentName);
1010

11-
public override string CodeGeneratorName => "Union-CodeGenerator";
11+
public override string CodeGeneratorName => "RegularUnion-CodeGenerator";
1212
public override string? FileNameSuffix => null;
1313

14-
private readonly UnionSourceGenState _state;
14+
private readonly RegularUnionSourceGenState _state;
1515
private readonly StringBuilder _sb;
1616
private readonly IReadOnlyList<TypeMember> _typeMembers;
1717

18-
public UnionCodeGenerator(
19-
UnionSourceGenState state,
18+
public RegularUnionCodeGenerator(
19+
RegularUnionSourceGenState state,
2020
StringBuilder sb)
2121
{
2222
_state = state;
@@ -29,11 +29,11 @@ public UnionCodeGenerator(
2929
}
3030

3131
private List<TypeMember> OrderTypeMembers(
32-
UnionSourceGenState state,
32+
RegularUnionSourceGenState state,
3333
StringBuilder sb)
3434
{
3535
var typeMembers = new List<TypeMember>();
36-
var unsortedTypeMembers = new List<UnionTypeMemberState>();
36+
var unsortedTypeMembers = new List<RegularUnionTypeMemberState>();
3737

3838
// Reversed for-loop because whole collection will be reversed again by the caller
3939
for (var i = _state.TypeMembers.Count - 1; i >= 0; i--)
@@ -77,7 +77,7 @@ private List<TypeMember> OrderTypeMembers(
7777
}
7878

7979
private static TypeMember MakeTypeMember(
80-
UnionTypeMemberState typeMember,
80+
RegularUnionTypeMemberState typeMember,
8181
StringBuilder sb)
8282
{
8383
var argName = typeMember.ContainingTypes
@@ -219,7 +219,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
219219
if (withState)
220220
{
221221
_sb.Append(@"
222-
/// <param name=""state"">State to be passed to the callbacks.</param>");
222+
/// <param name=""").Append(_state.Settings.SwitchMapStateParameterName).Append(@""">State to be passed to the callbacks.</param>");
223223
}
224224

225225
if (isPartially)
@@ -244,7 +244,7 @@ private void GenerateSwitchForAction(bool withState, bool isPartially)
244244
if (withState)
245245
{
246246
_sb.Append(@"<TState>(
247-
TState state,");
247+
TState ").AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(",");
248248
}
249249
else
250250
{
@@ -330,7 +330,7 @@ private void GenerateIndexBasedActionSwitchBody(bool withState, bool isPartially
330330
").AppendEscaped(typeMember.ArgumentName).Append("(");
331331

332332
if (withState)
333-
_sb.Append("state, ");
333+
_sb.AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(", ");
334334

335335
_sb.Append(@"value);
336336
return;");
@@ -348,7 +348,7 @@ private void GenerateIndexBasedActionSwitchBody(bool withState, bool isPartially
348348
@default?.Invoke(");
349349

350350
if (withState)
351-
_sb.Append("state, ");
351+
_sb.AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(", ");
352352

353353
_sb.Append("this);");
354354
}
@@ -365,7 +365,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
365365
if (withState)
366366
{
367367
_sb.Append(@"
368-
/// <param name=""state"">State to be passed to the callbacks.</param>");
368+
/// <param name=""").Append(_state.Settings.SwitchMapStateParameterName).Append(@""">State to be passed to the callbacks.</param>");
369369
}
370370

371371
if (isPartially)
@@ -390,7 +390,7 @@ private void GenerateSwitchForFunc(bool withState, bool isPartially)
390390
if (withState)
391391
{
392392
_sb.Append(@"<TState, TResult>(
393-
TState state,");
393+
TState ").AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(",");
394394
}
395395
else
396396
{
@@ -477,7 +477,7 @@ private void GenerateIndexBasedFuncSwitchBody(bool withState, bool isPartially)
477477
return ").AppendEscaped(typeMember.ArgumentName).Append("(");
478478

479479
if (withState)
480-
_sb.Append("state, ");
480+
_sb.AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(", ");
481481

482482
_sb.Append("value);");
483483
}
@@ -494,7 +494,7 @@ private void GenerateIndexBasedFuncSwitchBody(bool withState, bool isPartially)
494494
return @default(");
495495

496496
if (withState)
497-
_sb.Append("state, ");
497+
_sb.AppendEscaped(_state.Settings.SwitchMapStateParameterName).Append(", ");
498498

499499
_sb.Append("this);");
500500
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Text;
2+
3+
namespace Thinktecture.CodeAnalysis.RegularUnions;
4+
5+
public class RegularUnionCodeGeneratorFactory : ICodeGeneratorFactory<RegularUnionSourceGenState>
6+
{
7+
public static readonly ICodeGeneratorFactory<RegularUnionSourceGenState> Instance = new RegularUnionCodeGeneratorFactory();
8+
9+
public string CodeGeneratorName => "RegularUnion-CodeGenerator";
10+
11+
private RegularUnionCodeGeneratorFactory()
12+
{
13+
}
14+
15+
public CodeGeneratorBase Create(RegularUnionSourceGenState state, StringBuilder stringBuilder)
16+
{
17+
return new RegularUnionCodeGenerator(state, stringBuilder);
18+
}
19+
20+
public bool Equals(ICodeGeneratorFactory<RegularUnionSourceGenState> other)
21+
{
22+
return ReferenceEquals(this, other);
23+
}
24+
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionSettings.cs renamed to src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/RegularUnions/RegularUnionSettings.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
1-
namespace Thinktecture.CodeAnalysis.Unions;
1+
namespace Thinktecture.CodeAnalysis.RegularUnions;
22

3-
public sealed class UnionSettings : IEquatable<UnionSettings>, IHashCodeComputable
3+
public sealed class RegularUnionSettings : IEquatable<RegularUnionSettings>, IHashCodeComputable
44
{
55
public SwitchMapMethodsGeneration SwitchMethods { get; }
66
public SwitchMapMethodsGeneration MapMethods { get; }
77
public ConversionOperatorsGeneration ConversionFromValue { get; }
8+
public string SwitchMapStateParameterName { get; }
89

9-
public UnionSettings(AttributeData attribute)
10+
public RegularUnionSettings(AttributeData attribute)
1011
{
1112
SwitchMethods = attribute.FindSwitchMethods();
1213
MapMethods = attribute.FindMapMethods();
1314
ConversionFromValue = attribute.FindConversionFromValue() ?? ConversionOperatorsGeneration.Implicit;
15+
SwitchMapStateParameterName = attribute.FindSwitchMapStateParameterName();
1416
}
1517

1618
public override bool Equals(object? obj)
1719
{
18-
return obj is UnionSettings enumSettings && Equals(enumSettings);
20+
return obj is RegularUnionSettings enumSettings && Equals(enumSettings);
1921
}
2022

21-
public bool Equals(UnionSettings? other)
23+
public bool Equals(RegularUnionSettings? other)
2224
{
2325
if (other is null)
2426
return false;
@@ -28,7 +30,8 @@ public bool Equals(UnionSettings? other)
2830

2931
return SwitchMethods == other.SwitchMethods
3032
&& MapMethods == other.MapMethods
31-
&& ConversionFromValue == other.ConversionFromValue;
33+
&& ConversionFromValue == other.ConversionFromValue
34+
&& SwitchMapStateParameterName == other.SwitchMapStateParameterName;
3235
}
3336

3437
public override int GetHashCode()
@@ -38,6 +41,7 @@ public override int GetHashCode()
3841
var hashCode = SwitchMethods.GetHashCode();
3942
hashCode = (hashCode * 397) ^ MapMethods.GetHashCode();
4043
hashCode = (hashCode * 397) ^ ConversionFromValue.GetHashCode();
44+
hashCode = (hashCode * 397) ^ SwitchMapStateParameterName.GetHashCode();
4145

4246
return hashCode;
4347
}

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionSourceGenState.cs renamed to src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/RegularUnions/RegularUnionSourceGenState.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
namespace Thinktecture.CodeAnalysis.Unions;
1+
namespace Thinktecture.CodeAnalysis.RegularUnions;
22

3-
public class UnionSourceGenState : IEquatable<UnionSourceGenState>, ITypeFullyQualified, INamespaceAndName, IHashCodeComputable
3+
public class RegularUnionSourceGenState : IEquatable<RegularUnionSourceGenState>, ITypeFullyQualified, INamespaceAndName, IHashCodeComputable
44
{
55
public string? Namespace { get; }
66
public string Name { get; }
@@ -11,13 +11,13 @@ public class UnionSourceGenState : IEquatable<UnionSourceGenState>, ITypeFullyQu
1111

1212
public IReadOnlyList<string> GenericsFullyQualified { get; }
1313
public IReadOnlyList<ContainingTypeState> ContainingTypes { get; }
14-
public IReadOnlyList<UnionTypeMemberState> TypeMembers { get; }
15-
public UnionSettings Settings { get; }
14+
public IReadOnlyList<RegularUnionTypeMemberState> TypeMembers { get; }
15+
public RegularUnionSettings Settings { get; }
1616

17-
public UnionSourceGenState(
17+
public RegularUnionSourceGenState(
1818
INamedTypeSymbol type,
19-
IReadOnlyList<UnionTypeMemberState> typeMembers,
20-
UnionSettings settings)
19+
IReadOnlyList<RegularUnionTypeMemberState> typeMembers,
20+
RegularUnionSettings settings)
2121
{
2222
Name = type.Name;
2323
Namespace = type.ContainingNamespace?.IsGlobalNamespace == true ? null : type.ContainingNamespace?.ToString();
@@ -34,7 +34,7 @@ public UnionSourceGenState(
3434
Settings = settings;
3535
}
3636

37-
public bool Equals(UnionSourceGenState? other)
37+
public bool Equals(RegularUnionSourceGenState? other)
3838
{
3939
if (other is null)
4040
return false;

0 commit comments

Comments
 (0)