From b41c76219947e13836aafb8832109e35cfc9b360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C6=B0=C6=A1ng=20Kafka=20=C4=90inh=20Ho=C3=A0ng?= Date: Mon, 13 May 2024 14:32:55 +0700 Subject: [PATCH] Handle different access levels like private, protected, internal --- BindableProps/BindableProps.csproj | 2 +- BindablePropsSG/Generators/BaseGenerator.cs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/BindableProps/BindableProps.csproj b/BindableProps/BindableProps.csproj index 791a3c3..cc01b41 100644 --- a/BindableProps/BindableProps.csproj +++ b/BindableProps/BindableProps.csproj @@ -13,7 +13,7 @@ LICENSE.txt source-generator;maui;net-standard;net-6;net-7;net-8;utility;helper https://github.com/KafkaWannaFly/BindableProps - 1.4.2 + 1.4.3 https://github.com/KafkaWannaFly/BindableProps git README.md diff --git a/BindablePropsSG/Generators/BaseGenerator.cs b/BindablePropsSG/Generators/BaseGenerator.cs index f3372c9..e042923 100644 --- a/BindablePropsSG/Generators/BaseGenerator.cs +++ b/BindablePropsSG/Generators/BaseGenerator.cs @@ -1,4 +1,5 @@ using System.Collections.Immutable; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Text; using BindablePropsSG.Utils; @@ -86,6 +87,17 @@ protected virtual string ProcessClass(TypeDeclarationSyntax? typeDeclarationSynt var namespaceSyntax = typeDeclarationSyntax.Parent as BaseNamespaceDeclarationSyntax; var namespaceName = namespaceSyntax?.Name.ToString() ?? "global"; + var accessLevelKeyWords = typeDeclarationSyntax?.Modifiers + .Where( + modifier => modifier.IsKind(SyntaxKind.PrivateKeyword) + || modifier.IsKind(SyntaxKind.PublicKeyword) + || modifier.IsKind(SyntaxKind.InternalKeyword) + || modifier.IsKind(SyntaxKind.ProtectedKeyword) + ) + .Select(modifier => modifier.Text) ?? ImmutableList.Empty; + var accessLevel = string.Join(" ", accessLevelKeyWords); + + var source = new StringBuilder($@" #pragma warning disable @@ -94,13 +106,13 @@ protected virtual string ProcessClass(TypeDeclarationSyntax? typeDeclarationSynt namespace {namespaceName} {{ - public partial {typeDeclarationSyntax.Keyword.Text} {typeDeclarationSyntax.Identifier} + {accessLevel} partial {typeDeclarationSyntax.Keyword.Text} {typeDeclarationSyntax.Identifier} {{ "); foreach (var (syntax, symbol) in syntaxSymbols) { - ProcessField(source, typeDeclarationSyntax, syntax, symbol); + ProcessField(source, typeDeclarationSyntax!, syntax, symbol); } source.Append(@$"