Skip to content
Merged
  •  
  •  
  •  
46 changes: 40 additions & 6 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,10 @@ yet, so they sit in its `AnalyzerReleases.Unshipped.md`.
`BindCommand`), because one generator cannot see another's output.
- Generated output compiles at C# 12 and uses block-scoped namespaces.
- The attribute generators use these test projects: `tests/ReactiveUI.SourceGenerators.Tests` (snapshots, with a folder
per generator), `tests/ReactiveUI.SourceGenerators.V23.Tests` (the same generators against ReactiveUI 23) and
`tests/ReactiveUI.SourceGenerators.Execute*` (code that compiles and runs against generated output). The SG tests pin
ReactiveUI 24.3.0 where they need `ReactiveUI.IViewFor`.
per generator) and `tests/ReactiveUI.SourceGenerators.Execute*` (code that compiles and runs against generated
output).
- The attribute generators support ReactiveUI 26 and later, which takes its view API from ReactiveUI.Binding. They carry
no branch for an older ReactiveUI.

## Build & Test Commands

Expand Down Expand Up @@ -717,9 +718,9 @@ gets the same veto. That is the "better" half of the divergence: same outcome, n
types it can see at compile time, but `ICreatesObservableForProperty` is registered at run time and the
highest affinity wins - which is how ReactiveUI resolves the observation behind `Bind` and `OneWayBind`, not
just behind `WhenChanged`. The choice is made per observed link rather than per binding:
`ObservationAffinityChecker.FindHigherAffinityPlugin` is asked for a registration outranking the affinity
that link was generated from, and the link observes through `PluginPropertyObservable` when one wins and
through the generated mechanism when none does. A two-way binding observes both sides, so either side's
`PluginObservationSource.Choose` asks for a registration outranking the affinity that link was generated
from, and the link observes through `PluginPropertyObservable` when one wins and through the generated
mechanism when none does. A two-way binding observes both sides, so either side's
registration takes that side.

**The registration drives the observation without an expression engine.** `PluginPropertyObservable`
Expand Down Expand Up @@ -1134,6 +1135,7 @@ Not all platforms support before-change notifications (WPF DP, WinUI DP, WinForm
- **Indentation:** 4 spaces, no tabs
- **Fields:** `_camelCase` for private/internal
- **Visibility:** Always explicit, visibility first modifier
- **DebuggerDisplay:** The text starts with the type's name, e.g. `[DebuggerDisplay("NamespacePath: {Prefix,nq}")]`
- **Namespaces:** File-scoped preferred
- **Modern C#:** Nullable reference types, pattern matching, records, init setters
- **netstandard2.0 targets:** Use `IsExternalInit.cs` polyfill for records; avoid APIs not available in netstandard2.0 (e.g., use `if (x is null) throw new System.ArgumentNullException(...)` instead of `ArgumentNullException.ThrowIfNull()`)
Expand Down Expand Up @@ -1207,6 +1209,9 @@ All pipeline models are `sealed record` types with value equality. NEVER include
- Emitters write through `SourceWriter`, not `SyntaxFactory` and not a raw `StringBuilder`
- Generated code emitted as C# source via `context.AddSource()`
- `#pragma warning disable` at top of generated files
- `#nullable enable annotations` and `#nullable disable warnings` follow it from C# 8. The annotations directive
alone leaves nullable warnings as the project set them, and csc then runs nullable flow analysis over every
generated method. That analysis was about a fifth of the time csc spent compiling the generated code
- All generated types use `[Microsoft.CodeAnalysis.Embedded]` attribute

### Writing Generated Code
Expand Down Expand Up @@ -1259,6 +1264,35 @@ types by one name.
share (a consumer's root namespace, or the runtime's own below C# 10), its class name carries the assembly:
`__ReactiveUIGeneratedBindings_<Assembly>`.

### Generated Bodies Call Runtime Helpers

A binding's generated method states only what differs per call site: the types, the member names and the
accessors. The steps that are the same for every binding are public runtime methods, and generated code calls
them. csc binds and emits every generated line on every build, so a fixed block written out per call site costs
the consumer compile time for nothing.

| Step | Runtime helper | What generated code passes |
|------|----------------|----------------------------|
| Ask the binding hooks | `BindingHooks.ShouldBind` with `BindingHookProperty[]` paths | One reader and one expression factory per property |
| Offer a link to a registered provider | `PluginObservationSource.Choose` and `ChooseDistinct` | The property name, the affinity, an accessor, an expression factory and the generated observation |
| Convert a value | `GeneratedConversion.Convert` | The generated conversion as a lambda over the value and the hint, and its affinity |
| Offer a command binding to a registered binder | `CommandBinderService.BindCustom` | The control, the command stream, the typed parameter stream and the affinity |
| Ask the service locator for a view | `DefaultViewLocator.GetRegisteredView<TViewModel>` | The contract; the generated resolver adds only how the view is constructed |

- Every lambda generated code passes takes its input as an argument and captures nothing, so the compiler caches
it as one static delegate.
- An expression tree is passed as a factory (`Func<Expression>`). The runtime calls it only when a hook reads the
path or a registered provider wins the link.
- A registered `IPropertyBindingHook` or `ICreatesCommandBinding` receives values as `object`, which boxes value
types. A hook that implements `ITypedPropertyBindingHook` is handed a `BindingHookPath` and visits each property in
its own types. A binder that implements `ICreatesTypedCommandBinding` is handed the parameter stream typed. The
helpers call the typed member when the registration has one.
- A new fixed block belongs in a runtime helper, not in an emitter. Measure the lines a snapshot gains before you
add one to an emitter.
- Line count is not the whole cost. In a csc profile of generated bindings, binding invocations took about half
of the method compile time. Profile the compiler on real generated output before you assume that fewer lines
means a faster build.

### Two-Layer Language Version Constraint

There are **two distinct C# language contexts** in this project:
Expand Down
18 changes: 10 additions & 8 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

<PropertyGroup>
<RoslynCommonAnalyzersVersion>5.2.0</RoslynCommonAnalyzersVersion>
<ReactiveUIPrimitivesVersion>10.0.0</ReactiveUIPrimitivesVersion>
<ReactiveUIPrimitivesVersion>10.2.2</ReactiveUIPrimitivesVersion>
</PropertyGroup>

<ItemGroup>
<!-- Testing Framework. The tests run on Microsoft Testing Platform, so do not add Microsoft.NET.Test.Sdk. -->
<PackageVersion Include="TUnit" Version="1.85.0"/>
<PackageVersion Include="TUnit" Version="1.87.12"/>

<!-- Roslyn Framework -->
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="5.12.0"/>
Expand Down Expand Up @@ -75,26 +75,28 @@

<!-- Uno Platform. Uno.WinUI matches the project SDK; the Windows head uses the App SDK. -->
<PackageVersion Include="Uno.WinUI" Version="7.0.65"/>
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="2.6.5"/>
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="2.7.0"/>
<PackageVersion Include="ReactiveUI.Primitives.Uno" Version="$(ReactiveUIPrimitivesVersion)"/>
<PackageVersion Include="ReactiveUI.Primitives.Uno.Reactive" Version="$(ReactiveUIPrimitivesVersion)"/>

<!-- Test Infrastructure for the attribute generators -->
<PackageVersion Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="4.0.0"/>
<!-- No project references TraceEvent directly: BenchmarkDotNet brings it in, and this pin raises its version
for the allocation-trace readers in the benchmark projects. -->
<PackageVersion Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="4.0.2"/>
<PackageVersion Include="DynamicData" Version="9.5.0"/>

<!-- Avalonia sample app -->
<PackageVersion Include="Avalonia.Themes.Fluent" Version="12.1.6"/>
<PackageVersion Include="Avalonia.Fonts.Inter" Version="12.1.6"/>
<PackageVersion Include="Avalonia.Diagnostics" Version="11.3.23"/>
<PackageVersion Include="Avalonia.Desktop" Version="12.1.6"/>
<PackageVersion Include="ReactiveUI.Avalonia" Version="12.1.6"/>
<PackageVersion Include="ReactiveUI.Avalonia" Version="12.1.7"/>
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="11.0.0" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net11.0'))"/>
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="10.0.15" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net11.0'))"/>

<!-- Test App Dependencies-->
<PackageVersion Include="ReactiveUI" Version="26.0.1"/>
<PackageVersion Include="ReactiveUI.Reactive" Version="26.0.1"/>
<PackageVersion Include="ReactiveUI.Maui" Version="26.0.1"/>
<PackageVersion Include="ReactiveUI" Version="27.0.1"/>
<PackageVersion Include="ReactiveUI.Reactive" Version="27.0.1"/>
<PackageVersion Include="ReactiveUI.Maui" Version="27.0.1"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,25 @@ namespace ReactiveUI.Binding.Reactive
public void Register(ReactiveUI.Binding.Reactive.IBindingFallbackConverter converter) { }
public ReactiveUI.Binding.Reactive.IBindingFallbackConverter? TryGetConverter(System.Type fromType, System.Type toType) { }
}
[System.Diagnostics.DebuggerDisplay("BindingHookPath: {Count} properties")]
public sealed class BindingHookPath
{
public int Count { get; }
public void Accept(ReactiveUI.Binding.Reactive.IBindingHookPropertyVisitor visitor) { }
}
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.DebuggerDisplay("BindingHookProperty")]
public sealed class BindingHookProperty
{
public static ReactiveUI.Binding.Reactive.BindingHookProperty Create<TOwner, TValue>(System.Func<TOwner, TValue> read, System.Func<System.Linq.Expressions.Expression<System.Func<TOwner, TValue>>> expression) { }
}
public static class BindingHooks
{
public static bool Any { get; }
public static void Refresh() { }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static bool ShouldBind(object? source, object target, ReactiveUI.Binding.Reactive.BindingHookProperty[] sourcePath, ReactiveUI.Binding.Reactive.BindingHookProperty[] targetPath, ReactiveUI.Binding.Reactive.BindingDirection direction) { }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static bool ShouldBind(object? source, object target, System.Func<ReactiveUI.Binding.Reactive.IObservedChange<object, object>[]> getSourceProperties, System.Func<ReactiveUI.Binding.Reactive.IObservedChange<object, object>[]> getTargetProperties, ReactiveUI.Binding.Reactive.BindingDirection direction) { }
}
public static class BindingSchedulers
Expand Down Expand Up @@ -222,6 +236,8 @@ namespace ReactiveUI.Binding.Reactive
public bool Unmap<TViewModel>() where TViewModel : class { }
public bool Unmap<TViewModel>(string? contract) where TViewModel : class { }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static ReactiveUI.Binding.Reactive.IViewFor? GetRegisteredView<TViewModel>(string contract) where TViewModel : class { }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static void SetGeneratedViewDispatch(System.Func<object, string, ReactiveUI.Binding.Reactive.IViewFor?> dispatch) { }
}
[System.Diagnostics.DebuggerDisplay("DoubleToNullableDoubleTypeConverter: double -> double? (affinity {Affinity})")]
Expand Down Expand Up @@ -291,6 +307,10 @@ namespace ReactiveUI.Binding.Reactive
int GetAffinityForObjects(System.Type fromType, System.Type toType) { }
bool TryConvert(System.Type fromType, object from, System.Type toType, object? conversionHint, out object? result) { }
}
public interface IBindingHookPropertyVisitor
{
void Visit<TOwner, TValue>(TOwner owner, System.Linq.Expressions.Expression<System.Func<TOwner, TValue>> property, TValue value) { }
}
public interface IBindingTypeConverter : Splat.IEnableLogger
{
System.Type FromType { get; }
Expand All @@ -314,6 +334,11 @@ namespace ReactiveUI.Binding.Reactive
int GetAffinityForObject(System.Type type, string propertyName, bool beforeChanged) { }
System.IObservable<ReactiveUI.Binding.Reactive.IObservedChange<object, object?>> GetNotificationForProperty(object sender, System.Linq.Expressions.Expression expression, string propertyName, bool beforeChanged, bool suppressWarnings) { }
}
public interface ICreatesTypedCommandBinding : ReactiveUI.Binding.Reactive.ICreatesCommandBinding
{
System.IDisposable? BindTypedCommandToObject<T, TParameter>(System.Windows.Input.ICommand? command, T? target, System.IObservable<TParameter> commandParameter) where T : class { }
System.IDisposable? BindTypedCommandToObject<T, TParameter, TEventArgs>(System.Windows.Input.ICommand? command, T? target, System.IObservable<TParameter> commandParameter, string eventName) where T : class { }
}
public interface IInteraction<TInput, TOutput>
{
System.IObservable<TOutput> Handle(TInput input) { }
Expand Down Expand Up @@ -354,6 +379,10 @@ namespace ReactiveUI.Binding.Reactive
int GetAffinityForObjects(System.Type? fromType, System.Type? toType) { }
object? PerformSet(object? toTarget, object? newValue, object?[]? arguments) { }
}
public interface ITypedPropertyBindingHook : ReactiveUI.Binding.Reactive.IPropertyBindingHook
{
bool ExecuteHook(object? source, object target, ReactiveUI.Binding.Reactive.BindingHookPath sourcePath, ReactiveUI.Binding.Reactive.BindingHookPath targetPath, ReactiveUI.Binding.Reactive.BindingDirection direction) { }
}
public interface IViewFor : ReactiveUI.Binding.Reactive.IActivatableView
{
object? ViewModel { get; set; }
Expand Down Expand Up @@ -650,7 +679,7 @@ namespace ReactiveUI.Binding.Reactive
New = 3,
}
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.DebuggerDisplay("{Owner}.{PropertyName}")]
[System.Diagnostics.DebuggerDisplay("ObservablePropertyTarget: {Owner}.{PropertyName}")]
public readonly struct ObservablePropertyTarget : System.IEquatable<ReactiveUI.Binding.Reactive.ObservablePropertyTarget>
{
public ObservablePropertyTarget(object owner, string propertyName) { }
Expand Down Expand Up @@ -2069,6 +2098,8 @@ namespace ReactiveUI.Binding.Reactive.CommandBinding
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static class CommandBinderService
{
public static System.IDisposable? BindCustom<TControl, TParameter>(TControl control, System.IObservable<System.Windows.Input.ICommand?> commands, System.IObservable<TParameter>? parameter, int generatedAffinity) where TControl : class { }
public static System.IDisposable? BindCustom<TControl, TParameter, TEventArgs>(TControl control, System.IObservable<System.Windows.Input.ICommand?> commands, System.IObservable<TParameter>? parameter, int generatedAffinity, string eventName) where TControl : class { }
public static ReactiveUI.Binding.Reactive.ICreatesCommandBinding? GetBinder<T>(bool hasEventTarget) { }
}
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
Expand Down Expand Up @@ -2119,6 +2150,11 @@ namespace ReactiveUI.Binding.Reactive.Fallback
public static bool HasHigherAffinityPlugin<T>(int generatedAffinity, bool hasEventTarget) { }
}
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static class GeneratedConversion
{
public static System.IObservable<TTo> Convert<TFrom, TTo>(System.IObservable<TFrom> source, ReactiveUI.Binding.Reactive.IBindingTypeConverter? converterOverride, object? conversionHint, int generatedAffinity, bool isIdentity, System.Func<TFrom, object?, (bool Success, TTo Value)> convert, System.Func<TFrom, TTo>? assignmentFallback) { }
}
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static class ObservationAffinityChecker
{
public static ReactiveUI.Binding.Reactive.ICreatesObservableForProperty? FindHigherAffinityPlugin(System.Type type, string propertyName, int generatedAffinity, bool beforeChanged) { }
Expand Down Expand Up @@ -2388,6 +2424,8 @@ namespace ReactiveUI.Binding.Reactive.Observables
public static class PluginObservationSource
{
public static System.IObservable<T> Choose<T>(object source, System.Linq.Expressions.Expression expression, string propertyName, bool beforeChange, int generatedAffinity, System.Func<object, T?> getter, System.IObservable<T> generated) { }
public static System.IObservable<T> Choose<TOwner, T>(TOwner source, string propertyName, int generatedAffinity, bool beforeChange, System.Func<TOwner, T?> getter, System.Func<System.Linq.Expressions.Expression<System.Func<TOwner, T>>> expression, System.IObservable<T> generated) where TOwner : notnull { }
public static System.IObservable<T> ChooseDistinct<TOwner, T>(TOwner source, string propertyName, int generatedAffinity, bool beforeChange, System.Func<TOwner, T?> getter, System.Func<System.Linq.Expressions.Expression<System.Func<TOwner, T>>> expression, System.IObservable<T> generated) where TOwner : notnull { }
}
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[System.Diagnostics.DebuggerDisplay("PluginPropertyObservable: Property = {_propertyName}, Source = {_source}, BeforeChange = {_beforeChange}")]
Expand Down
Loading
Loading