Skip to content

Commit

Permalink
Forward generator options to FAWMN
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Aug 20, 2024
1 parent 603e81c commit 761a22c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

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

token.ThrowIfCancellationRequested();

// Override the property changing support if explicitly disabled
shouldInvokeOnPropertyChanging &= GetEnableINotifyPropertyChangingSupport(options);

token.ThrowIfCancellationRequested();

// Get the property type and name
string typeNameWithNullabilityAnnotations = fieldSymbol.Type.GetFullyQualifiedNameWithNullabilityAnnotations();
string fieldName = fieldSymbol.Name;
Expand Down Expand Up @@ -320,6 +328,27 @@ public static bool TryGetInfo(
return true;
}

/// <summary>
/// Gets the value for the "MvvmToolkitEnableINotifyPropertyChangingSupport" property.
/// </summary>
/// <param name="options">The options in use for the generator.</param>
/// <returns>The value for the "MvvmToolkitEnableINotifyPropertyChangingSupport" property.</returns>
public static bool GetEnableINotifyPropertyChangingSupport(AnalyzerConfigOptions options)
{
if (options.TryGetValue("build_property.MvvmToolkitEnableINotifyPropertyChangingSupport", out string? propertyValue))
{
if (bool.TryParse(propertyValue, out bool enableINotifyPropertyChangingSupport))
{
return enableINotifyPropertyChangingSupport;
}
}

// This setting is enabled by default, for backwards compatibility.
// Note that this path should never be reached, as the default
// value is also set in a .targets file bundled in the package.
return true;
}

/// <summary>
/// Validates the containing type for a given field being annotated.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Gather info for all annotated fields
IncrementalValuesProvider<(HierarchyInfo Hierarchy, Result<PropertyInfo?> Info)> propertyInfoWithErrors =
context.SyntaxProvider
.ForAttributeWithMetadataName(
context.ForAttributeWithMetadataNameAndOptions(
"CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute",
static (node, _) => node is VariableDeclaratorSyntax { Parent: VariableDeclarationSyntax { Parent: FieldDeclarationSyntax { Parent: ClassDeclarationSyntax or RecordDeclarationSyntax, AttributeLists.Count: > 0 } } },
static (context, token) =>
Expand All @@ -44,7 +43,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
token.ThrowIfCancellationRequested();
_ = Execute.TryGetInfo(fieldDeclaration, fieldSymbol, context.SemanticModel, token, out PropertyInfo? propertyInfo, out ImmutableArray<DiagnosticInfo> diagnostics);
_ = Execute.TryGetInfo(
fieldDeclaration,
fieldSymbol,
context.SemanticModel,
context.GlobalOptions,
token,
out PropertyInfo? propertyInfo,
out ImmutableArray<DiagnosticInfo> diagnostics);
token.ThrowIfCancellationRequested();
Expand Down

0 comments on commit 761a22c

Please sign in to comment.