Skip to content

Commit 725bb73

Browse files
authored
Fix For Reactive Property with Nested Classes (#152)
* Fix For Reactive Property with Nested Classes * Update slight difference in output with regards to spacing
1 parent 005dfee commit 725bb73

23 files changed

+170
-121
lines changed

src/ReactiveUI.SourceGenerator.Tests/REACTIVE/ReactiveGeneratorTests.FromReactiveProperiesWithAttributes#TestNs.TestVM.Properties.g.verified.cs

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

88
namespace TestNs
99
{
10-
/// <summary>
10+
/// <summary>
1111
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
1212
/// </summary>
1313
public partial class TestVM

src/ReactiveUI.SourceGenerator.Tests/REACTIVE/ReactiveGeneratorTests.FromReactiveProperties#TestNs.TestVM.Properties.g.verified.cs

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

88
namespace TestNs
99
{
10-
/// <summary>
10+
/// <summary>
1111
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
1212
/// </summary>
1313
public partial class TestVM

src/ReactiveUI.SourceGenerator.Tests/REACTIVE/ReactiveGeneratorTests.FromReactivePropertiesWithAccess#TestNs.TestVM.Properties.g.verified.cs

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

88
namespace TestNs
99
{
10-
/// <summary>
10+
/// <summary>
1111
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
1212
/// </summary>
1313
public partial class TestVM

src/ReactiveUI.SourceGenerator.Tests/REACTIVE/ReactiveGeneratorTests.FromReactivePropertiesWithAttributesAccessAndInheritance#TestNs.TestVM.Properties.g.verified.cs

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

88
namespace TestNs
99
{
10-
/// <summary>
10+
/// <summary>
1111
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
1212
/// </summary>
1313
public partial class TestVM

src/ReactiveUI.SourceGenerator.Tests/REACTIVE/ReactiveGeneratorTests.FromReactivePropertiesWithIdenticalClass#TestNs1.TestVM.Properties.g.verified.cs

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

88
namespace TestNs1
99
{
10-
/// <summary>
10+
/// <summary>
1111
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
1212
/// </summary>
1313
public partial class TestVM

src/ReactiveUI.SourceGenerator.Tests/REACTIVE/ReactiveGeneratorTests.FromReactivePropertiesWithIdenticalClass#TestNs2.TestVM.Properties.g.verified.cs

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

88
namespace TestNs2
99
{
10-
/// <summary>
10+
/// <summary>
1111
/// Partial class for the TestVM which contains ReactiveUI Reactive property initialization.
1212
/// </summary>
1313
public partial class TestVM

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

0 commit comments

Comments
 (0)