Skip to content

Commit

Permalink
Update ReactiveGenerator.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Nov 20, 2024
1 parent 39de904 commit 7f57ce1
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions ReactiveGenerator/ReactiveGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
ctx.AddSource("ReactiveAttribute.g.cs", SourceText.From(AttributeSource, Encoding.UTF8));
});


// Get MSBuild property for enabling legacy mode
var useLegacyMode = context.AnalyzerConfigOptionsProvider
.Select((provider, _) => bool.TryParse(
provider.GlobalOptions.TryGetValue("build_property.UseBackingFields", out var value) ? value : "false",
provider.GlobalOptions.TryGetValue("build_property.UseBackingFields", out var value)
? value
: "false",
out var result) && result);

// Get property declarations
Expand All @@ -48,8 +50,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
source.Right,
spc));
}
private static bool IsCandidateProperty(SyntaxNode node)

private static bool IsCandidateProperty(SyntaxNode node)
{
// Same as before
if (node is not PropertyDeclarationSyntax propertyDeclaration)
Expand Down Expand Up @@ -84,27 +86,28 @@ private static bool IsCandidateProperty(SyntaxNode node)

return null;
}

private static bool InheritsFromReactiveObject(INamedTypeSymbol typeSymbol)
{
var current = typeSymbol;
while (current is not null)
{
if (current.Name == "ReactiveObject" &&
if (current.Name == "ReactiveObject" &&
current.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat).StartsWith("global::ReactiveUI."))
return true;
current = current.BaseType;
}

return false;
}

private static bool HasReactivePropertiesInHierarchy(INamedTypeSymbol typeSymbol)
{
foreach (var member in typeSymbol.GetMembers().OfType<IPropertySymbol>())
{
if (member.GetAttributes().Any(attr =>
attr.AttributeClass is not null &&
(attr.AttributeClass.Name is "ReactiveAttribute" or "Reactive")))
if (member.GetAttributes().Any(attr =>
attr.AttributeClass is not null &&
(attr.AttributeClass.Name is "ReactiveAttribute" or "Reactive")))
{
return true;
}
Expand All @@ -130,7 +133,7 @@ attr.AttributeClass is not null &&
return null;
}

if (current.BaseType is null ||
if (current.BaseType is null ||
!HasReactivePropertiesInHierarchy(current.BaseType))
{
return current;
Expand Down Expand Up @@ -205,7 +208,7 @@ private static string GetAccessorAccessibility(IMethodSymbol? accessor)
};
}

private static string GenerateClassSource(
private static string GenerateClassSource(
INamedTypeSymbol classSymbol,
List<IPropertySymbol> allProperties,
bool implementInpc,
Expand Down Expand Up @@ -238,6 +241,7 @@ private static string GenerateClassSource(
sb.AppendLine("using System.ComponentModel;");
sb.AppendLine("using System.Runtime.CompilerServices;");
}

sb.AppendLine();

if (namespaceName != null)
Expand All @@ -257,7 +261,8 @@ private static string GenerateClassSource(
{
var propertyName = property.Name;
var fieldName = GetEventArgsFieldName(propertyName);
sb.AppendLine($" private static readonly PropertyChangedEventArgs {fieldName} = new PropertyChangedEventArgs(nameof({propertyName}));");
sb.AppendLine(
$" private static readonly PropertyChangedEventArgs {fieldName} = new PropertyChangedEventArgs(nameof({propertyName}));");
}

if (typeProperties.Any())
Expand All @@ -269,7 +274,8 @@ private static string GenerateClassSource(
sb.AppendLine(" public event PropertyChangedEventHandler? PropertyChanged;");
sb.AppendLine();

sb.AppendLine(" protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)");
sb.AppendLine(
" protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)");
sb.AppendLine(" {");
sb.AppendLine(" PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));");
sb.AppendLine(" }");
Expand Down Expand Up @@ -320,22 +326,22 @@ private static string GenerateClassSource(

return sb.ToString();
}

private static string GetPropertyTypeWithNullability(IPropertySymbol property)
{
var nullableAnnotation = property.NullableAnnotation;
var baseType = property.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);

// If the type is a reference type and it's nullable, add the ? annotation
if (nullableAnnotation == NullableAnnotation.Annotated &&
if (nullableAnnotation == NullableAnnotation.Annotated &&
!property.Type.IsValueType)
{
return baseType + "?";
}

return baseType;
}

private static void GenerateLegacyProperty(
StringBuilder sb,
IPropertySymbol property,
Expand All @@ -359,7 +365,8 @@ private static void GenerateLegacyProperty(
sb.AppendLine($" {getterModifier}get => {backingFieldName};");
if (property.SetMethod != null)
{
sb.AppendLine($" {setterModifier}set => this.RaiseAndSetIfChanged(ref {backingFieldName}, value);");
sb.AppendLine(
$" {setterModifier}set => this.RaiseAndSetIfChanged(ref {backingFieldName}, value);");
}
}
else
Expand Down Expand Up @@ -434,7 +441,7 @@ private static void GenerateFieldKeywordProperty(

sb.AppendLine(" }");
}

private static string GetBackingFieldName(string propertyName)
{
return "_" + char.ToLowerInvariant(propertyName[0]) + propertyName.Substring(1);
Expand Down

0 comments on commit 7f57ce1

Please sign in to comment.