Skip to content

Commit

Permalink
Feature: Wpf IViewFor Generator (#13)
Browse files Browse the repository at this point in the history
* Feature: Wpf IViewFor Generator

* Add Wpf to resolve usings
  • Loading branch information
ChrisPulman authored Aug 2, 2024
1 parent bf206c3 commit 48485a9
Show file tree
Hide file tree
Showing 14 changed files with 415 additions and 16 deletions.
18 changes: 13 additions & 5 deletions src/ReactiveUI.SourceGenerators.Execute/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public static class EntryPoint
/// <summary>
/// Defines the entry point of the application.
/// </summary>
public static void Main() => new TestClass();
public static void Main() => _ = TestViewModel.Instance;
}

/// <summary>
/// TestClass.
/// </summary>
[DataContract]
public partial class TestClass : ReactiveObject
public partial class TestViewModel : ReactiveObject
{
[JsonInclude]
[DataMember]
Expand All @@ -45,9 +45,9 @@ public partial class TestClass : ReactiveObject
private int _test1Property;

/// <summary>
/// Initializes a new instance of the <see cref="TestClass"/> class.
/// Initializes a new instance of the <see cref="TestViewModel"/> class.
/// </summary>
public TestClass()
public TestViewModel()
{
InitializeCommands();

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

Test8ObservableCommand?.Execute(100).Subscribe(Console.Out.WriteLine);
Console.Out.WriteLine($"Test2Property Value: {Test2Property}");
Console.Out.WriteLine($"Test2Property Value: {Test2}");
Console.Out.WriteLine($"Test2Property underlying Value: {_test2Property}");

Test9AsyncCommand?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
Expand All @@ -85,6 +85,14 @@ public TestClass()
Console.ReadLine();
}

/// <summary>
/// Gets the instance.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static TestViewModel Instance { get; } = new();

/// <summary>
/// Gets the can execute test1.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
Expand Down
25 changes: 25 additions & 0 deletions src/ReactiveUI.SourceGenerators.Execute/TestView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Windows;
using ReactiveUI;
using ReactiveUI.SourceGenerators;

namespace SGReactiveUI.SourceGenerators.Test;

/// <summary>
/// TestView.
/// </summary>
[IViewFor(nameof(TestViewModel))]
public partial class TestView : Window
{
/// <summary>
/// Initializes a new instance of the <see cref="TestView"/> class.
/// </summary>
public TestView()
{
ViewModel = TestViewModel.Instance;
}
}
21 changes: 21 additions & 0 deletions src/ReactiveUI.SourceGenerators/Attributes/IViewForAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System;

namespace ReactiveUI.SourceGenerators;

/// <summary>
/// ReactiveObjectAttribute.
/// </summary>
/// <seealso cref="System.Attribute" />
/// <remarks>
/// Initializes a new instance of the <see cref="IViewForAttribute"/> class.
/// </remarks>
/// <param name="viewModelType">Type of the view model.</param>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
#pragma warning disable CS9113 // Parameter is unread.
public sealed class IViewForAttribute(string? viewModelType) : Attribute;
#pragma warning restore CS9113 // Parameter is unread.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ public static bool InheritsFromFullyQualifiedMetadataName(this ITypeSymbol typeS
return false;
}

/// <summary>
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
/// </summary>
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
/// <param name="name">The full name of the type to check for inheritance.</param>
/// <returns>Whether or not <paramref name="typeSymbol"/> inherits from <paramref name="name"/>.</returns>
public static bool InheritsFromFullyQualifiedMetadataNameStartingWith(this ITypeSymbol typeSymbol, string name)
{
var baseType = typeSymbol.BaseType;

while (baseType is not null)
{
if (baseType.ContainsFullyQualifiedMetadataName(name))
{
return true;
}

baseType = baseType.BaseType;
}

return false;
}

/// <summary>
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
/// </summary>
Expand Down Expand Up @@ -188,6 +211,15 @@ public static bool HasFullyQualifiedMetadataName(this ITypeSymbol symbol, string
return builder.WrittenSpan.SequenceEqual(name.AsSpan());
}

public static bool ContainsFullyQualifiedMetadataName(this ITypeSymbol symbol, string name)
{
using var builder = ImmutableArrayBuilder<char>.Rent();

symbol.AppendFullyQualifiedMetadataName(builder);

return builder.WrittenSpan.ToString().Contains(name);
}

/// <summary>
/// Gets the fully qualified metadata name for a given <see cref="ITypeSymbol"/> instance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ReactiveUI.SourceGenerators.Extensions;
using ReactiveUI.SourceGenerators.Helpers;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace ReactiveUI.SourceGenerators.Models;
namespace ReactiveUI.SourceGenerators.Helpers;

/// <summary>
/// A model representing an attribute declaration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Operations;
using ReactiveUI.SourceGenerators.Helpers;

namespace ReactiveUI.SourceGenerators.Models;
namespace ReactiveUI.SourceGenerators.Helpers;

/// <inheritdoc/>
internal partial record TypedConstantInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using ReactiveUI.SourceGenerators.Helpers;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace ReactiveUI.SourceGenerators.Models;
namespace ReactiveUI.SourceGenerators.Helpers;

/// <summary>
/// A model representing a typed constant item.
Expand Down
Loading

0 comments on commit 48485a9

Please sign in to comment.