diff --git a/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs b/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs index 037dc98fa78..5d6e419b890 100644 --- a/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs +++ b/Microsoft.Toolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs @@ -434,7 +434,7 @@ private static PropertyDeclarationSyntax CreatePropertyDeclaration( AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))), AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))))) .AddAttributeLists(validationAttributes.Select(static a => AttributeList(SingletonSeparatedList(a))).ToArray()) - .WithLeadingTrivia(leadingTrivia) + .WithLeadingTrivia(leadingTrivia.Where(static trivia => !trivia.IsKind(SyntaxKind.RegionDirectiveTrivia) && !trivia.IsKind(SyntaxKind.EndRegionDirectiveTrivia))) .AddModifiers(Token(SyntaxKind.PublicKeyword)) .AddAccessorListAccessors( AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) diff --git a/UnitTests/UnitTests.NetCore/Mvvm/Test_ICommandAttribute.cs b/UnitTests/UnitTests.NetCore/Mvvm/Test_ICommandAttribute.cs index 4a5c377b613..b1e365974f7 100644 --- a/UnitTests/UnitTests.NetCore/Mvvm/Test_ICommandAttribute.cs +++ b/UnitTests/UnitTests.NetCore/Mvvm/Test_ICommandAttribute.cs @@ -8,6 +8,8 @@ using Microsoft.Toolkit.Mvvm.Input; using Microsoft.VisualStudio.TestTools.UnitTesting; +#pragma warning disable SA1124 + namespace UnitTests.Mvvm { [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1601", Justification = "Type only used for testing")] @@ -78,6 +80,8 @@ private async Task DelayAndIncrementCounterAsync() Counter += 1; } + #region Test region + /// /// This is multi line with also other stuff below /// @@ -100,6 +104,8 @@ private async Task DelayAndIncrementCounterWithValueAsync(int count) Counter += count; } + #endregion + [ICommand] private async Task DelayAndIncrementCounterWithValueAndTokenAsync(int count, CancellationToken token) { diff --git a/UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs b/UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs index f7c596ba438..3971e034bd3 100644 --- a/UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs +++ b/UnitTests/UnitTests.NetCore/Mvvm/Test_ObservablePropertyAttribute.cs @@ -11,6 +11,8 @@ using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.VisualStudio.TestTools.UnitTesting; +#pragma warning disable SA1124 + #nullable enable namespace UnitTests.Mvvm @@ -58,6 +60,86 @@ public void Test_ObservablePropertyAttribute_Events() Assert.AreEqual(changed.Item2, 42); } + // See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225 + [TestCategory("Mvvm")] + [TestMethod] + public void Test_ObservablePropertyAttributeWithinRegion_Events() + { + var model = new SampleModel(); + + (PropertyChangingEventArgs, int) changing = default; + (PropertyChangedEventArgs, int) changed = default; + + model.PropertyChanging += (s, e) => + { + Assert.IsNull(changing.Item1); + Assert.IsNull(changed.Item1); + Assert.AreSame(model, s); + Assert.IsNotNull(s); + Assert.IsNotNull(e); + + changing = (e, model.Counter); + }; + + model.PropertyChanged += (s, e) => + { + Assert.IsNotNull(changing.Item1); + Assert.IsNull(changed.Item1); + Assert.AreSame(model, s); + Assert.IsNotNull(s); + Assert.IsNotNull(e); + + changed = (e, model.Counter); + }; + + model.Counter = 42; + + Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Counter)); + Assert.AreEqual(changing.Item2, 0); + Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Counter)); + Assert.AreEqual(changed.Item2, 42); + } + + // See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225 + [TestCategory("Mvvm")] + [TestMethod] + public void Test_ObservablePropertyAttributeRightBelowRegion_Events() + { + var model = new SampleModel(); + + (PropertyChangingEventArgs, string?) changing = default; + (PropertyChangedEventArgs, string?) changed = default; + + model.PropertyChanging += (s, e) => + { + Assert.IsNull(changing.Item1); + Assert.IsNull(changed.Item1); + Assert.AreSame(model, s); + Assert.IsNotNull(s); + Assert.IsNotNull(e); + + changing = (e, model.Name); + }; + + model.PropertyChanged += (s, e) => + { + Assert.IsNotNull(changing.Item1); + Assert.IsNull(changed.Item1); + Assert.AreSame(model, s); + Assert.IsNotNull(s); + Assert.IsNotNull(e); + + changed = (e, model.Name); + }; + + model.Name = "Bob"; + + Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Name)); + Assert.AreEqual(changing.Item2, null); + Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Name)); + Assert.AreEqual(changed.Item2, "Bob"); + } + [TestCategory("Mvvm")] [TestMethod] public void Test_AlsoNotifyChangeForAttribute_Events() @@ -162,6 +244,16 @@ public partial class SampleModel : ObservableObject /// [ObservableProperty] private int data; + + #region More properties + + [ObservableProperty] + private int counter; + + #endregion + + [ObservableProperty] + private string? name; } [INotifyPropertyChanged]