Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions src/Controls/src/SourceGen/Visitors/ExpandMarkupsVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@

namespace Microsoft.Maui.Controls.SourceGen;

class ExpandMarkupsVisitor : IXamlNodeVisitor
class ExpandMarkupsVisitor(SourceGenContext context) : IXamlNodeVisitor
{
public ExpandMarkupsVisitor(SourceGenContext context) => Context = context;
record XmlLineInfoProvider(IXmlLineInfo XmlLineInfo) : IXmlLineInfoProvider
{
}

record SGContextProvider(SourceGenContext Context)
{
}

public static readonly IList<XmlName> Skips =
[
Expand All @@ -19,7 +25,7 @@ class ExpandMarkupsVisitor : IXamlNodeVisitor
XmlName.xName,
];

SourceGenContext Context { get; }
SourceGenContext Context { get; } = context;
public TreeVisitingMode VisitingMode => TreeVisitingMode.BottomUp;
public bool StopOnDataTemplate => false;
public bool StopOnResourceDictionary => false;
Expand Down Expand Up @@ -63,7 +69,7 @@ public void Visit(ListNode node, INode parentNode)
INode? ParseExpression(ref string expression, IXmlNamespaceResolver nsResolver, IXmlLineInfo xmlLineInfo, INode node, INode parentNode)
{
if (expression.StartsWith("{}", StringComparison.Ordinal))
return new ValueNode(expression.Substring(2), null);
return new ValueNode(expression.Substring(2), null, xmlLineInfo?.LineNumber ?? -1, xmlLineInfo?.LinePosition ?? -1);

if (expression[expression.Length - 1] != '}')
{
Expand Down Expand Up @@ -92,16 +98,12 @@ public void Visit(ListNode node, INode parentNode)
var serviceProvider = new XamlServiceProvider(node, Context);
serviceProvider.Add(typeof(IXmlNamespaceResolver), nsResolver);
serviceProvider.Add(typeof(SGContextProvider), new SGContextProvider(Context));
if (xmlLineInfo != null)
serviceProvider.Add(typeof(IXmlLineInfoProvider), new XmlLineInfoProvider(xmlLineInfo));

return new MarkupExpansionParser().Parse(match!, ref expression, serviceProvider);
}

class SGContextProvider
{
public SGContextProvider(SourceGenContext context) => Context = context;

public SourceGenContext Context { get; }
}

public class MarkupExpansionParser : MarkupExpressionParser, IExpressionParser<INode>
{
Expand All @@ -110,7 +112,7 @@ public class MarkupExpansionParser : MarkupExpressionParser, IExpressionParser<I

public INode Parse(string match, ref string remaining, IServiceProvider serviceProvider)
{
if (!(serviceProvider.GetService(typeof(IXmlNamespaceResolver)) is IXmlNamespaceResolver nsResolver))
if (serviceProvider.GetService(typeof(IXmlNamespaceResolver)) is not IXmlNamespaceResolver nsResolver)
throw new ArgumentException();
IXmlLineInfo? xmlLineInfo = null;
if (serviceProvider.GetService(typeof(IXmlLineInfoProvider)) is IXmlLineInfoProvider xmlLineInfoProvider)
Expand Down Expand Up @@ -149,7 +151,10 @@ public INode Parse(string match, ref string remaining, IServiceProvider serviceP
catch (XamlParseException xpe)
{
if (contextProvider != null)
{
contextProvider.Context.ReportDiagnostic(Diagnostic.Create(Descriptors.XamlParserError, LocationHelpers.LocationCreate(contextProvider.Context.ProjectItem.RelativePath!, xmlLineInfo!, match), xpe.Message));
return null!;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't ask what changed for that return null to be needed, I have no idea

}
else
throw;
}
Expand All @@ -174,11 +179,11 @@ public INode Parse(string match, ref string remaining, IServiceProvider serviceP
if (childname == XmlName.xTypeArguments)
{
typeArguments = TypeArgumentsParser.ParseExpression(parsed.strValue, nsResolver, xmlLineInfo);
childnodes.Add((childname, new ValueNode(typeArguments, nsResolver)));
childnodes.Add((childname, new ValueNode(typeArguments, nsResolver, xmlLineInfo?.LineNumber ?? -1, xmlLineInfo?.LinePosition ?? -1)));
}
else
{
var childnode = parsed.value as INode ?? new ValueNode(parsed.strValue, nsResolver);
var childnode = parsed.value as INode ?? new ValueNode(parsed.strValue, nsResolver, xmlLineInfo?.LineNumber ?? -1, xmlLineInfo?.LinePosition ?? -1);
childnodes.Add((childname, childnode));
}
}
Expand All @@ -198,9 +203,7 @@ public INode Parse(string match, ref string remaining, IServiceProvider serviceP
throw new NotSupportedException();


_node = xmlLineInfo == null
? new ElementNode(xmltype, null, nsResolver)
: new ElementNode(xmltype, null, nsResolver, xmlLineInfo.LineNumber, xmlLineInfo.LinePosition);
_node = new ElementNode(xmltype, null, nsResolver, xmlLineInfo?.LineNumber ?? -1, xmlLineInfo?.LinePosition ?? -1);

foreach (var (childname, childnode) in childnodes)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public partial class TestPage
private partial void InitializeComponent()
{
var bindingExtension = new global::Microsoft.Maui.Controls.Xaml.BindingExtension();
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingExtension!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), -1, -1);
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingExtension!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 8, 5);
var __root = this;
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 2, 2);
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
Expand All @@ -82,12 +82,12 @@ private partial void InitializeComponent()
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
global::Microsoft.Maui.Controls.Internals.NameScope.SetNameScope(__root, iNameScope);
#endif
#line 1 "{{testXamlFilePath}}"
#line 8 "{{testXamlFilePath}}"
bindingExtension.Path = "Foo.Bar.Title";
#line default
var bindingBase = CreateTypedBindingFrom_bindingExtension(bindingExtension);
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(bindingBase!) == null)
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingBase!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), -1, -1);
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingBase!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 8, 5);
__root.SetBinding(global::Microsoft.Maui.Controls.Page.TitleProperty, bindingBase);
static global::Microsoft.Maui.Controls.BindingBase CreateTypedBindingFrom_bindingExtension(global::Microsoft.Maui.Controls.Xaml.BindingExtension extension)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public void Test()
<x:Int32>32</x:Int32>
</x:Array>
</ContentPage.Resources>
<Label Text="{Binding Path=., Converter={StaticResource reverseConverter}}" x:DataType="x:String"/>
</ContentPage>
""";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.SourceGen.UnitTests.InitializeComponent;
namespace Microsoft.Maui.Controls.SourceGen.UnitTests;

public class LineInfoTests : SourceGenXamlInitializeComponentTestBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public partial class TestPage
private partial void InitializeComponent()
{
var bindingExtension = new global::Microsoft.Maui.Controls.Xaml.BindingExtension();
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingExtension!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), -1, -1);
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingExtension!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 6, 5);
var __root = this;
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 2, 2);
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
Expand All @@ -68,12 +68,12 @@ private partial void InitializeComponent()
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
global::Microsoft.Maui.Controls.Internals.NameScope.SetNameScope(__root, iNameScope);
#endif
#line 1 "{{testXamlFilePath}}"
#line 6 "{{testXamlFilePath}}"
bindingExtension.Path = "Title";
#line default
var bindingBase = new global::Microsoft.Maui.Controls.Binding(bindingExtension.Path, bindingExtension.Mode, bindingExtension.Converter, bindingExtension.ConverterParameter, bindingExtension.StringFormat, bindingExtension.Source) { UpdateSourceEventName = bindingExtension.UpdateSourceEventName, FallbackValue = bindingExtension.FallbackValue, TargetNullValue = bindingExtension.TargetNullValue };
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(bindingBase!) == null)
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingBase!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), -1, -1);
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingBase!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 6, 5);
__root.SetBinding(global::Microsoft.Maui.Controls.Page.TitleProperty, bindingBase);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.SourceGen.UnitTests.InitializeComponent;
namespace Microsoft.Maui.Controls.SourceGen.UnitTests;

public class SimplifyOnPlatform : SourceGenXamlInitializeComponentTestBase
{
Expand Down Expand Up @@ -149,7 +149,7 @@ private partial void InitializeComponent()
xamlServiceProvider2.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IXamlTypeResolver), new global::Microsoft.Maui.Controls.Xaml.Internals.XamlTypeResolver(xmlNamespaceResolver2, typeof(global::Test.TestPage).Assembly));
setter1.Property = ((global::Microsoft.Maui.Controls.IExtendedTypeConverter)new global::Microsoft.Maui.Controls.BindablePropertyConverter()).ConvertFromInvariantString("IsVisible", xamlServiceProvider2) as global::Microsoft.Maui.Controls.BindableProperty;
#line default
#line 1 "{{testXamlFilePath}}"
#line 9 "{{testXamlFilePath}}"
setter1.Value = "True";
#line default
var setter3 = new global::Microsoft.Maui.Controls.Setter {Property = global::Microsoft.Maui.Controls.VisualElement.IsVisibleProperty, Value = (bool)new global::Microsoft.Maui.Controls.VisualElement.VisibilityConverter().ConvertFromInvariantString("True")!};
Expand Down
Loading