├── .editorconfig ├── .github └── FUNDING.yml ├── .gitignore ├── Avalonia.PropertyGenerator.sln ├── Directory.Build.props ├── Directory.Build.targets ├── LICENSE.md ├── README.md ├── appveyor.yml ├── build └── Targets │ ├── Import.props │ ├── Import.targets │ ├── PackageProperties.props │ ├── PackageProperties.targets │ ├── PackageVersions.targets │ └── RepoLayout.props └── src ├── Avalonia.PropertyGenerator.CSharp.Demo ├── App.axaml ├── App.axaml.cs ├── Avalonia.PropertyGenerator.CSharp.Demo.csproj ├── DemoControl.axaml ├── DemoControl.axaml.cs ├── MainWindow.axaml └── MainWindow.axaml.cs └── Avalonia.PropertyGenerator.CSharp ├── AnalyzerReleases.Shipped.md ├── AnalyzerReleases.Unshipped.md ├── AttachedProperty.cs ├── Avalonia.PropertyGenerator.CSharp.csproj ├── AvaloniaToClrPropertyGenerator.cs ├── DeclaringType.cs ├── DirectProperty.cs ├── IsExternalInit.cs ├── Property.cs ├── StyledProperty.cs ├── Types.cs └── Visitors ├── AvaloniaPropertyDeclaringTypeVisitor.cs ├── AvaloniaPropertyFieldVisitor.cs └── AvaloniaPropertyRootVisitor.cs /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = crlf 6 | indent_size = 4 7 | indent_style = space 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.cs] 12 | csharp_style_conditional_delegate_call = true:warning 13 | csharp_style_deconstructed_variable_declaration = true:suggestion 14 | csharp_style_expression_bodied_accessors = true:suggestion 15 | csharp_style_expression_bodied_constructors = false:warning 16 | csharp_style_expression_bodied_indexers = true:suggestion 17 | csharp_style_expression_bodied_methods = true:suggestion 18 | csharp_style_expression_bodied_operators = true:suggestion 19 | csharp_style_expression_bodied_properties = true:suggestion 20 | csharp_style_inlined_variable_declaration = true:warning 21 | csharp_style_pattern_matching_over_as_with_null_check = true:warning 22 | csharp_style_pattern_matching_over_is_with_cast_check = true:warning 23 | csharp_style_throw_expression = true:warning 24 | csharp_style_var_elsewhere = true:suggestion 25 | csharp_style_var_for_built_in_types = false:none 26 | csharp_style_var_when_type_is_apparent = true:suggestion 27 | 28 | csharp_new_line_before_catch = true:warning 29 | csharp_new_line_before_else = true:warning 30 | csharp_new_line_before_finally = true:warning 31 | csharp_new_line_before_members_in_anonymous_types = true:warning 32 | csharp_new_line_before_members_in_object_initializers = true:warning 33 | csharp_new_line_before_open_brace = all 34 | csharp_new_line_between_query_expression_clauses = true:warning 35 | 36 | csharp_indent_case_contents = true:warning 37 | csharp_indent_case_contents_when_block = false:warning 38 | csharp_indent_labels = one_less_than_current:suggestion 39 | csharp_indent_switch_labels = true:warning 40 | 41 | csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:warning 42 | 43 | csharp_preserve_single_line_blocks = true:suggestion 44 | csharp_preserve_single_line_statements = false:warning 45 | 46 | csharp_space_after_cast = false:warning 47 | csharp_space_after_colon_in_inheritance_clause = true:warning 48 | csharp_space_after_keywords_in_control_flow_statements = true:warning 49 | csharp_space_around_binary_operators = before_and_after:warning 50 | csharp_space_before_colon_in_inheritance_clause = true:warning 51 | csharp_space_between_method_call_empty_parameter_list_parentheses = false:warning 52 | csharp_space_between_method_call_name_and_opening_parenthesis = false:warning 53 | csharp_space_between_method_call_parameter_list_parentheses = false:warning 54 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false:warning 55 | csharp_space_between_method_declaration_parameter_list_parentheses = false:warning 56 | csharp_space_between_parentheses = false:warning 57 | 58 | csharp_prefer_braces = true:warning 59 | csharp_prefer_simple_default_expression = false:none 60 | 61 | csharp_using_directive_placement = outside_namespace:warning 62 | 63 | dotnet_separate_import_directive_groups = true 64 | 65 | dotnet_sort_system_directives_first = true:suggestion 66 | 67 | dotnet_style_coalesce_expression = true:warning 68 | dotnet_style_collection_initializer = true:suggestion 69 | dotnet_style_explicit_tuple_names = true:suggestion 70 | dotnet_style_null_propagation = true:warning 71 | dotnet_style_object_initializer = true:suggestion 72 | dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:suggestion 73 | dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:suggestion 74 | dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion 75 | dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:suggestion 76 | dotnet_style_predefined_type_for_locals_parameters_members = true:warning 77 | dotnet_style_predefined_type_for_member_access = false:warning 78 | dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion 79 | dotnet_style_prefer_inferred_tuple_names = true:suggestion 80 | dotnet_style_qualification_for_event = false:warning 81 | dotnet_style_qualification_for_field = false:warning 82 | dotnet_style_qualification_for_method = false:warning 83 | dotnet_style_qualification_for_property = false:warning 84 | csharp_style_pattern_local_over_anonymous_function = true:suggestion 85 | dotnet_style_prefer_auto_properties = true:warning 86 | dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion 87 | dotnet_style_prefer_conditional_expression_over_return = true:suggestion 88 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning 89 | dotnet_style_readonly_field = true:warning 90 | dotnet_style_require_accessibility_modifiers = for_non_interface_members:warning 91 | 92 | dotnet_naming_rule.camel_case_for_private_fields.severity = suggestion 93 | dotnet_naming_rule.camel_case_for_private_fields.symbols = private_fields 94 | dotnet_naming_rule.camel_case_for_private_fields.style = camel_case_underscore_style 95 | 96 | dotnet_naming_symbols.private_fields.applicable_kinds = field 97 | dotnet_naming_symbols.private_fields.applicable_accessibilities = private 98 | 99 | dotnet_naming_style.camel_case_underscore_style.required_prefix = _ 100 | dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case 101 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: jp2masa 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # User-specific files 2 | 3 | .vs/ 4 | *.suo 5 | *.user 6 | *.userosscache 7 | *.sln.docstates 8 | 9 | # Build results 10 | 11 | [Aa]rtifacts/ 12 | [Bb]in/ 13 | [Oo]bj/ 14 | -------------------------------------------------------------------------------- /Avalonia.PropertyGenerator.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio Version 17 3 | VisualStudioVersion = 17.2.32630.192 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F1CA42E2-1B03-4AD0-9CF8-3835DD0CC728}" 6 | ProjectSection(SolutionItems) = preProject 7 | .editorconfig = .editorconfig 8 | appveyor.yml = appveyor.yml 9 | Directory.Build.props = Directory.Build.props 10 | Directory.Build.targets = Directory.Build.targets 11 | README.md = README.md 12 | EndProjectSection 13 | EndProject 14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{A416D0AA-C885-4CD3-B89A-A33360D231E7}" 15 | EndProject 16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Targets", "Targets", "{189398A8-47D7-4752-9AAF-6E9632AD9F98}" 17 | ProjectSection(SolutionItems) = preProject 18 | build\Targets\Import.props = build\Targets\Import.props 19 | build\Targets\Import.targets = build\Targets\Import.targets 20 | build\Targets\PackageProperties.props = build\Targets\PackageProperties.props 21 | build\Targets\PackageProperties.targets = build\Targets\PackageProperties.targets 22 | build\Targets\PackageVersions.targets = build\Targets\PackageVersions.targets 23 | build\Targets\RepoLayout.props = build\Targets\RepoLayout.props 24 | EndProjectSection 25 | EndProject 26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.PropertyGenerator.CSharp", "src\Avalonia.PropertyGenerator.CSharp\Avalonia.PropertyGenerator.CSharp.csproj", "{5B1DE05B-3878-402D-B629-22FA03CDD588}" 27 | EndProject 28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.PropertyGenerator.CSharp.Demo", "src\Avalonia.PropertyGenerator.CSharp.Demo\Avalonia.PropertyGenerator.CSharp.Demo.csproj", "{B9BBDBE5-50A0-4265-982F-36DB2AA12E12}" 29 | EndProject 30 | Global 31 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 32 | Debug|Any CPU = Debug|Any CPU 33 | Release|Any CPU = Release|Any CPU 34 | EndGlobalSection 35 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 36 | {5B1DE05B-3878-402D-B629-22FA03CDD588}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {5B1DE05B-3878-402D-B629-22FA03CDD588}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {5B1DE05B-3878-402D-B629-22FA03CDD588}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {5B1DE05B-3878-402D-B629-22FA03CDD588}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {B9BBDBE5-50A0-4265-982F-36DB2AA12E12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {B9BBDBE5-50A0-4265-982F-36DB2AA12E12}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {B9BBDBE5-50A0-4265-982F-36DB2AA12E12}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {B9BBDBE5-50A0-4265-982F-36DB2AA12E12}.Release|Any CPU.Build.0 = Release|Any CPU 44 | EndGlobalSection 45 | GlobalSection(SolutionProperties) = preSolution 46 | HideSolutionNode = FALSE 47 | EndGlobalSection 48 | GlobalSection(NestedProjects) = preSolution 49 | {A416D0AA-C885-4CD3-B89A-A33360D231E7} = {F1CA42E2-1B03-4AD0-9CF8-3835DD0CC728} 50 | {189398A8-47D7-4752-9AAF-6E9632AD9F98} = {A416D0AA-C885-4CD3-B89A-A33360D231E7} 51 | EndGlobalSection 52 | GlobalSection(ExtensibilityGlobals) = postSolution 53 | SolutionGuid = {3A9EB4DD-8527-4171-AA20-45BF527D9737} 54 | EndGlobalSection 55 | EndGlobal 56 | -------------------------------------------------------------------------------- /Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(MSBuildThisFileDirectory) 5 | 6 | 7 | 8 | $(RepoRoot)\Icon.ico 9 | 10 | 11 | 12 | Latest 13 | Enable 14 | 15 | 16 | 17 | True 18 | 19 | 20 | 21 | 0.10.0 22 | -build.$(APPVEYOR_BUILD_NUMBER)+$(APPVEYOR_REPO_COMMIT.Substring(0, 7)) 23 | -localbuild$([System.DateTime]::Now.ToString("yyyyMMddHHmmss")) 24 | -beta7 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Directory.Build.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | ===================== 3 | 4 | Copyright © jp2masa 5 | 6 | Permission is hereby granted, free of charge, to any person 7 | obtaining a copy of this software and associated documentation 8 | files (the “Software”), to deal in the Software without 9 | restriction, including without limitation the rights to use, 10 | copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the 12 | Software is furnished to do so, subject to the following 13 | conditions: 14 | 15 | The above copyright notice and this permission notice shall be 16 | included in all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 20 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 22 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 23 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 24 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 25 | OTHER DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build status](https://ci.appveyor.com/api/projects/status/1ow4vfhp9t92k3bh/branch/master?svg=true)](https://ci.appveyor.com/project/jp2masa/avalonia-propertygenerator/branch/master) 2 | [![NuGet](https://img.shields.io/nuget/v/jp2masa.Avalonia.PropertyGenerator.CSharp.svg)](https://www.nuget.org/packages/jp2masa.Avalonia.PropertyGenerator.CSharp/) 3 | [![MyGet](https://img.shields.io/myget/jp2masa/vpre/jp2masa.Avalonia.PropertyGenerator.CSharp.svg?label=myget)](https://www.myget.org/feed/jp2masa/package/nuget/jp2masa.Avalonia.PropertyGenerator.CSharp) 4 | 5 | # Avalonia.PropertyGenerator 6 | 7 | Avalonia.PropertyGenerator generates the appropriate CLR members for Avalonia property definitions. 8 | 9 | ## Usage 10 | 11 | 1. Add reference to `jp2masa.Avalonia.PropertyGenerator.CSharp` package: 12 | 13 | ```xml 14 | 15 | ``` 16 | 17 | 2. Declare Avalonia properties as usual, except the CLR members, which are now automatically generated! 18 | 19 | ## Example 20 | 21 | ### Source 22 | 23 | ```cs 24 | using Avalonia.Controls; 25 | using Avalonia.Controls.Primitives; 26 | 27 | namespace Avalonia.PropertyGenerator.CSharp.Demo 28 | { 29 | internal sealed partial class DemoControl : TemplatedControl 30 | { 31 | public static readonly StyledProperty NumberProperty = 32 | AvaloniaProperty.Register(nameof(Number)); 33 | 34 | [BackingField(Name = "m_text", Accessibility = BackingFieldAccessibility.Internal)] 35 | public static readonly DirectProperty TextProperty = 36 | AvaloniaProperty.RegisterDirect(nameof(Text), o => o.Text, (o, v) => o.Text = v); 37 | 38 | public static readonly AttachedProperty BoolProperty = 39 | AvaloniaProperty.RegisterAttached("Bool"); 40 | 41 | public DemoControl() 42 | { 43 | m_text = "Hello World!"; 44 | } 45 | } 46 | } 47 | ``` 48 | 49 | ### Generated code 50 | 51 | ```cs 52 | namespace Avalonia.PropertyGenerator.CSharp.Demo 53 | { 54 | partial class DemoControl 55 | { 56 | public double Number 57 | { 58 | get => GetValue(NumberProperty); 59 | set => SetValue(NumberProperty, value); 60 | } 61 | 62 | internal string? m_text; 63 | 64 | public string? Text 65 | { 66 | get => m_text; 67 | set => SetAndRaise(TextProperty, ref m_text, value); 68 | } 69 | 70 | public static bool GetBool(Avalonia.AvaloniaObject obj) => obj.GetValue(BoolProperty); 71 | 72 | public static void SetBool(Avalonia.AvaloniaObject obj, bool value) => obj.SetValue(BoolProperty, value); 73 | } 74 | } 75 | 76 | ``` 77 | 78 | ## TODO 79 | 80 | - Readonly direct properties 81 | - Generate XML documentation 82 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 0.10.0-build{build} 2 | image: Visual Studio 2022 3 | 4 | shallow_clone: true 5 | clone_folder: c:\Avalonia.PropertyGenerator 6 | 7 | configuration: 8 | - Debug 9 | - Release 10 | platform: Any CPU 11 | 12 | matrix: 13 | fast_finish: true 14 | 15 | nuget: 16 | account_feed: false 17 | project_feed: true 18 | disable_publish_on_pr: true 19 | 20 | build_script: 21 | - cmd: dotnet msbuild /t:Restore;Build;Pack Avalonia.PropertyGenerator.sln 22 | 23 | test: off 24 | 25 | artifacts: 26 | - path: 'artifacts\Debug\nupkg\*.nupkg' 27 | name: DebugNupkg 28 | - path: 'artifacts\Release\nupkg\*.nupkg' 29 | name: ReleaseNupkg 30 | 31 | deploy: 32 | - provider: NuGet 33 | api_key: 34 | secure: nsINQ1oHohP7tM/wP5Icr1Ko3P/kgo12dHZlEd1HoYDOGkmRFQE6qJVx26uZLL7O 35 | artifact: ReleaseNupkg 36 | on: 37 | appveyor_repo_tag: true 38 | configuration: Release 39 | - provider: NuGet 40 | server: https://www.myget.org/F/jp2masa/api/v2/package 41 | artifact: DebugNupkg 42 | api_key: 43 | secure: puOcEbngEmaVMEnUL20u4mzATgvoyaTPRWGGwE98as1+8KGY3ypOKzt5OV63duwI 44 | on: 45 | branch: master 46 | configuration: Debug 47 | -------------------------------------------------------------------------------- /build/Targets/Import.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /build/Targets/Import.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /build/Targets/PackageProperties.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(PackageVersion)$(PackageVersionSuffix) 5 | 6 | 7 | 8 | Avalonia.PropertyGenerator generates the appropriate CLR members for Avalonia property definitions. 9 | 10 | 11 | 12 | jp2masa 13 | Copyright © $([System.DateTime]::Now.Year) jp2masa 14 | 15 | http://github.com/jp2masa/Avalonia.PropertyGenerator 16 | MIT 17 | True 18 | avalonia property styled direct attached roslyn source generator dotnet core csharp 19 | git 20 | https://github.com/jp2masa/Avalonia.PropertyGenerator.git 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /build/Targets/PackageProperties.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(AssemblyName) 5 | jp2masa.$(PackageId) 6 | $(PackageDescription) 7 | 8 | $(BaseDescription) 9 | $(BaseDescription) 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /build/Targets/PackageVersions.targets: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 0.10.21 5 | 3.3.4 6 | 3.10.0 7 | 5.0.3 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /build/Targets/RepoLayout.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Debug 5 | AnyCPU 6 | $(Platform) 7 | 8 | 9 | 10 | $(RepoRoot)artifacts\ 11 | $(ArtifactsDir)$(Configuration)\nupkg\ 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp.Demo/App.axaml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp.Demo/App.axaml.cs: -------------------------------------------------------------------------------- 1 | using Avalonia.Controls.ApplicationLifetimes; 2 | using Avalonia.Markup.Xaml; 3 | 4 | namespace Avalonia.PropertyGenerator.CSharp.Demo 5 | { 6 | public class App : Application 7 | { 8 | public override void Initialize() => AvaloniaXamlLoader.Load(this); 9 | 10 | public override void OnFrameworkInitializationCompleted() 11 | { 12 | if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) 13 | { 14 | desktop.MainWindow = new MainWindow(); 15 | } 16 | 17 | base.OnFrameworkInitializationCompleted(); 18 | } 19 | 20 | private static void Main(string[] args) => 21 | BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); 22 | 23 | private static AppBuilder BuildAvaloniaApp() => 24 | AppBuilder.Configure() 25 | .UsePlatformDetect() 26 | .LogToTrace(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp.Demo/Avalonia.PropertyGenerator.CSharp.Demo.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | WinExe 6 | False 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp.Demo/DemoControl.axaml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp.Demo/DemoControl.axaml.cs: -------------------------------------------------------------------------------- 1 | using Avalonia.Controls; 2 | using Avalonia.Controls.Primitives; 3 | 4 | namespace Avalonia.PropertyGenerator.CSharp.Demo 5 | { 6 | internal sealed partial class DemoControl : TemplatedControl 7 | { 8 | public static readonly StyledProperty NumberProperty = 9 | AvaloniaProperty.Register(nameof(Number)); 10 | 11 | public static readonly StyledProperty NullableNumberProperty = 12 | AvaloniaProperty.Register(nameof(NullableNumber)); 13 | 14 | [BackingField(Name = "m_text", Accessibility = BackingFieldAccessibility.Internal)] 15 | public static readonly DirectProperty TextProperty = 16 | AvaloniaProperty.RegisterDirect(nameof(Text), o => o.Text, (o, v) => o.Text = v); 17 | 18 | [Readonly] 19 | public static readonly DirectProperty ReadonlyTextProperty = 20 | AvaloniaProperty.RegisterDirect(nameof(ReadonlyText), o => o.ReadonlyText); 21 | 22 | public static readonly AttachedProperty BoolProperty = 23 | AvaloniaProperty.RegisterAttached("Bool"); 24 | 25 | public static readonly StyledProperty ExistingStyledProperty = 26 | AvaloniaProperty.Register(nameof(ExistingStyled)); 27 | 28 | public string ExistingStyled => GetValue(ExistingStyledProperty); 29 | 30 | static DemoControl() 31 | { 32 | NumberProperty.Changed.AddClassHandler( 33 | (sender, e) => 34 | { 35 | if (e.NewValue is double number) 36 | { 37 | sender.SetAndRaise(ReadonlyTextProperty, ref sender._readonlyText, number.ToString()); 38 | sender.SetValue(ExistingStyledProperty, number.ToString()); 39 | } 40 | } 41 | ); 42 | } 43 | 44 | public DemoControl() 45 | { 46 | m_text = "Hello World!"; 47 | _readonlyText = "0"; 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp.Demo/MainWindow.axaml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp.Demo/MainWindow.axaml.cs: -------------------------------------------------------------------------------- 1 | using Avalonia.Controls; 2 | using Avalonia.Markup.Xaml; 3 | 4 | namespace Avalonia.PropertyGenerator.CSharp.Demo 5 | { 6 | internal sealed class MainWindow : Window 7 | { 8 | public MainWindow() 9 | { 10 | InitializeComponent(); 11 | #if DEBUG 12 | this.AttachDevTools(); 13 | #endif 14 | } 15 | 16 | private void InitializeComponent() => AvaloniaXamlLoader.Load(this); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/AnalyzerReleases.Shipped.md: -------------------------------------------------------------------------------- 1 | ; Shipped analyzer releases 2 | ; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md 3 | 4 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/AnalyzerReleases.Unshipped.md: -------------------------------------------------------------------------------- 1 | ; Unshipped analyzer release 2 | ; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md 3 | 4 | ### New Rules 5 | Rule ID | Category | Severity | Notes 6 | --------|----------|----------|------- 7 | AVPROP01 | Avalonia.PropertyGenerator.CSharp | Warning | Types 8 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/AttachedProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using Microsoft.CodeAnalysis; 4 | 5 | namespace Avalonia.PropertyGenerator.CSharp 6 | { 7 | internal sealed class AttachedProperty : Property 8 | { 9 | private AttachedProperty( 10 | IFieldSymbol field, 11 | string name, 12 | bool getterExists, 13 | Accessibility getterAccessibility, 14 | bool setterExists, 15 | Accessibility setterAccessibility) 16 | : base(field, name) 17 | { 18 | GetterExists = getterExists; 19 | GetterAccessibility = getterAccessibility; 20 | 21 | SetterExists = setterExists; 22 | SetterAccessibility = setterAccessibility; 23 | } 24 | 25 | public bool GetterExists { get; } 26 | 27 | public Accessibility GetterAccessibility { get; } 28 | 29 | public bool SetterExists { get; } 30 | 31 | public Accessibility SetterAccessibility { get; } 32 | 33 | public static new AttachedProperty? TryCreate(Types types, IFieldSymbol field) 34 | { 35 | var name = GetPropertyName(field); 36 | 37 | if (name is null) 38 | { 39 | return null; 40 | } 41 | 42 | var getterName = "Get" + name; 43 | var setterName = "Set" + name; 44 | 45 | var getterExists = false; 46 | var setterExists = false; 47 | 48 | if (field.ContainingType is INamedTypeSymbol declaringType) 49 | { 50 | getterExists = declaringType.GetMembers().Any(x => String.Equals(x.Name, getterName, StringComparison.Ordinal)); 51 | setterExists = declaringType.GetMembers().Any(x => String.Equals(x.Name, setterName, StringComparison.Ordinal)); 52 | } 53 | 54 | return new AttachedProperty(field, name, getterExists, field.DeclaredAccessibility, setterExists, field.DeclaredAccessibility); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/Avalonia.PropertyGenerator.CSharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | True 6 | False 7 | True 8 | True 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/AvaloniaToClrPropertyGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Text; 5 | using Microsoft.CodeAnalysis; 6 | using Microsoft.CodeAnalysis.CSharp; 7 | using Microsoft.CodeAnalysis.Text; 8 | 9 | using Avalonia.PropertyGenerator.CSharp.Visitors; 10 | 11 | namespace Avalonia.PropertyGenerator.CSharp 12 | { 13 | [Generator] 14 | internal sealed class AvaloniaToClrPropertyGenerator : ISourceGenerator 15 | { 16 | public void Initialize(GeneratorInitializationContext context) { } 17 | 18 | public void Execute(GeneratorExecutionContext context) 19 | { 20 | var compilation = context.Compilation; 21 | 22 | if (Types.FromCompilation(compilation, context.ReportDiagnostic) is not Types wellKnownTypes) 23 | { 24 | return; 25 | } 26 | 27 | var attributes = 28 | @"using System; 29 | 30 | namespace Avalonia.PropertyGenerator 31 | { 32 | /// 33 | /// Copied from roslyn: https://github.com/dotnet/roslyn/blob/9007fbc80c19cef52defe1cfb981c838b995c74d/src/Compilers/Core/Portable/Symbols/Accessibility.cs 34 | /// 35 | internal enum BackingFieldAccessibility 36 | { 37 | // 38 | // Summary: 39 | // No accessibility specified. 40 | NotApplicable = 0, 41 | Private = 1, 42 | ProtectedAndInternal = 2, 43 | ProtectedAndFriend = 2, 44 | Protected = 3, 45 | Internal = 4, 46 | Friend = 4, 47 | ProtectedOrInternal = 5, 48 | ProtectedOrFriend = 5, 49 | Public = 6 50 | } 51 | 52 | [AttributeUsage(AttributeTargets.Field)] 53 | internal sealed class BackingFieldAttribute : Attribute 54 | { 55 | public string? Name { get; set; } 56 | 57 | public BackingFieldAccessibility Accessibility { get; set; } 58 | } 59 | 60 | [AttributeUsage(AttributeTargets.Field)] 61 | internal sealed class ReadonlyAttribute : Attribute 62 | { 63 | } 64 | } 65 | "; 66 | 67 | var attributesSource = SourceText.From(attributes, Encoding.UTF8); 68 | 69 | if (context.ParseOptions is not CSharpParseOptions parseOptions) 70 | { 71 | throw new InvalidOperationException("Only C# is currently supported!"); 72 | } 73 | 74 | compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(attributesSource, parseOptions)); 75 | 76 | var visitor = new AvaloniaPropertyRootVisitor(wellKnownTypes); 77 | var types = visitor.Visit(compilation.Assembly.GlobalNamespace); 78 | 79 | if (!types.HasValue || types.Value.Length == 0) 80 | { 81 | return; 82 | } 83 | 84 | context.AddSource("Attributes.cs", attributesSource); 85 | 86 | foreach (var type in types) 87 | { 88 | var sourceBuilder = new StringBuilder( 89 | $@"namespace {type.Type.ContainingNamespace.ToDisplayString()} 90 | {{"); 91 | 92 | sourceBuilder.Append($@" 93 | partial class {type.Type.Name} 94 | {{"); 95 | 96 | foreach (var property in type.StyledProperties) 97 | { 98 | 99 | if (!property.ClrPropertyExists) 100 | { 101 | var typeFullName = TypeToFullDisplayString(((INamedTypeSymbol)property.Field.Type).TypeArguments[0]); 102 | var accessibility = GetAccessibilityText(property.ClrPropertyAccessibility); 103 | 104 | sourceBuilder.Append($@" 105 | {accessibility}{typeFullName} {property.Name} 106 | {{ 107 | get => GetValue({property.Field.Name}); 108 | set => SetValue({property.Field.Name}, value); 109 | }} 110 | "); 111 | } 112 | } 113 | 114 | foreach (var property in type.DirectProperties) 115 | { 116 | var typeFullName = TypeToFullDisplayString(((INamedTypeSymbol)property.Field.Type).TypeArguments[1]); 117 | 118 | var propertyAccessibility = GetAccessibilityText(property.ClrPropertyAccessibility); 119 | var fieldAccessibility = GetAccessibilityText(property.BackingFieldAccessibility); 120 | 121 | var isReadonly = property.IsReadonly; 122 | 123 | if (!property.BackingFieldExists) 124 | { 125 | sourceBuilder.Append($@" 126 | {fieldAccessibility}{typeFullName} {property.BackingFieldName}; 127 | "); 128 | } 129 | 130 | if (!property.ClrPropertyExists) 131 | { 132 | if (isReadonly) 133 | { 134 | sourceBuilder.Append($@" 135 | {propertyAccessibility}{typeFullName} {property.Name} => {property.BackingFieldName}; 136 | "); 137 | } 138 | else 139 | { 140 | sourceBuilder.Append($@" 141 | {propertyAccessibility}{typeFullName} {property.Name} 142 | {{ 143 | get => {property.BackingFieldName}; 144 | set => SetAndRaise({property.Field.Name}, ref {property.BackingFieldName}, value); 145 | }} 146 | "); 147 | } 148 | } 149 | } 150 | 151 | foreach (var property in type.AttachedProperties) 152 | { 153 | var typeFullName = TypeToFullDisplayString(((INamedTypeSymbol)property.Field.Type).TypeArguments[0]); 154 | 155 | var getterAccessibility = GetAccessibilityText(property.GetterAccessibility); 156 | var setterAccessibility = GetAccessibilityText(property.SetterAccessibility); 157 | 158 | if (!property.GetterExists) 159 | { 160 | sourceBuilder.Append($@" 161 | {getterAccessibility}static {typeFullName} Get{property.Name}({TypeToFullDisplayString(wellKnownTypes.AvaloniaObject)} obj) => obj.GetValue({property.Field.Name}); 162 | "); 163 | } 164 | 165 | if (!property.SetterExists) 166 | { 167 | sourceBuilder.Append($@" 168 | {setterAccessibility}static void Set{property.Name}({TypeToFullDisplayString(wellKnownTypes.AvaloniaObject)} obj, {typeFullName} value) => obj.SetValue({property.Field.Name}, value); 169 | "); 170 | } 171 | } 172 | 173 | sourceBuilder.Append( 174 | @" } 175 | } 176 | "); 177 | 178 | context.AddSource( 179 | Path.GetFileName(type.Type.DeclaringSyntaxReferences.First().SyntaxTree.FilePath), 180 | SourceText.From(sourceBuilder.ToString(), Encoding.UTF8)); 181 | } 182 | } 183 | 184 | private static string GetAccessibilityText(Accessibility accessibility) 185 | { 186 | var result = SyntaxFacts.GetText(accessibility); 187 | 188 | if (!String.IsNullOrWhiteSpace(result)) 189 | { 190 | result += ' '; 191 | } 192 | 193 | return result; 194 | } 195 | 196 | private static string TypeToFullDisplayString(ITypeSymbol type) => 197 | type.ToDisplayString( 198 | SymbolDisplayFormat.FullyQualifiedFormat.WithMiscellaneousOptions( 199 | SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers 200 | | SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier 201 | | SymbolDisplayMiscellaneousOptions.UseSpecialTypes 202 | ) 203 | ); 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/DeclaringType.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Immutable; 2 | using Microsoft.CodeAnalysis; 3 | 4 | namespace Avalonia.PropertyGenerator.CSharp 5 | { 6 | internal sealed record DeclaringType( 7 | INamedTypeSymbol Type, 8 | ImmutableArray StyledProperties, 9 | ImmutableArray DirectProperties, 10 | ImmutableArray AttachedProperties); 11 | } 12 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/DirectProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using Microsoft.CodeAnalysis; 4 | 5 | namespace Avalonia.PropertyGenerator.CSharp 6 | { 7 | internal sealed class DirectProperty : Property 8 | { 9 | private DirectProperty( 10 | IFieldSymbol field, 11 | string name, 12 | bool clrPropertyExists, 13 | Accessibility clrPropertyAccessibility, 14 | bool backingFieldExists, 15 | Accessibility backingFieldAccessibility, 16 | string backingFieldName, 17 | bool isReadonly) 18 | : base(field, name) 19 | { 20 | ClrPropertyExists = clrPropertyExists; 21 | ClrPropertyAccessibility = clrPropertyAccessibility; 22 | 23 | BackingFieldExists = backingFieldExists; 24 | BackingFieldAccessibility = backingFieldAccessibility; 25 | BackingFieldName = backingFieldName; 26 | 27 | IsReadonly = isReadonly; 28 | } 29 | 30 | public bool ClrPropertyExists { get; } 31 | 32 | public Accessibility ClrPropertyAccessibility { get; } 33 | 34 | public bool BackingFieldExists { get; } 35 | 36 | public Accessibility BackingFieldAccessibility { get; } 37 | 38 | public string BackingFieldName { get; } 39 | 40 | public bool IsReadonly { get; } 41 | 42 | public static new DirectProperty? TryCreate(Types types, IFieldSymbol field) 43 | { 44 | var name = GetPropertyName(field); 45 | 46 | if (name is null) 47 | { 48 | return null; 49 | } 50 | 51 | var backingFieldAccessibility = Accessibility.NotApplicable; 52 | var backingFieldName = default(string); 53 | var isReadonly = false; 54 | 55 | foreach (var attribute in field.GetAttributes()) 56 | { 57 | if (attribute.AttributeClass?.ToDisplayString() == "Avalonia.PropertyGenerator.ReadonlyAttribute") 58 | { 59 | isReadonly = true; 60 | continue; 61 | } 62 | 63 | if (attribute.AttributeClass?.ToDisplayString() == "Avalonia.PropertyGenerator.BackingFieldAttribute") 64 | { 65 | foreach (var arg in attribute.NamedArguments) 66 | { 67 | if (arg.Key == "Name" && arg.Value.Value is string value) 68 | { 69 | backingFieldName = value; 70 | } 71 | else if (arg.Key == "Accessibility" && arg.Value.Value is not null) 72 | { 73 | backingFieldAccessibility = (Accessibility)arg.Value.Value; 74 | } 75 | } 76 | 77 | continue; 78 | } 79 | } 80 | 81 | if (backingFieldAccessibility == Accessibility.NotApplicable) 82 | { 83 | backingFieldAccessibility = Accessibility.Private; 84 | } 85 | 86 | backingFieldName ??= GetDefaultBackingFieldName(name); 87 | 88 | var clrPropertyExists = false; 89 | var backingFieldExists = false; 90 | 91 | if (field.ContainingType is INamedTypeSymbol declaringType) 92 | { 93 | clrPropertyExists = declaringType.GetMembers().Any(x => String.Equals(x.Name, name, StringComparison.Ordinal)); 94 | backingFieldExists = declaringType.GetMembers().Any(x => String.Equals(x.Name, backingFieldName, StringComparison.Ordinal)); 95 | } 96 | 97 | return new DirectProperty(field, name, clrPropertyExists, field.DeclaredAccessibility, backingFieldExists, backingFieldAccessibility, backingFieldName, isReadonly); 98 | } 99 | 100 | private static string GetDefaultBackingFieldName(string name) => 101 | String.IsNullOrWhiteSpace(name) 102 | ? throw new InvalidOperationException() 103 | : $"_{Char.ToLowerInvariant(name[0])}{name.Substring(1)}"; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/IsExternalInit.cs: -------------------------------------------------------------------------------- 1 | namespace System.Runtime.CompilerServices 2 | { 3 | internal static class IsExternalInit { } 4 | } 5 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/Property.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | 3 | namespace Avalonia.PropertyGenerator.CSharp 4 | { 5 | internal abstract class Property 6 | { 7 | protected Property(IFieldSymbol field, string name) 8 | { 9 | Field = field; 10 | Name = name; 11 | } 12 | 13 | public IFieldSymbol Field { get; } 14 | 15 | public string Name { get; } 16 | 17 | public static Property? TryCreate(Types types, IFieldSymbol field) 18 | { 19 | if (!field.IsStatic) 20 | { 21 | return null; 22 | } 23 | 24 | if (field.Type is INamedTypeSymbol type && type.IsGenericType) 25 | { 26 | type = type.ConstructUnboundGenericType(); 27 | 28 | if (SymbolEqualityComparer.Default.Equals(type, types.StyledProperty)) 29 | { 30 | return StyledProperty.TryCreate(types, field); 31 | } 32 | 33 | if (SymbolEqualityComparer.Default.Equals(type, types.DirectProperty)) 34 | { 35 | return DirectProperty.TryCreate(types, field); 36 | } 37 | 38 | if (SymbolEqualityComparer.Default.Equals(type, types.AttachedProperty)) 39 | { 40 | return AttachedProperty.TryCreate(types, field); 41 | } 42 | } 43 | 44 | return null; 45 | } 46 | 47 | public static string? GetPropertyName(IFieldSymbol field) 48 | { 49 | var i = field.Name.LastIndexOf("Property"); 50 | 51 | if (i == -1 || i == 0) 52 | { 53 | return null; 54 | } 55 | 56 | return field.Name.Substring(0, i); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/StyledProperty.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using Microsoft.CodeAnalysis; 4 | 5 | namespace Avalonia.PropertyGenerator.CSharp 6 | { 7 | internal sealed class StyledProperty : Property 8 | { 9 | private StyledProperty(IFieldSymbol field, string name, bool clrPropertyExists, Accessibility clrPropertyAccessibility) 10 | : base(field, name) 11 | { 12 | ClrPropertyExists = clrPropertyExists; 13 | ClrPropertyAccessibility = clrPropertyAccessibility; 14 | } 15 | 16 | public bool ClrPropertyExists { get; } 17 | 18 | public Accessibility ClrPropertyAccessibility { get; } 19 | 20 | public static new StyledProperty? TryCreate(Types types, IFieldSymbol field) 21 | { 22 | var name = GetPropertyName(field); 23 | 24 | if (name is null) 25 | { 26 | return null; 27 | } 28 | 29 | var clrPropertyExists = false; 30 | 31 | if (field.ContainingType is INamedTypeSymbol declaringType) 32 | { 33 | clrPropertyExists = declaringType.GetMembers().Any(x => String.Equals(x.Name, name, StringComparison.Ordinal)); 34 | } 35 | 36 | return new StyledProperty(field, name, clrPropertyExists, field.DeclaredAccessibility); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/Types.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.CodeAnalysis; 3 | 4 | namespace Avalonia.PropertyGenerator.CSharp 5 | { 6 | internal sealed record Types( 7 | INamedTypeSymbol AvaloniaObject, 8 | INamedTypeSymbol StyledProperty, 9 | INamedTypeSymbol DirectProperty, 10 | INamedTypeSymbol AttachedProperty) 11 | { 12 | private const string GetTypeWarningId = "AVPROP01"; 13 | private const string GetTypeWarningTitle = "AVPROP01"; 14 | private const string GetTypeWarningMessage = "Failed to load '{0}'"; 15 | private const string GetTypeWarningCategory = "Avalonia.PropertyGenerator.CSharp"; 16 | private const string GetTypeWarningDescription = "The Avalonia property generator was unable to find a property type from the Avalonia assemblies. Make sure Avalonia is correctly referenced. If you believe this is a bug, please report it: https://github.com/jp2masa/Avalonia.PropertyGenerator/issues/new."; 17 | 18 | private static readonly DiagnosticDescriptor GetTypeWarning = 19 | new DiagnosticDescriptor( 20 | GetTypeWarningId, 21 | GetTypeWarningTitle, 22 | GetTypeWarningMessage, 23 | GetTypeWarningCategory, 24 | DiagnosticSeverity.Warning, 25 | isEnabledByDefault: true, 26 | GetTypeWarningDescription); 27 | 28 | public static Types? FromCompilation(Compilation compilation, Action? reportDiagnostic) 29 | { 30 | INamedTypeSymbol? GetType(string name) 31 | { 32 | var type = compilation.GetTypeByMetadataName(name); 33 | 34 | if (type is null) 35 | { 36 | reportDiagnostic?.Invoke(Diagnostic.Create(GetTypeWarning, null, name)); 37 | return null; 38 | } 39 | 40 | if (type.IsGenericType && !type.IsUnboundGenericType) 41 | { 42 | type = type.ConstructUnboundGenericType(); 43 | } 44 | 45 | return type; 46 | } 47 | 48 | var types = new Types( 49 | GetType("Avalonia.AvaloniaObject")!, 50 | GetType("Avalonia.StyledProperty`1")!, 51 | GetType("Avalonia.DirectProperty`2")!, 52 | GetType("Avalonia.AttachedProperty`1")!); 53 | 54 | if (types.AvaloniaObject is null 55 | || types.StyledProperty is null 56 | || types.DirectProperty is null 57 | || types.AttachedProperty is null) 58 | { 59 | return null; 60 | } 61 | 62 | return types; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/Visitors/AvaloniaPropertyDeclaringTypeVisitor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Immutable; 2 | using System.Linq; 3 | using Microsoft.CodeAnalysis; 4 | 5 | namespace Avalonia.PropertyGenerator.CSharp.Visitors 6 | { 7 | internal sealed class AvaloniaPropertyDeclaringTypeVisitor : SymbolVisitor 8 | { 9 | private readonly Types _types; 10 | 11 | public AvaloniaPropertyDeclaringTypeVisitor(Types types) 12 | { 13 | _types = types; 14 | } 15 | 16 | public override DeclaringType? VisitNamedType(INamedTypeSymbol symbol) 17 | { 18 | var type = symbol; 19 | 20 | while (type is not null) 21 | { 22 | if (SymbolEqualityComparer.Default.Equals(type, _types.AvaloniaObject)) 23 | { 24 | break; 25 | } 26 | 27 | type = type.BaseType; 28 | } 29 | 30 | if (type is null) 31 | { 32 | return null; 33 | } 34 | 35 | var styled = ImmutableArray.CreateBuilder(); 36 | var direct = ImmutableArray.CreateBuilder(); 37 | var attached = ImmutableArray.CreateBuilder(); 38 | 39 | var visitor = new AvaloniaPropertyFieldVisitor(_types); 40 | 41 | foreach (var member in symbol.GetMembers()) 42 | { 43 | var property = member.Accept(visitor); 44 | 45 | switch (property) 46 | { 47 | case StyledProperty x: 48 | styled.Add(x); 49 | break; 50 | case DirectProperty x: 51 | direct.Add(x); 52 | break; 53 | case AttachedProperty x: 54 | attached.Add(x); 55 | break; 56 | } 57 | } 58 | 59 | return (styled.Count > 0 || direct.Count > 0 || attached.Count > 0) 60 | ? new DeclaringType(symbol, styled.ToImmutable(), direct.ToImmutable(), attached.ToImmutable()) 61 | : default; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/Visitors/AvaloniaPropertyFieldVisitor.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CodeAnalysis; 2 | 3 | namespace Avalonia.PropertyGenerator.CSharp.Visitors 4 | { 5 | internal sealed class AvaloniaPropertyFieldVisitor : SymbolVisitor 6 | { 7 | private readonly Types _types; 8 | 9 | public AvaloniaPropertyFieldVisitor(Types types) 10 | { 11 | _types = types; 12 | } 13 | 14 | public override Property? VisitField(IFieldSymbol symbol) 15 | { 16 | if (Property.TryCreate(_types, symbol) is Property property) 17 | { 18 | return property; 19 | } 20 | 21 | return null; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Avalonia.PropertyGenerator.CSharp/Visitors/AvaloniaPropertyRootVisitor.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Immutable; 2 | using Microsoft.CodeAnalysis; 3 | 4 | namespace Avalonia.PropertyGenerator.CSharp.Visitors 5 | { 6 | internal sealed class AvaloniaPropertyRootVisitor : SymbolVisitor?> 7 | { 8 | private readonly Types _types; 9 | 10 | public AvaloniaPropertyRootVisitor(Types types) 11 | { 12 | _types = types; 13 | } 14 | 15 | public override ImmutableArray? VisitNamespace(INamespaceSymbol symbol) 16 | { 17 | var builder = ImmutableArray.CreateBuilder(); 18 | var visitor = new AvaloniaPropertyDeclaringTypeVisitor(_types); 19 | 20 | foreach (var member in symbol.GetMembers()) 21 | { 22 | var ns = member.Accept(this); 23 | 24 | if (ns is not null) 25 | { 26 | builder.AddRange(ns); 27 | } 28 | 29 | var type = member.Accept(visitor); 30 | 31 | if (type is not null) 32 | { 33 | builder.Add(type); 34 | } 35 | } 36 | 37 | return builder.Count > 0 ? builder.ToImmutable() : null; 38 | } 39 | } 40 | } 41 | --------------------------------------------------------------------------------