From 4ea62c6a3ab1a5843600b24aad890855d00f0616 Mon Sep 17 00:00:00 2001 From: tesar-tech Date: Thu, 28 Nov 2024 16:32:45 +0100 Subject: [PATCH] Blazorise constants with numeric values. Fix for enums from different namespaces (e.g. Snackbar) --- .../Helpers/DefaultValueHelper.cs | 16 +++++++++++--- .../Helpers/FullyQualifiedNameRewriter.cs | 2 +- .../Helpers/InfoExtractor.cs | 2 +- .../Helpers/StringHelpers.cs | 22 +++++++++++++++---- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/DefaultValueHelper.cs b/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/DefaultValueHelper.cs index ad5b33ce5e..761f15381c 100644 --- a/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/DefaultValueHelper.cs +++ b/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/DefaultValueHelper.cs @@ -112,7 +112,7 @@ private static Optional TryToGetDefaultValueFromGetter( Compilation comp ExpressionSyntax fieldInitializerValue = variableDeclarator.Initializer.Value; - string expressionString = QualifyExpressionSyntax( compilation, fieldInitializerValue ); + object expressionString = QualifyExpressionSyntax( compilation, fieldInitializerValue ); return new Optional( expressionString ); } @@ -128,17 +128,27 @@ private static Optional TryToGetConstantValueFromPropertyInitializer( Co // Get the expression representing the initializer value ExpressionSyntax initializerValue = propertySyntax.Initializer.Value; - string expressionString = QualifyExpressionSyntax( compilation, initializerValue ); + object expressionString = QualifyExpressionSyntax( compilation, initializerValue ); return new Optional( expressionString ); } - private static string QualifyExpressionSyntax( Compilation compilation, ExpressionSyntax expression ) + private static object QualifyExpressionSyntax( Compilation compilation, ExpressionSyntax expression ) { var semanticModel = compilation.GetSemanticModel( expression.SyntaxTree ); var rewriter = new FullyQualifiedNameRewriter( semanticModel ); var qualifiedExpression = rewriter.Visit( expression ); + if ( qualifiedExpression.ToString().Contains( "Blazorise" ) ) + {//this will process constants, but only Blazorise constants. Blazorise.Snackbar.Constants.DefaultIntervalBeforeClose will return value (5000), otherwise the constant name (int.MaxValue) + var symbolInfo = semanticModel.GetSymbolInfo( expression ); + if ( symbolInfo.Symbol is IFieldSymbol { IsConst: true } fieldSymbol ) + { + // If the symbol is a constant field, return its value + return fieldSymbol.ConstantValue; + } + } + return qualifiedExpression.ToString(); } diff --git a/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/FullyQualifiedNameRewriter.cs b/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/FullyQualifiedNameRewriter.cs index 4ee18c587d..3c72e8024c 100644 --- a/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/FullyQualifiedNameRewriter.cs +++ b/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/FullyQualifiedNameRewriter.cs @@ -47,7 +47,7 @@ public override SyntaxNode VisitMemberAccessExpression(MemberAccessExpressionSyn // Parse the fully qualified name into an expression var fullyQualifiedExpression = SyntaxFactory.ParseExpression(fullyQualifiedName) .WithTriviaFrom(node); - + return fullyQualifiedExpression; } // Reconstruct the member access expression with updated components diff --git a/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/InfoExtractor.cs b/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/InfoExtractor.cs index 016fd6577f..a2f025e1ca 100644 --- a/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/InfoExtractor.cs +++ b/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/InfoExtractor.cs @@ -38,7 +38,7 @@ public static ApiDocsForComponentProperty GetPropertyDetails( Compilation compil }; string defaultValueAsString = property.Type.Name == "String" ? defaultValueString : $"""" $$""" - {StringHelpers.TypeToStringDetails( defaultValueString, propertyDetails.TypeName )} + {StringHelpers.TypeToStringDetails( defaultValueString, propertyDetails.Type )} """ """"; propertyDetails.DefaultValueString = defaultValueAsString; diff --git a/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/StringHelpers.cs b/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/StringHelpers.cs index 485511671b..2070fdc4bc 100644 --- a/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/StringHelpers.cs +++ b/Source/SourceGenerators/Blazorise.ApiDocsGenerator/Helpers/StringHelpers.cs @@ -43,19 +43,33 @@ public static string FormatProperly( object value ) const string toReplace = "global::"; const string toReplace2 = "Blazorise."; - public static string TypeToStringDetails( string value, string propertyDetailsTypeName ) + public static string TypeToStringDetails( string value, string propertyDetailsType ) { - + if ( value.StartsWith( toReplace ) ) { value = value.Substring( toReplace.Length ); } + + if ( value.Contains( toReplace2 ) ) + { + string propertyDetailsTypeNameWithDot = propertyDetailsType.Replace(toReplace,"") + ".";//From Color.Default => Default + + if ( value.Contains( propertyDetailsTypeNameWithDot ) ) + { + value = value.Substring( propertyDetailsTypeNameWithDot.Length ); + Logger.Log($"Returning {value}"); + + return value; + } + } + if ( value.StartsWith( toReplace2 ) ) { - value = value.Substring( toReplace2.Length ); - string propertyDetailsTypeNameWithDot = propertyDetailsTypeName + ".";//From Color.Default => Default + string propertyDetailsTypeNameWithDot = propertyDetailsType + ".";//From Color.Default => Default if ( value.StartsWith( propertyDetailsTypeNameWithDot ) ) value = value.Substring( propertyDetailsTypeNameWithDot.Length ); + value = value.Substring( toReplace2.Length ); } return value;