Skip to content

Commit 761a22c

Browse files
committed
Forward generator options to FAWMN
1 parent 603e81c commit 761a22c

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.CodeAnalysis;
1717
using Microsoft.CodeAnalysis.CSharp;
1818
using Microsoft.CodeAnalysis.CSharp.Syntax;
19+
using Microsoft.CodeAnalysis.Diagnostics;
1920
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
2021
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
2122

@@ -35,6 +36,7 @@ internal static class Execute
3536
/// <param name="fieldSyntax">The <see cref="FieldDeclarationSyntax"/> instance to process.</param>
3637
/// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
3738
/// <param name="semanticModel">The <see cref="SemanticModel"/> instance for the current run.</param>
39+
/// <param name="options">The options in use for the generator.</param>
3840
/// <param name="token">The cancellation token for the current operation.</param>
3941
/// <param name="propertyInfo">The resulting <see cref="PropertyInfo"/> value, if successfully retrieved.</param>
4042
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
@@ -43,6 +45,7 @@ public static bool TryGetInfo(
4345
FieldDeclarationSyntax fieldSyntax,
4446
IFieldSymbol fieldSymbol,
4547
SemanticModel semanticModel,
48+
AnalyzerConfigOptions options,
4649
CancellationToken token,
4750
[NotNullWhen(true)] out PropertyInfo? propertyInfo,
4851
out ImmutableArray<DiagnosticInfo> diagnostics)
@@ -66,6 +69,11 @@ public static bool TryGetInfo(
6669

6770
token.ThrowIfCancellationRequested();
6871

72+
// Override the property changing support if explicitly disabled
73+
shouldInvokeOnPropertyChanging &= GetEnableINotifyPropertyChangingSupport(options);
74+
75+
token.ThrowIfCancellationRequested();
76+
6977
// Get the property type and name
7078
string typeNameWithNullabilityAnnotations = fieldSymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations();
7179
string fieldName = fieldSymbol.Name;
@@ -320,6 +328,27 @@ public static bool TryGetInfo(
320328
return true;
321329
}
322330

331+
/// <summary>
332+
/// Gets the value for the "MvvmToolkitEnableINotifyPropertyChangingSupport" property.
333+
/// </summary>
334+
/// <param name="options">The options in use for the generator.</param>
335+
/// <returns>The value for the "MvvmToolkitEnableINotifyPropertyChangingSupport" property.</returns>
336+
public static bool GetEnableINotifyPropertyChangingSupport(AnalyzerConfigOptions options)
337+
{
338+
if (options.TryGetValue("build_property.MvvmToolkitEnableINotifyPropertyChangingSupport", out string? propertyValue))
339+
{
340+
if (bool.TryParse(propertyValue, out bool enableINotifyPropertyChangingSupport))
341+
{
342+
return enableINotifyPropertyChangingSupport;
343+
}
344+
}
345+
346+
// This setting is enabled by default, for backwards compatibility.
347+
// Note that this path should never be reached, as the default
348+
// value is also set in a .targets file bundled in the package.
349+
return true;
350+
}
351+
323352
/// <summary>
324353
/// Validates the containing type for a given field being annotated.
325354
/// </summary>

src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2525
{
2626
// Gather info for all annotated fields
2727
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<PropertyInfo?> Info)> propertyInfoWithErrors =
28-
context.SyntaxProvider
29-
.ForAttributeWithMetadataName(
28+
context.ForAttributeWithMetadataNameAndOptions(
3029
"CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute",
3130
static (node, _) => node is VariableDeclaratorSyntax { Parent: VariableDeclarationSyntax { Parent: FieldDeclarationSyntax { Parent: ClassDeclarationSyntax or RecordDeclarationSyntax, AttributeLists.Count: > 0 } } },
3231
static (context, token) =>
@@ -44,7 +43,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4443

4544
token.ThrowIfCancellationRequested();
4645

47-
_ = Execute.TryGetInfo(fieldDeclaration, fieldSymbol, context.SemanticModel, token, out PropertyInfo? propertyInfo, out ImmutableArray<DiagnosticInfo> diagnostics);
46+
_ = Execute.TryGetInfo(
47+
fieldDeclaration,
48+
fieldSymbol,
49+
context.SemanticModel,
50+
context.GlobalOptions,
51+
token,
52+
out PropertyInfo? propertyInfo,
53+
out ImmutableArray<DiagnosticInfo> diagnostics);
4854

4955
token.ThrowIfCancellationRequested();
5056

0 commit comments

Comments
 (0)