Skip to content

Commit

Permalink
Add property accessibility
Browse files Browse the repository at this point in the history
  • Loading branch information
rrmanzano committed Feb 6, 2023
1 parent c4f96a8 commit b0889b3
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ private bool ValidateIsNullOrEmpty(BindableObject _, string? value)
[AutoBindable]
private readonly string? _displayName;

[AutoBindable(PropertyAccessibility = BindablePropertyAccessibility.ProtectedOrInternal)]
private readonly string? _age;

[AutoBindable(HidesUnderlyingProperty = true, DefaultValue = "Color.FromArgb(\"#cc3340\")")]
private readonly Color _backgroundColor = Color.FromArgb("#cc3340");

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
using Maui.BindableProperty.Generator.Core;

namespace Maui.BindableProperty.Generator.Demo.CustomControls
namespace Maui.BindableProperty.Generator.Demo.CustomControls;

public partial class HeaderGenericControl<T> : VerticalStackLayout
{
public partial class HeaderGenericControl<T> : VerticalStackLayout
{
#pragma warning disable CS0169
#pragma warning disable CS0169

[AutoBindable]
private bool _isGenerated;
[AutoBindable]
private bool _isGenerated;

[AutoBindable(OnChanged = nameof(UpdateGenericSample))]
private readonly T _genericSample = default!;
[AutoBindable(OnChanged = nameof(UpdateGenericSample))]
private readonly T _genericSample = default!;

#pragma warning restore CS0169
#pragma warning restore CS0169

public HeaderGenericControl()
{
this.IsGenerated = false;
}
public HeaderGenericControl()
{
this.IsGenerated = false;
}

private void UpdateGenericSample()
{
// Do stuff here
}
private void UpdateGenericSample()
{
// Do stuff here
}

private void UpdateGenericSample(T oldValue, T newValue)
{
// Do stuff here
}
private void UpdateGenericSample(T oldValue, T newValue)
{
// Do stuff here
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ public class AutoBindableConstants
public const string AttrDefaultBindingMode = "DefaultBindingMode";

public const string AttrHidesUnderlyingProperty = "HidesUnderlyingProperty";

public const string AttrPropertyAccessibility = "PropertyAccessibility";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Maui.BindableProperty.Generator.Helpers;
using Maui.BindableProperty.Generator.Core.BindableProperty.Implementation;
using Maui.BindableProperty.Generator.Core.BindableProperty.Implementation.Interfaces;
using Microsoft.CodeAnalysis.CSharp;

namespace Maui.BindableProperty.Generator.Core.BindableProperty;

Expand All @@ -25,6 +26,44 @@ public class AutoBindablePropertyGenerator : ISourceGenerator
using System;
namespace Maui.BindableProperty.Generator.Core
{
public enum BindablePropertyAccessibility
{
/// <summary>
/// If 'Undefined', bindable property will be defined in the same way as the class that contains it.
/// </summary>
Undefined = 0,
/// <summary>
/// Bindable property will be defined as 'private'
/// </summary>
Private = 1,
/// <summary>
/// Bindable property will be defined as 'private protected'
/// </summary>
ProtectedAndInternal = 2,
/// <summary>
/// Bindable property will be defined as 'protected'
/// </summary>
Protected = 3,
/// <summary>
/// Bindable property will be defined as 'internal'
/// </summary>
Internal = 4,
/// <summary>
/// Bindable property will be defined as 'protected internal'
/// </summary>
ProtectedOrInternal = 5,
/// <summary>
/// Bindable property will be defined as 'public'
/// </summary>
Public = 6
}
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
[System.Diagnostics.Conditional(""AutoBindableGenerator_DEBUG"")]
public sealed class AutoBindableAttribute : Attribute
Expand All @@ -42,6 +81,8 @@ public AutoBindableAttribute(){}
public string? ValidateValue { get; set; }
public bool HidesUnderlyingProperty { get; set; } = false;
public BindablePropertyAccessibility PropertyAccessibility { get; set; } = BindablePropertyAccessibility.Undefined;
}
}";

Expand Down Expand Up @@ -107,7 +148,8 @@ private string ProcessClass(INamedTypeSymbol classSymbol, List<IFieldSymbol> fie
w._("#nullable enable");
using (w.B(@$"namespace {namespaceName}"))
{
using (w.B(@$"public partial class {classSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}"))
var classAccessibility = SyntaxFacts.GetText(classSymbol.DeclaredAccessibility);
using (w.B(@$"{classAccessibility} partial class {classSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}"))
{
// Create properties for each field
foreach (IFieldSymbol fieldSymbol in fields)
Expand Down Expand Up @@ -182,9 +224,14 @@ private void ProcessBindableProperty(
var hidesUnderlying = applyHidesUnderlying ? " new" : string.Empty;
var declaringType = fieldType.WithNullableAnnotation(NullableAnnotation.None);
var parameters = $"nameof({propertyName}),typeof({declaringType.ToDisplayString(CommonSymbolDisplayFormat.DefaultFormat)}),typeof({classSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}){customParameters}".Split(',');
var propertyDeclaredAccessibility = fieldSymbol.GetValue<int>(attributeSymbol, AutoBindableConstants.AttrPropertyAccessibility);
var propertyAccessibility =
propertyDeclaredAccessibility == 0
? SyntaxFacts.GetText(classSymbol.DeclaredAccessibility)
: SyntaxFacts.GetText((Accessibility)propertyDeclaredAccessibility);

w._(AttributeBuilder.GetAttrGeneratedCodeString());
w._($@"public static{hidesUnderlying} readonly {AutoBindableConstants.FullNameMauiControls}.BindableProperty {bindablePropertyName} =");
w._($@"{propertyAccessibility} static{hidesUnderlying} readonly {AutoBindableConstants.FullNameMauiControls}.BindableProperty {bindablePropertyName} =");
w._($"{w.GetIndentString(6)}{AutoBindableConstants.FullNameMauiControls}.BindableProperty.Create(");

for (int i = 0; i < parameters.Length; i++)
Expand All @@ -196,7 +243,7 @@ private void ProcessBindableProperty(

w._();
AttributeBuilder.WriteAllAttrGeneratedCodeStrings(w);
using (w.B(@$"public{hidesUnderlying} {fieldType.ToDisplayString(CommonSymbolDisplayFormat.DefaultFormat)} {propertyName}"))
using (w.B(@$"{propertyAccessibility}{hidesUnderlying} {fieldType.ToDisplayString(CommonSymbolDisplayFormat.DefaultFormat)} {propertyName}"))
{
w._($@"get => ({fieldType.ToDisplayString(CommonSymbolDisplayFormat.DefaultFormat)})GetValue({bindablePropertyName});");
if (this.ExistsBodySetter())
Expand Down

0 comments on commit b0889b3

Please sign in to comment.