Skip to content

Commit

Permalink
fix(hr): Fix invalid code gen for custom markup extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Dec 9, 2024
1 parent cd8c0d8 commit 59533b9
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.Style
{
Expand All @@ -78,7 +77,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.Style
{
Expand All @@ -105,7 +103,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.DataTemplate(__ResourceOwner_1, (__owner) => new _MainPage_d6cd66944958ced0c513e0a04797b51d_TestReproMainPageSC0().Build(__owner)
) .GenericApply(__that, __nameScope, (ApplyMethod_6 ))
Expand Down Expand Up @@ -151,12 +148,12 @@ private void ApplyMethod_4(global::Microsoft.UI.Xaml.Style __p1, MainPage __that
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__p1, "OriginalSourceLocation", "file:///C:/Project/0/MainPage.xaml#L10:6");
}

private void ApplyMethod_6(global::System.Object /* oups */ __p1, MainPage __that, global::Microsoft.UI.Xaml.NameScope __nameScope)
private void ApplyMethod_6(global::System.Object __p1, MainPage __that, global::Microsoft.UI.Xaml.NameScope __nameScope)
{
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__p1, "OriginalSourceLocation", "file:///C:/Project/0/MainPage.xaml#L13:6");
}

private void ApplyMethod_7(global::System.Object /* oups */ __p1, MainPage __that, global::Microsoft.UI.Xaml.NameScope __nameScope)
private void ApplyMethod_7(global::System.Object __p1, MainPage __that, global::Microsoft.UI.Xaml.NameScope __nameScope)
{
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__p1, "OriginalSourceLocation", "file:///C:/Project/0/MainPage.xaml#L42:8");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.Style
{
Expand All @@ -78,7 +77,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.Style
{
Expand All @@ -105,7 +103,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.DataTemplate(__ResourceOwner_1, (__owner) => new _MainPage_d6cd66944958ced0c513e0a04797b51d_TestReproMainPageSC0().Build(__owner)
) .GenericApply(__that, __nameScope, (ApplyMethod_6 ))
Expand Down Expand Up @@ -147,7 +144,7 @@ private void ApplyMethod_4(global::Microsoft.UI.Xaml.Style __p1, MainPage __that
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__p1, "OriginalSourceLocation", "file:///C:/Project/0/MainPage.xaml#L10:6");
}

private void ApplyMethod_6(global::System.Object /* oups */ __p1, MainPage __that, global::Microsoft.UI.Xaml.NameScope __nameScope)
private void ApplyMethod_6(global::System.Object __p1, MainPage __that, global::Microsoft.UI.Xaml.NameScope __nameScope)
{
global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__p1, "OriginalSourceLocation", "file:///C:/Project/0/MainPage.xaml#L13:6");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.Style
{
Expand All @@ -78,7 +77,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.Style
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MyResourceDictionary)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.DataTemplate(__ResourceOwner_1, (__owner) => new _MyResourceDictionary_92716e07ff456818f6d4125e055d4d57_TestReproMyResourceDictionarySC0().Build(__owner)
) ;
Expand Down Expand Up @@ -214,7 +213,6 @@ private object Get_1(object __ResourceOwner_1) =>
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MyResourceDictionaryRD)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.DataTemplate(__ResourceOwner_1, (__owner) => new __Resources._MyResourceDictionary_92716e07ff456818f6d4125e055d4d57_MyResourceDictionaryRDSC2().Build(__owner)
) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private void InitializeComponent()
] =
new global::Uno.UI.Xaml.WeakResourceInitializer(this, __ResourceOwner_1 =>
{
// var __that = (MainPage)__ResourceOwner_1;
return
new global::Microsoft.UI.Xaml.Style
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2744,8 +2744,6 @@ private IDisposable BuildLazyResourceInitializer(IIndentedStringBuilder writer)

var bodyDisposable = writer.BlockInvariant($"new global::Uno.UI.Xaml.WeakResourceInitializer({currentScope}, {CurrentResourceOwner} => ");

writer.AppendLineInvariantIndented($"// var __that = ({CurrentScope.ClassName}){CurrentResourceOwner};");

writer.AppendLineInvariantIndented($"return ");

var indent = writer.Indent();
Expand Down Expand Up @@ -2997,8 +2995,6 @@ private void TryExtractAutomationId(XamlMemberDefinition member, string[] target

private void BuildExtendedProperties(IIndentedStringBuilder outerwriter, XamlObjectDefinition objectDefinition, bool useGenericApply = false)
{
useGenericApply = true;

_generatorContext.CancellationToken.ThrowIfCancellationRequested();

TryAnnotateWithGeneratorSource(outerwriter);
Expand Down Expand Up @@ -3117,7 +3113,7 @@ private void BuildExtendedProperties(IIndentedStringBuilder outerwriter, XamlObj
}
else
{
BuildCustomMarkupExtensionPropertyValue(writer, member, closureName);
BuildCustomMarkupExtensionPropertyValue(writer, member, closureName, $"(({CurrentScope.ClassName})__that)");
}
}
else if (member.Objects.Count > 0)
Expand Down Expand Up @@ -3479,9 +3475,9 @@ private void BuildExtendedProperties(IIndentedStringBuilder outerwriter, XamlObj
}

// Local function used to build a property/value for any custom MarkupExtensions
void BuildCustomMarkupExtensionPropertyValue(IIndentedStringBuilder writer, XamlMemberDefinition member, string closure)
void BuildCustomMarkupExtensionPropertyValue(IIndentedStringBuilder writer, XamlMemberDefinition member, string closure, string resourceOwner)
{
var propertyValue = GetCustomMarkupExtensionValue(member, closure);
var propertyValue = GetCustomMarkupExtensionValue(member, closure, resourceOwner);
if (!propertyValue.IsNullOrEmpty())
{
writer.AppendIndented($"{closure}.{member.Member.Name} = {propertyValue};\r\n");
Expand Down Expand Up @@ -3922,7 +3918,7 @@ private XamlLazyApplyBlockIIndentedStringBuilder CreateApplyBlock(IIndentedStrin
delegateType,
!_isTopLevelDictionary,
RegisterApplyMethod,
appliedType?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) ?? "global::System.Object /* oups */",
appliedType?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) ?? "global::System.Object",
CurrentScope.ClassName,
$"ApplyMethod_{(_applyIndex++).ToString(CultureInfo.InvariantCulture)}");
}
Expand Down Expand Up @@ -4577,7 +4573,7 @@ private string BuildMemberPropertyValue(XamlMemberDefinition m, bool isTemplateB
}
}

private string GetCustomMarkupExtensionValue(XamlMemberDefinition member, string? target = null)
private string GetCustomMarkupExtensionValue(XamlMemberDefinition member, string? target = null, string? resourceOwner = null)
{
// Get the type of the custom markup extension
var markup = member
Expand Down Expand Up @@ -4623,9 +4619,12 @@ private string GetCustomMarkupExtensionValue(XamlMemberDefinition member, string
.JoinBy(", ");
var markupInitializer = !properties.IsNullOrEmpty() ? $" {{ {properties} }}" : "()";

var thatCurrentResourceOwnerName = CurrentResourceOwnerName == "this"
? CurrentResourceOwnerName
: "__that." + CurrentResourceOwnerName;
var thatCurrentResourceOwnerName = resourceOwner switch
{
not null => resourceOwner,
null when CurrentResourceOwner is { } owner => "__that." + owner,
_ => "this"
};

// Build the parser context for ProvideValue(IXamlServiceProvider)
var providerDetails = new string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ internal class XamlLazyApplyBlockIIndentedStringBuilder : IIndentedStringBuilder
private readonly bool _exposeContext;
private readonly Action<string> _onRegisterApplyMethodBody;
private readonly string? _exposeContextMethod;
private readonly string? _appliedType;
private readonly string? _topLevelType;
private readonly string _appliedType;
private readonly string _topLevelType;

public XamlLazyApplyBlockIIndentedStringBuilder(
IIndentedStringBuilder source,
string closureName, string? applyPrefix,
string closureName,
string? applyPrefix,
string? delegateType,
bool exposeContext,
Action<string> onRegisterApplyMethodBody,
string? appliedType,
string? topLevelType,
string appliedType,
string topLevelType,
string? exposeContextMethod,
IDisposable? parentDisposable = null)
{
Expand Down

0 comments on commit 59533b9

Please sign in to comment.