Skip to content

Commit 0bf69ba

Browse files
committed
Fix For Reactive Property with Nested Classes
1 parent 005dfee commit 0bf69ba

17 files changed

+164
-115
lines changed

src/ReactiveUI.SourceGenerators.Execute/TestViewModel3.cs

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,43 @@ public partial class TestViewModel3 : ReactiveObject
1616
[Reactive]
1717
private float _testVM3Property;
1818

19-
/////// <summary>
20-
/////// TestInnerClass.
21-
/////// </summary>
22-
////public partial class TestInnerClass1 : ReactiveObject
23-
////{
24-
//// [Reactive]
25-
//// private int _testInner1;
26-
////}
27-
28-
/////// <summary>
29-
/////// TestInnerClass.
30-
/////// </summary>
31-
////public partial class TestInnerClass2 : ReactiveObject
32-
////{
33-
//// [Reactive]
34-
//// private int _testInner2;
35-
36-
//// /// <summary>
37-
//// /// TestInnerClass4.
38-
//// /// </summary>
39-
//// /// <seealso cref="ReactiveUI.ReactiveObject" />
40-
//// public partial class TestInnerClass3 : ReactiveObject
41-
//// {
42-
//// [Reactive]
43-
//// private int _testInner3;
44-
//// }
45-
////}
19+
[Reactive]
20+
private float _testVM3Property2;
21+
22+
/// <summary>
23+
/// TestInnerClass.
24+
/// </summary>
25+
public partial class TestInnerClass1 : ReactiveObject
26+
{
27+
[Reactive]
28+
private int _testInner1;
29+
30+
[Reactive]
31+
private int _testInner11;
32+
}
33+
34+
/// <summary>
35+
/// TestInnerClass.
36+
/// </summary>
37+
public partial class TestInnerClass2 : ReactiveObject
38+
{
39+
[Reactive]
40+
private int _testInner2;
41+
42+
[Reactive]
43+
private int _testInner22;
44+
45+
/// <summary>
46+
/// TestInnerClass4.
47+
/// </summary>
48+
/// <seealso cref="ReactiveUI.ReactiveObject" />
49+
public partial class TestInnerClass3 : ReactiveObject
50+
{
51+
[Reactive]
52+
private int _testInner3;
53+
54+
[Reactive]
55+
private int _testInner33;
56+
}
57+
}
4658
}

src/ReactiveUI.SourceGenerators/Core/Models/TargetInfo.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// The .NET Foundation licenses this file to you under the MIT license.
44
// See the LICENSE file in the project root for full license information.
55

6+
using System.Collections.Generic;
67
using Microsoft.CodeAnalysis;
78
using ReactiveUI.SourceGenerators.Extensions;
89
using ReactiveUI.SourceGenerators.Helpers;
@@ -15,7 +16,8 @@ internal sealed partial record TargetInfo(
1516
string TargetNamespace,
1617
string TargetNamespaceWithNamespace,
1718
string TargetVisibility,
18-
string TargetType)
19+
string TargetType,
20+
TargetInfo? ParentInfo)
1921
{
2022
public static TargetInfo From(INamedTypeSymbol namedTypeSymbol)
2123
{
@@ -26,12 +28,64 @@ public static TargetInfo From(INamedTypeSymbol namedTypeSymbol)
2628
var targetAccessibility = namedTypeSymbol.GetAccessibilityString();
2729
var targetType = namedTypeSymbol.GetTypeString();
2830

31+
var parentInfo = namedTypeSymbol.ContainingType is not null
32+
? From(namedTypeSymbol.ContainingType)
33+
: null;
34+
2935
return new(
3036
targetHintName,
3137
targetName,
3238
targetNamespace,
3339
targetNameWithNamespace,
3440
targetAccessibility,
35-
targetType);
41+
targetType,
42+
parentInfo);
43+
}
44+
45+
public static void GetParentClasses(ref List<string> parentClassDeclarations, TargetInfo? targetInfo)
46+
{
47+
if (targetInfo is not null)
48+
{
49+
var parentClassDeclaration = $"{targetInfo.TargetVisibility} partial {targetInfo.TargetType} {targetInfo.TargetName}";
50+
51+
// Add the parent class declaration if it does not exist in the list
52+
if (!parentClassDeclarations.Contains(parentClassDeclaration))
53+
{
54+
parentClassDeclarations.Add(parentClassDeclaration);
55+
}
56+
57+
if (targetInfo.ParentInfo is not null)
58+
{
59+
// Recursively get the parent classes
60+
GetParentClasses(ref parentClassDeclarations, targetInfo.ParentInfo);
61+
}
62+
}
63+
}
64+
65+
public static string GenerateParentClassDeclarations(ref List<string> parentClassDeclarations)
66+
{
67+
// Reverse the list to get the parent classes in the correct order
68+
parentClassDeclarations.Reverse();
69+
70+
// Generate the parent class declarations
71+
var parentClassDeclarationsString = string.Join("\n{\n", parentClassDeclarations);
72+
if (!string.IsNullOrWhiteSpace(parentClassDeclarationsString))
73+
{
74+
parentClassDeclarationsString += "\n{\n";
75+
}
76+
77+
return parentClassDeclarationsString;
78+
}
79+
80+
public static string GenerateClosingBrackets(int numberOfBrackets)
81+
{
82+
var closingBrackets = new string('}', numberOfBrackets);
83+
closingBrackets = closingBrackets.Replace("}", "}\n");
84+
if (!string.IsNullOrWhiteSpace(closingBrackets))
85+
{
86+
closingBrackets = "\n" + closingBrackets;
87+
}
88+
89+
return closingBrackets;
3690
}
3791
}

src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.Execute.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,7 @@ public partial class IViewForGenerator
9292
token.ThrowIfCancellationRequested();
9393

9494
return new(
95-
targetInfo.FileHintName,
96-
targetInfo.TargetName,
97-
targetInfo.TargetNamespace,
98-
targetInfo.TargetNamespaceWithNamespace,
99-
targetInfo.TargetVisibility,
100-
targetInfo.TargetType,
95+
targetInfo,
10196
viewModelTypeName!,
10297
viewForBaseType);
10398
}

src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4040
context.RegisterSourceOutput(iViewForInfo, static (context, input) =>
4141
{
4242
var groupedPropertyInfo = input.GroupBy(
43-
static info => (info.FileHintName, info.TargetName, info.TargetNamespace, info.TargetVisibility, info.TargetType),
43+
static info => (info.TargetInfo.FileHintName, info.TargetInfo.TargetName, info.TargetInfo.TargetNamespace, info.TargetInfo.TargetVisibility, info.TargetInfo.TargetType),
4444
static info => info)
4545
.ToImmutableArray();
4646

src/ReactiveUI.SourceGenerators/IViewFor/Models/IViewForInfo.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
// See the LICENSE file in the project root for full license information.
55

66
using ReactiveUI.SourceGenerators.Helpers;
7+
using ReactiveUI.SourceGenerators.Models;
78

89
namespace ReactiveUI.SourceGenerators.Input.Models;
910

1011
/// <summary>
1112
/// A model with gathered info on a given command method.
1213
/// </summary>
1314
internal sealed record IViewForInfo(
14-
string FileHintName,
15-
string TargetName,
16-
string TargetNamespace,
17-
string TargetNamespaceWithNamespace,
18-
string TargetVisibility,
19-
string TargetType,
15+
TargetInfo TargetInfo,
2016
string ViewModelTypeName,
2117
IViewForBaseType BaseType);

src/ReactiveUI.SourceGenerators/ObservableAsProperty/Models/ObservableFieldInfo.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@
44
// See the LICENSE file in the project root for full license information.
55

66
using ReactiveUI.SourceGenerators.Helpers;
7+
using ReactiveUI.SourceGenerators.Models;
78

89
namespace ReactiveUI.SourceGenerators.Reactive.Models;
910

1011
/// <summary>
1112
/// A model with gathered info on a given field.
1213
/// </summary>
1314
internal sealed record ObservableFieldInfo(
14-
string FileHintName,
15-
string TargetName,
16-
string TargetNamespace,
17-
string TargetNamespaceWithNamespace,
18-
string TargetVisibility,
19-
string TargetType,
15+
TargetInfo TargetInfo,
2016
string TypeNameWithNullabilityAnnotations,
2117
string FieldName,
2218
string PropertyName,

src/ReactiveUI.SourceGenerators/ObservableAsProperty/Models/ObservableMethodInfo.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,20 @@
55

66
using System.Globalization;
77
using ReactiveUI.SourceGenerators.Helpers;
8+
using ReactiveUI.SourceGenerators.Models;
89

910
namespace ReactiveUI.SourceGenerators.ObservableAsProperty.Models
1011
{
1112
internal record ObservableMethodInfo(
12-
string FileHintName,
13-
string TargetName,
14-
string TargetNamespace,
15-
string TargetNamespaceWithNamespace,
16-
string TargetVisibility,
17-
string TargetType,
18-
string MethodName,
19-
string MethodReturnType,
20-
string? ArgumentType,
21-
string PropertyName,
22-
string ObservableType,
23-
bool IsNullableType,
24-
bool IsProperty,
25-
EquatableArray<string> ForwardedPropertyAttributes)
13+
TargetInfo TargetInfo,
14+
string MethodName,
15+
string MethodReturnType,
16+
string? ArgumentType,
17+
string PropertyName,
18+
string ObservableType,
19+
bool IsNullableType,
20+
bool IsProperty,
21+
EquatableArray<string> ForwardedPropertyAttributes)
2622
{
2723
public string GetGeneratedFieldName()
2824
{

src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator{FromField}.Execute.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,7 @@ public sealed partial class ObservableAsPropertyGenerator
195195

196196
return new(
197197
new(
198-
targetInfo.FileHintName,
199-
targetInfo.TargetName,
200-
targetInfo.TargetNamespace,
201-
targetInfo.TargetNamespaceWithNamespace,
202-
targetInfo.TargetVisibility,
203-
targetInfo.TargetType,
198+
targetInfo,
204199
typeNameWithNullabilityAnnotations,
205200
fieldName,
206201
propertyName,

src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator{FromField}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private static void RunObservableAsPropertyFromField(in IncrementalGeneratorInit
4242
var groupedPropertyInfo = input
4343
.Where(static x => x.Value != null)
4444
.Select(static x => x.Value!).GroupBy(
45-
static info => (info.FileHintName, info.TargetName, info.TargetNamespace, info.TargetVisibility, info.TargetType),
45+
static info => (info.TargetInfo.FileHintName, info.TargetInfo.TargetName, info.TargetInfo.TargetNamespace, info.TargetInfo.TargetVisibility, info.TargetInfo.TargetType),
4646
static info => info)
4747
.ToImmutableArray();
4848

src/ReactiveUI.SourceGenerators/ObservableAsProperty/ObservableAsPropertyGenerator{FromObservable}.Execute.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,7 @@ public sealed partial class ObservableAsPropertyGenerator
7474

7575
return new(
7676
new(
77-
targetInfo.FileHintName,
78-
targetInfo.TargetName,
79-
targetInfo.TargetNamespace,
80-
targetInfo.TargetNamespaceWithNamespace,
81-
targetInfo.TargetVisibility,
82-
targetInfo.TargetType,
77+
targetInfo,
8378
methodSymbol.Name,
8479
methodSymbol.ReturnType.GetFullyQualifiedNameWithNullabilityAnnotations(),
8580
methodSymbol.Parameters.FirstOrDefault()?.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),
@@ -120,12 +115,7 @@ public sealed partial class ObservableAsPropertyGenerator
120115

121116
return new(
122117
new(
123-
targetInfo.FileHintName,
124-
targetInfo.TargetName,
125-
targetInfo.TargetNamespace,
126-
targetInfo.TargetNamespaceWithNamespace,
127-
targetInfo.TargetVisibility,
128-
targetInfo.TargetType,
118+
targetInfo,
129119
propertySymbol.Name,
130120
propertySymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations(),
131121
propertySymbol.Parameters.FirstOrDefault()?.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat),

0 commit comments

Comments
 (0)