|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Generators.Base.Extensions.Common; |
| 5 | +using Microsoft.CodeAnalysis; |
| 6 | +using Microsoft.CodeAnalysis.CSharp; |
| 7 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 8 | + |
| 9 | +namespace Generators.Base.Extensions.New |
| 10 | +{ |
| 11 | + public static class RecordDeclarationSyntaxExtensions |
| 12 | + { |
| 13 | + public static RecordDeclarationSyntax Construct( |
| 14 | + this RecordDeclarationSyntax symbol, |
| 15 | + params string[] typeArguments |
| 16 | + ) |
| 17 | + { |
| 18 | + // Erstelle eine Liste der TypeArgumentSyntax-Knoten basierend auf den übergebenen Strings |
| 19 | + var typeArgumentList = SyntaxFactory.TypeArgumentList( |
| 20 | + SyntaxFactory.SeparatedList<TypeSyntax>( |
| 21 | + typeArguments.Select(arg => SyntaxFactory.ParseTypeName(arg)) |
| 22 | + ) |
| 23 | + ); |
| 24 | + |
| 25 | + // Erstelle eine neue BaseList, wenn Typargumente vorhanden sind |
| 26 | + var newBaseList = |
| 27 | + symbol.BaseList != null |
| 28 | + ? symbol.BaseList.WithTypes( |
| 29 | + SyntaxFactory.SeparatedList<BaseTypeSyntax>( |
| 30 | + symbol.BaseList.Types.Select(bt => |
| 31 | + bt.WithType( |
| 32 | + SyntaxFactory.GenericName( |
| 33 | + ((IdentifierNameSyntax)bt.Type).Identifier, |
| 34 | + typeArgumentList |
| 35 | + ) |
| 36 | + ) |
| 37 | + ) |
| 38 | + ) |
| 39 | + ) |
| 40 | + : null; |
| 41 | + |
| 42 | + // Rückgabe einer neuen Klasse mit den modifizierten Basisklassen |
| 43 | + return symbol.WithBaseList(newBaseList); |
| 44 | + } |
| 45 | + |
| 46 | + public static bool HasAttribute<TAttribute>( |
| 47 | + this RecordDeclarationSyntax RecordDeclarationSyntax |
| 48 | + ) |
| 49 | + { |
| 50 | + // Check if the type declaration has the BaseContext attribute |
| 51 | + foreach (var attributeList in RecordDeclarationSyntax.AttributeLists) |
| 52 | + { |
| 53 | + foreach (var attribute in attributeList.Attributes) |
| 54 | + { |
| 55 | + var sdf = attribute.Name.ToFullString(); |
| 56 | + if (attribute.Name.ToString() == typeof(TAttribute).RealAttributeName()) |
| 57 | + { |
| 58 | + return true; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + public static string GetName(this RecordDeclarationSyntax RecordDeclaration) |
| 66 | + { |
| 67 | + return RecordDeclaration.Identifier.Text; |
| 68 | + } |
| 69 | + |
| 70 | + public static string GetNamespace(this RecordDeclarationSyntax RecordDeclaration) |
| 71 | + { |
| 72 | + // Get the root of the syntax tree |
| 73 | + var root = RecordDeclaration.SyntaxTree.GetRoot(); |
| 74 | + |
| 75 | + // Traverse up to find the NamespaceDeclarationSyntax or CompilationUnitSyntax |
| 76 | + var namespaceDeclaration = RecordDeclaration |
| 77 | + .Ancestors() |
| 78 | + .OfType<NamespaceDeclarationSyntax>() |
| 79 | + .FirstOrDefault(); |
| 80 | + if (namespaceDeclaration != null) |
| 81 | + { |
| 82 | + return namespaceDeclaration.Name.ToString(); |
| 83 | + } |
| 84 | + |
| 85 | + // If not found, check the compilation unit (the top-level container) |
| 86 | + var compilationUnit = RecordDeclaration |
| 87 | + .Ancestors() |
| 88 | + .OfType<CompilationUnitSyntax>() |
| 89 | + .FirstOrDefault(); |
| 90 | + if (compilationUnit != null) |
| 91 | + { |
| 92 | + return string.Empty; // No namespace, top-level code |
| 93 | + } |
| 94 | + |
| 95 | + // Return null or empty if not found |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + public static IEnumerable<IPropertySymbol>? GetAllPropertiesWithBaseClass( |
| 100 | + this RecordDeclarationSyntax RecordDeclaration, |
| 101 | + SemanticModel semanticModel, |
| 102 | + bool onlyPublic |
| 103 | + ) |
| 104 | + { |
| 105 | + var allProperties = new List<IPropertySymbol>(); |
| 106 | + |
| 107 | + var classSymbol = |
| 108 | + semanticModel.GetDeclaredSymbol(RecordDeclaration) as INamedTypeSymbol; |
| 109 | + if (classSymbol == null) |
| 110 | + return null; |
| 111 | + |
| 112 | + allProperties = classSymbol.GetAllProperties(true); |
| 113 | + if (onlyPublic) |
| 114 | + { |
| 115 | + return allProperties.Where(p => p.DeclaredAccessibility == Accessibility.Public); |
| 116 | + } |
| 117 | + |
| 118 | + return allProperties; |
| 119 | + } |
| 120 | + |
| 121 | + private static IEnumerable<INamedTypeSymbol> GetBaseTypes(INamedTypeSymbol type) |
| 122 | + { |
| 123 | + var baseTypes = new List<INamedTypeSymbol>(); |
| 124 | + var currentType = type.BaseType; |
| 125 | + |
| 126 | + while (currentType != null) |
| 127 | + { |
| 128 | + baseTypes.Add(currentType); |
| 129 | + currentType = currentType.BaseType; |
| 130 | + } |
| 131 | + |
| 132 | + return baseTypes; |
| 133 | + } |
| 134 | + |
| 135 | + public static List<PropertyDeclarationSyntax> GetAllProperties( |
| 136 | + this RecordDeclarationSyntax RecordDeclaration, |
| 137 | + bool onlyPublic |
| 138 | + ) |
| 139 | + { |
| 140 | + var properties = new List<PropertyDeclarationSyntax>(); |
| 141 | + |
| 142 | + var classProperties = RecordDeclaration.Members.OfType<PropertyDeclarationSyntax>(); |
| 143 | + if (onlyPublic) |
| 144 | + { |
| 145 | + classProperties = classProperties.Where(p => |
| 146 | + p.Modifiers.Any(m => m.IsKind(SyntaxKind.PublicKeyword)) |
| 147 | + ); |
| 148 | + } |
| 149 | + properties.AddRange(classProperties); |
| 150 | + |
| 151 | + return properties.ToList(); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments