| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | using System.Threading; |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.CSharp; |
| | | 5 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 6 | | |
| | | 7 | | namespace Hertzole.ScriptableValues.Generator; |
| | | 8 | | |
| | | 9 | | internal static class SyntaxExtensions |
| | | 10 | | { |
| | | 11 | | public static bool TryGetFieldDeclaration(this SyntaxNode node, out FieldDeclarationSyntax? fieldDeclaration) |
| | | 12 | | { |
| | 27 | 13 | | if (node.IsKind(SyntaxKind.FieldDeclaration)) |
| | | 14 | | { |
| | 0 | 15 | | fieldDeclaration = (FieldDeclarationSyntax) node; |
| | 0 | 16 | | return true; |
| | | 17 | | } |
| | | 18 | | |
| | 27 | 19 | | SyntaxNode? parent = node.Parent; |
| | 72 | 20 | | while (parent != null) |
| | | 21 | | { |
| | 60 | 22 | | if (parent.IsKind(SyntaxKind.FieldDeclaration)) |
| | | 23 | | { |
| | 15 | 24 | | fieldDeclaration = (FieldDeclarationSyntax) parent; |
| | 15 | 25 | | return true; |
| | | 26 | | } |
| | | 27 | | |
| | 45 | 28 | | parent = parent.Parent; |
| | | 29 | | } |
| | | 30 | | |
| | 12 | 31 | | fieldDeclaration = null; |
| | 12 | 32 | | return false; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | public static bool TryGetFieldOrPropertyWrapper(this SyntaxNode node, in SemanticModel semanticModel, out ImmutableA |
| | | 36 | | { |
| | 0 | 37 | | using ArrayBuilder<FieldOrPropertyWrapper> builder = new ArrayBuilder<FieldOrPropertyWrapper>(); |
| | | 38 | | |
| | 0 | 39 | | if (node is FieldDeclarationSyntax fieldSyntax) |
| | | 40 | | { |
| | 0 | 41 | | foreach (VariableDeclaratorSyntax variable in fieldSyntax.Declaration.Variables) |
| | | 42 | | { |
| | 0 | 43 | | ISymbol? declareSymbol = semanticModel.GetDeclaredSymbol(variable); |
| | 0 | 44 | | if (declareSymbol is not IFieldSymbol fieldSymbol) |
| | | 45 | | { |
| | | 46 | | continue; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | builder.Add(new FieldOrPropertyWrapper(fieldSymbol)); |
| | | 50 | | } |
| | | 51 | | } |
| | 0 | 52 | | else if (node is PropertyDeclarationSyntax propertySyntax) |
| | | 53 | | { |
| | 0 | 54 | | IPropertySymbol? symbol = semanticModel.GetDeclaredSymbol(propertySyntax); |
| | 0 | 55 | | if (symbol is not IPropertySymbol propertySymbol) |
| | | 56 | | { |
| | 0 | 57 | | return false; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | builder.Add(new FieldOrPropertyWrapper(propertySymbol)); |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | wrapper = builder.ToImmutable(); |
| | 0 | 64 | | return wrapper.Length > 0; |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | public static INamedTypeSymbol? GetAttributeSymbol(this AttributeSyntax syntax, SemanticModel semanticModel, Cancell |
| | | 68 | | { |
| | 0 | 69 | | if (semanticModel.GetSymbolInfo(syntax).Symbol is not IMethodSymbol methodSymbol) |
| | | 70 | | { |
| | 0 | 71 | | return null; |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | cancellation.ThrowIfCancellationRequested(); |
| | | 75 | | |
| | 0 | 76 | | if (methodSymbol.ContainingType is INamedTypeSymbol attributeSymbol) |
| | | 77 | | { |
| | 0 | 78 | | return attributeSymbol; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | return null; |
| | | 82 | | } |
| | | 83 | | } |