Skip to content

Commit 48485a9

Browse files
authored
Feature: Wpf IViewFor Generator (#13)
* Feature: Wpf IViewFor Generator * Add Wpf to resolve usings
1 parent bf206c3 commit 48485a9

File tree

14 files changed

+415
-16
lines changed

14 files changed

+415
-16
lines changed

src/ReactiveUI.SourceGenerators.Execute/Program.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public static class EntryPoint
2525
/// <summary>
2626
/// Defines the entry point of the application.
2727
/// </summary>
28-
public static void Main() => new TestClass();
28+
public static void Main() => _ = TestViewModel.Instance;
2929
}
3030

3131
/// <summary>
3232
/// TestClass.
3333
/// </summary>
3434
[DataContract]
35-
public partial class TestClass : ReactiveObject
35+
public partial class TestViewModel : ReactiveObject
3636
{
3737
[JsonInclude]
3838
[DataMember]
@@ -45,9 +45,9 @@ public partial class TestClass : ReactiveObject
4545
private int _test1Property;
4646

4747
/// <summary>
48-
/// Initializes a new instance of the <see cref="TestClass"/> class.
48+
/// Initializes a new instance of the <see cref="TestViewModel"/> class.
4949
/// </summary>
50-
public TestClass()
50+
public TestViewModel()
5151
{
5252
InitializeCommands();
5353

@@ -72,7 +72,7 @@ public TestClass()
7272
_test2PropertyHelper = Test8ObservableCommand!.ToProperty(this, x => x.Test2Property);
7373

7474
Test8ObservableCommand?.Execute(100).Subscribe(Console.Out.WriteLine);
75-
Console.Out.WriteLine($"Test2Property Value: {Test2Property}");
75+
Console.Out.WriteLine($"Test2Property Value: {Test2}");
7676
Console.Out.WriteLine($"Test2Property underlying Value: {_test2Property}");
7777

7878
Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
@@ -85,6 +85,14 @@ public TestClass()
8585
Console.ReadLine();
8686
}
8787

88+
/// <summary>
89+
/// Gets the instance.
90+
/// </summary>
91+
/// <value>
92+
/// The instance.
93+
/// </value>
94+
public static TestViewModel Instance { get; } = new();
95+
8896
/// <summary>
8997
/// Gets the can execute test1.
9098
/// </summary>

src/ReactiveUI.SourceGenerators.Execute/ReactiveUI.SourceGenerators.Execute.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net8.0-windows</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<UseWPF>true</UseWPF>
78
<Nullable>enable</Nullable>
89
<IsPackable>false</IsPackable>
910
<LangVersion>latest</LangVersion>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
using System.Windows;
7+
using ReactiveUI;
8+
using ReactiveUI.SourceGenerators;
9+
10+
namespace SGReactiveUI.SourceGenerators.Test;
11+
12+
/// <summary>
13+
/// TestView.
14+
/// </summary>
15+
[IViewFor(nameof(TestViewModel))]
16+
public partial class TestView : Window
17+
{
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="TestView"/> class.
20+
/// </summary>
21+
public TestView()
22+
{
23+
ViewModel = TestViewModel.Instance;
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
// See the LICENSE file in the project root for full license information.
5+
6+
using System;
7+
8+
namespace ReactiveUI.SourceGenerators;
9+
10+
/// <summary>
11+
/// ReactiveObjectAttribute.
12+
/// </summary>
13+
/// <seealso cref="System.Attribute" />
14+
/// <remarks>
15+
/// Initializes a new instance of the <see cref="IViewForAttribute"/> class.
16+
/// </remarks>
17+
/// <param name="viewModelType">Type of the view model.</param>
18+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
19+
#pragma warning disable CS9113 // Parameter is unread.
20+
public sealed class IViewForAttribute(string? viewModelType) : Attribute;
21+
#pragma warning restore CS9113 // Parameter is unread.

src/ReactiveUI.SourceGenerators/Extensions/ITypeSymbolExtensions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ public static bool InheritsFromFullyQualifiedMetadataName(this ITypeSymbol typeS
5757
return false;
5858
}
5959

60+
/// <summary>
61+
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
62+
/// </summary>
63+
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
64+
/// <param name="name">The full name of the type to check for inheritance.</param>
65+
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns>
66+
public static bool InheritsFromFullyQualifiedMetadataNameStartingWith(this ITypeSymbol typeSymbol, string name)
67+
{
68+
var baseType = typeSymbol.BaseType;
69+
70+
while (baseType is not null)
71+
{
72+
if (baseType.ContainsFullyQualifiedMetadataName(name))
73+
{
74+
return true;
75+
}
76+
77+
baseType = baseType.BaseType;
78+
}
79+
80+
return false;
81+
}
82+
6083
/// <summary>
6184
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
6285
/// </summary>
@@ -188,6 +211,15 @@ public static bool HasFullyQualifiedMetadataName(this ITypeSymbol symbol, string
188211
return builder.WrittenSpan.SequenceEqual(name.AsSpan());
189212
}
190213

214+
public static bool ContainsFullyQualifiedMetadataName(this ITypeSymbol symbol, string name)
215+
{
216+
using var builder = ImmutableArrayBuilder<char>.Rent();
217+
218+
symbol.AppendFullyQualifiedMetadataName(builder);
219+
220+
return builder.WrittenSpan.ToString().Contains(name);
221+
}
222+
191223
/// <summary>
192224
/// Gets the fully qualified metadata name for a given <see cref="ITypeSymbol"/> instance.
193225
/// </summary>

src/ReactiveUI.SourceGenerators/Reactive/Models/AttributeInfo.cs renamed to src/ReactiveUI.SourceGenerators/Helpers/AttributeInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
using Microsoft.CodeAnalysis;
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
1212
using ReactiveUI.SourceGenerators.Extensions;
13-
using ReactiveUI.SourceGenerators.Helpers;
1413
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1514

16-
namespace ReactiveUI.SourceGenerators.Models;
15+
namespace ReactiveUI.SourceGenerators.Helpers;
1716

1817
/// <summary>
1918
/// A model representing an attribute declaration.

src/ReactiveUI.SourceGenerators/Reactive/Models/TypedConstantInfo.Factory.cs renamed to src/ReactiveUI.SourceGenerators/Helpers/TypedConstantInfo.Factory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
using Microsoft.CodeAnalysis;
1212
using Microsoft.CodeAnalysis.CSharp.Syntax;
1313
using Microsoft.CodeAnalysis.Operations;
14-
using ReactiveUI.SourceGenerators.Helpers;
1514

16-
namespace ReactiveUI.SourceGenerators.Models;
15+
namespace ReactiveUI.SourceGenerators.Helpers;
1716

1817
/// <inheritdoc/>
1918
internal partial record TypedConstantInfo

src/ReactiveUI.SourceGenerators/Reactive/Models/TypedConstantInfo.cs renamed to src/ReactiveUI.SourceGenerators/Helpers/TypedConstantInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
using Microsoft.CodeAnalysis;
1010
using Microsoft.CodeAnalysis.CSharp;
1111
using Microsoft.CodeAnalysis.CSharp.Syntax;
12-
using ReactiveUI.SourceGenerators.Helpers;
1312
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1413

15-
namespace ReactiveUI.SourceGenerators.Models;
14+
namespace ReactiveUI.SourceGenerators.Helpers;
1615

1716
/// <summary>
1817
/// A model representing a typed constant item.

0 commit comments

Comments
 (0)