|
| 1 | +using System.Collections.Immutable; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | + |
| 7 | +namespace LVGLSharp.Analyzers; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Enforces the demo layout pattern that LVGL currently renders reliably. |
| 11 | +/// </summary> |
| 12 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 13 | +public sealed class DemoLayoutAnalyzer : DiagnosticAnalyzer |
| 14 | +{ |
| 15 | + private const string Category = "Layout"; |
| 16 | + private const string TableLayoutPanelMetadataName = "System.Windows.Forms.TableLayoutPanel"; |
| 17 | + private const string LvglTableLayoutPanelMetadataName = "LVGLSharp.Forms.TableLayoutPanel"; |
| 18 | + private const string FlowLayoutPanelMetadataName = "System.Windows.Forms.FlowLayoutPanel"; |
| 19 | + private const string LvglFlowLayoutPanelMetadataName = "LVGLSharp.Forms.FlowLayoutPanel"; |
| 20 | + private const string SizeTypeMetadataName = "System.Windows.Forms.SizeType"; |
| 21 | + private const string LvglSizeTypeMetadataName = "LVGLSharp.Forms.SizeType"; |
| 22 | + |
| 23 | + private static readonly LocalizableString DirectChildTitle = new LocalizableResourceString("DirectChildRuleTitle", AnalyzerResources.ResourceManager, typeof(AnalyzerResources)); |
| 24 | + private static readonly LocalizableString DirectChildMessage = new LocalizableResourceString("DirectChildRuleMessage", AnalyzerResources.ResourceManager, typeof(AnalyzerResources)); |
| 25 | + private static readonly LocalizableString DirectChildDescription = new LocalizableResourceString("DirectChildRuleDescription", AnalyzerResources.ResourceManager, typeof(AnalyzerResources)); |
| 26 | + private static readonly LocalizableString AbsoluteRowTitle = new LocalizableResourceString("AbsoluteRowRuleTitle", AnalyzerResources.ResourceManager, typeof(AnalyzerResources)); |
| 27 | + private static readonly LocalizableString AbsoluteRowMessage = new LocalizableResourceString("AbsoluteRowRuleMessage", AnalyzerResources.ResourceManager, typeof(AnalyzerResources)); |
| 28 | + private static readonly LocalizableString AbsoluteRowDescription = new LocalizableResourceString("AbsoluteRowRuleDescription", AnalyzerResources.ResourceManager, typeof(AnalyzerResources)); |
| 29 | + |
| 30 | + internal static readonly DiagnosticDescriptor DirectChildRule = new( |
| 31 | + id: "LVGL001", |
| 32 | + title: DirectChildTitle, |
| 33 | + messageFormat: DirectChildMessage, |
| 34 | + category: Category, |
| 35 | + defaultSeverity: DiagnosticSeverity.Warning, |
| 36 | + isEnabledByDefault: true, |
| 37 | + description: DirectChildDescription); |
| 38 | + |
| 39 | + internal static readonly DiagnosticDescriptor AbsoluteRowRule = new( |
| 40 | + id: "LVGL002", |
| 41 | + title: AbsoluteRowTitle, |
| 42 | + messageFormat: AbsoluteRowMessage, |
| 43 | + category: Category, |
| 44 | + defaultSeverity: DiagnosticSeverity.Warning, |
| 45 | + isEnabledByDefault: true, |
| 46 | + description: AbsoluteRowDescription); |
| 47 | + |
| 48 | + /// <inheritdoc /> |
| 49 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [DirectChildRule, AbsoluteRowRule]; |
| 50 | + |
| 51 | + /// <inheritdoc /> |
| 52 | + public override void Initialize(AnalysisContext context) |
| 53 | + { |
| 54 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); |
| 55 | + context.EnableConcurrentExecution(); |
| 56 | + context.RegisterSyntaxNodeAction(AnalyzeInvocation, SyntaxKind.InvocationExpression); |
| 57 | + } |
| 58 | + |
| 59 | + private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context) |
| 60 | + { |
| 61 | + if (!IsDemoDesignerFile(context.Node.SyntaxTree)) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + if (context.Node is not InvocationExpressionSyntax invocation || invocation.Expression is not MemberAccessExpressionSyntax memberAccess) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + if (memberAccess.Name.Identifier.ValueText != "Add" || memberAccess.Expression is not MemberAccessExpressionSyntax collectionAccess) |
| 72 | + { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + switch (collectionAccess.Name.Identifier.ValueText) |
| 77 | + { |
| 78 | + case "Controls": |
| 79 | + AnalyzeControlsAdd(context, invocation, collectionAccess); |
| 80 | + break; |
| 81 | + |
| 82 | + case "RowStyles": |
| 83 | + AnalyzeRowStylesAdd(context, invocation, collectionAccess); |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private static void AnalyzeControlsAdd(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax collectionAccess) |
| 89 | + { |
| 90 | + if (!IsTableLayoutPanel(context.SemanticModel.GetTypeInfo(collectionAccess.Expression, context.CancellationToken).Type)) |
| 91 | + { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (invocation.ArgumentList.Arguments.Count == 0) |
| 96 | + { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + var childExpression = invocation.ArgumentList.Arguments[0].Expression; |
| 101 | + var childType = context.SemanticModel.GetTypeInfo(childExpression, context.CancellationToken).Type; |
| 102 | + if (IsFlowLayoutPanel(childType)) |
| 103 | + { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + var childTypeName = childType?.Name ?? childExpression.ToString(); |
| 108 | + context.ReportDiagnostic(Diagnostic.Create(DirectChildRule, childExpression.GetLocation(), childTypeName)); |
| 109 | + } |
| 110 | + |
| 111 | + private static void AnalyzeRowStylesAdd(SyntaxNodeAnalysisContext context, InvocationExpressionSyntax invocation, MemberAccessExpressionSyntax collectionAccess) |
| 112 | + { |
| 113 | + if (!IsTableLayoutPanel(context.SemanticModel.GetTypeInfo(collectionAccess.Expression, context.CancellationToken).Type)) |
| 114 | + { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + if (invocation.ArgumentList.Arguments.Count == 0) |
| 119 | + { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + if (invocation.ArgumentList.Arguments[0].Expression is not ObjectCreationExpressionSyntax objectCreation || objectCreation.ArgumentList is null) |
| 124 | + { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + if (objectCreation.ArgumentList.Arguments.Count == 0) |
| 129 | + { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + var sizeTypeExpression = objectCreation.ArgumentList.Arguments[0].Expression; |
| 134 | + if (!IsPercentSizeType(context.SemanticModel.GetSymbolInfo(sizeTypeExpression, context.CancellationToken).Symbol)) |
| 135 | + { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + context.ReportDiagnostic(Diagnostic.Create(AbsoluteRowRule, sizeTypeExpression.GetLocation())); |
| 140 | + } |
| 141 | + |
| 142 | + private static bool IsDemoDesignerFile(SyntaxTree syntaxTree) |
| 143 | + { |
| 144 | + var filePath = syntaxTree.FilePath; |
| 145 | + if (string.IsNullOrWhiteSpace(filePath)) |
| 146 | + { |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + var normalizedPath = filePath.Replace('\\', '/'); |
| 151 | + return normalizedPath.Contains("/src/Demos/", StringComparison.OrdinalIgnoreCase) |
| 152 | + && normalizedPath.EndsWith(".Designer.cs", StringComparison.OrdinalIgnoreCase); |
| 153 | + } |
| 154 | + |
| 155 | + private static bool IsTableLayoutPanel(ITypeSymbol? typeSymbol) |
| 156 | + => IsNamedType(typeSymbol, TableLayoutPanelMetadataName) || IsNamedType(typeSymbol, LvglTableLayoutPanelMetadataName); |
| 157 | + |
| 158 | + private static bool IsFlowLayoutPanel(ITypeSymbol? typeSymbol) |
| 159 | + => IsNamedType(typeSymbol, FlowLayoutPanelMetadataName) || IsNamedType(typeSymbol, LvglFlowLayoutPanelMetadataName); |
| 160 | + |
| 161 | + private static bool IsPercentSizeType(ISymbol? symbol) |
| 162 | + => symbol is IFieldSymbol fieldSymbol |
| 163 | + && fieldSymbol.Name == "Percent" |
| 164 | + && (fieldSymbol.ContainingType.ToDisplayString() == SizeTypeMetadataName || fieldSymbol.ContainingType.ToDisplayString() == LvglSizeTypeMetadataName); |
| 165 | + |
| 166 | + private static bool IsNamedType(ITypeSymbol? typeSymbol, string metadataName) |
| 167 | + { |
| 168 | + for (var current = typeSymbol; current is not null; current = current.BaseType) |
| 169 | + { |
| 170 | + if (current.ToDisplayString() == metadataName) |
| 171 | + { |
| 172 | + return true; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return false; |
| 177 | + } |
| 178 | +} |
0 commit comments