From db310920f4c23acf3858280b7d65182df7807a4d Mon Sep 17 00:00:00 2001 From: yaobiao131 <28655758+yaobiao131@users.noreply.github.com> Date: Fri, 4 Apr 2025 19:40:26 +0800 Subject: [PATCH 1/4] fix: Ensure that MarkupExtension search behavior is consistent with winui, and prioritize classes with Extension extensions --- .../XamlGenerator/XamlFileGenerator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index 15c8c3fb59ed..630ef6172e6b 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -1996,8 +1996,8 @@ private bool HasDescendantsWith(XamlObjectDefinition xamlObjectDefinition, Func< // the fully qualified type name. // In this case, we go through this code path as it's much more efficient than FindType. var baseTypeString = $"{ns}.{xamlType.Name}"; - findType = _metadataHelper.FindTypeByFullName(baseTypeString) as INamedTypeSymbol; - findType ??= _metadataHelper.FindTypeByFullName(baseTypeString + "Extension") as INamedTypeSymbol; // Support shortened syntax + findType = _metadataHelper.FindTypeByFullName(baseTypeString + "Extension") as INamedTypeSymbol; // Support shortened syntax + findType ??= _metadataHelper.FindTypeByFullName(baseTypeString) as INamedTypeSymbol; } else { From e326d7a7a27483787942bc89cc3ad7f6f2dbd7bf Mon Sep 17 00:00:00 2001 From: yaobiao131 <28655758+yaobiao131@users.noreply.github.com> Date: Sat, 26 Apr 2025 21:52:44 +0800 Subject: [PATCH 2/4] fix: Update XamlFileGenerator.cs --- .../XamlGenerator/XamlFileGenerator.cs | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index 630ef6172e6b..9bcdc124996a 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -1989,29 +1989,26 @@ private bool HasDescendantsWith(XamlObjectDefinition xamlObjectDefinition, Func< var ns = GetTrimmedNamespace(xamlType.PreferredXamlNamespace); // No MarkupExtensions are defined in the framework, so we expect a user-defined namespace INamedTypeSymbol? findType; - if (ns != xamlType.PreferredXamlNamespace) - { - // If GetTrimmedNamespace returned a different string, it's a "using:"-prefixed namespace. - // In this case, we'll have `baseTypeString` as - // the fully qualified type name. - // In this case, we go through this code path as it's much more efficient than FindType. - var baseTypeString = $"{ns}.{xamlType.Name}"; - findType = _metadataHelper.FindTypeByFullName(baseTypeString + "Extension") as INamedTypeSymbol; // Support shortened syntax - findType ??= _metadataHelper.FindTypeByFullName(baseTypeString) as INamedTypeSymbol; - } - else - { - // It looks like FindType always returns null in this code path. + if (ns == xamlType.PreferredXamlNamespace) + { + // It looks like FindType always returns null in this code path. // So, we avoid the costly call here. - return null; - } - - if (findType?.Is(Generation.MarkupExtensionSymbol.Value) ?? false) - { - return findType; - } - - return null; + return null; + } + + // If GetTrimmedNamespace returned a different string, it's a "using:"-prefixed namespace. + // In this case, we'll have `baseTypeString` as + // the fully qualified type name. + // In this case, we go through this code path as it's much more efficient than FindType. + var baseTypeString = $"{ns}.{xamlType.Name}"; + + // Try finding the type with "Extension" suffix first, then without + return FindMarkupExtensionType(baseTypeString + "Extension") ?? FindMarkupExtensionType(baseTypeString); + } + + private INamedTypeSymbol? FindMarkupExtensionType(string fullTypeName) + { + return _metadataHelper.FindTypeByFullName(fullTypeName) is INamedTypeSymbol type && type.Is(Generation.MarkupExtensionSymbol.Value) ? type : null; } private bool IsCustomMarkupExtensionType(XamlType? xamlType) => From 399668f4df43f8fc86ded3790ec1e714d081486d Mon Sep 17 00:00:00 2001 From: yaobiao131 <28655758+yaobiao131@users.noreply.github.com> Date: Sat, 26 Apr 2025 23:07:29 +0800 Subject: [PATCH 3/4] fix: remove unused variable --- .../Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index 9bcdc124996a..c26d18a9fd86 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -1988,7 +1988,6 @@ private bool HasDescendantsWith(XamlObjectDefinition xamlObjectDefinition, Func< } var ns = GetTrimmedNamespace(xamlType.PreferredXamlNamespace); // No MarkupExtensions are defined in the framework, so we expect a user-defined namespace - INamedTypeSymbol? findType; if (ns == xamlType.PreferredXamlNamespace) { // It looks like FindType always returns null in this code path. From 730fe7788c77ecd2fd914ff4f3d50eab3c077131 Mon Sep 17 00:00:00 2001 From: yaobiao131 <28655758+yaobiao131@users.noreply.github.com> Date: Sat, 26 Apr 2025 23:24:59 +0800 Subject: [PATCH 4/4] chore: Adjust Styles --- .../XamlGenerator/XamlFileGenerator.cs | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index c26d18a9fd86..1a74a4065552 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -1989,25 +1989,25 @@ private bool HasDescendantsWith(XamlObjectDefinition xamlObjectDefinition, Func< var ns = GetTrimmedNamespace(xamlType.PreferredXamlNamespace); // No MarkupExtensions are defined in the framework, so we expect a user-defined namespace if (ns == xamlType.PreferredXamlNamespace) - { - // It looks like FindType always returns null in this code path. + { + // It looks like FindType always returns null in this code path. // So, we avoid the costly call here. - return null; - } - + return null; + } + // If GetTrimmedNamespace returned a different string, it's a "using:"-prefixed namespace. // In this case, we'll have `baseTypeString` as // the fully qualified type name. // In this case, we go through this code path as it's much more efficient than FindType. - var baseTypeString = $"{ns}.{xamlType.Name}"; - - // Try finding the type with "Extension" suffix first, then without - return FindMarkupExtensionType(baseTypeString + "Extension") ?? FindMarkupExtensionType(baseTypeString); + var baseTypeString = $"{ns}.{xamlType.Name}"; + + // Try finding the type with "Extension" suffix first, then without + return FindMarkupExtensionType(baseTypeString + "Extension") ?? FindMarkupExtensionType(baseTypeString); } private INamedTypeSymbol? FindMarkupExtensionType(string fullTypeName) { - return _metadataHelper.FindTypeByFullName(fullTypeName) is INamedTypeSymbol type && type.Is(Generation.MarkupExtensionSymbol.Value) ? type : null; + return _metadataHelper.FindTypeByFullName(fullTypeName) is INamedTypeSymbol type && type.Is(Generation.MarkupExtensionSymbol.Value) ? type : null; } private bool IsCustomMarkupExtensionType(XamlType? xamlType) =>