Skip to content

Commit c00c1b6

Browse files
[XSG] Correct lineinfo for expanded markups
- fixes #30855
1 parent a373335 commit c00c1b6

File tree

7 files changed

+29
-22
lines changed

7 files changed

+29
-22
lines changed

src/Controls/src/SourceGen/Visitors/ExpandMarkupsVisitor.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77

88
namespace Microsoft.Maui.Controls.SourceGen;
99

10-
class ExpandMarkupsVisitor : IXamlNodeVisitor
10+
class ExpandMarkupsVisitor(SourceGenContext context) : IXamlNodeVisitor
1111
{
12-
public ExpandMarkupsVisitor(SourceGenContext context) => Context = context;
12+
record XmlLineInfoProvider(IXmlLineInfo XmlLineInfo) : IXmlLineInfoProvider
13+
{
14+
}
15+
16+
record SGContextProvider(SourceGenContext Context)
17+
{
18+
}
1319

1420
public static readonly IList<XmlName> Skips =
1521
[
@@ -19,7 +25,7 @@ class ExpandMarkupsVisitor : IXamlNodeVisitor
1925
XmlName.xName,
2026
];
2127

22-
SourceGenContext Context { get; }
28+
SourceGenContext Context { get; } = context;
2329
public TreeVisitingMode VisitingMode => TreeVisitingMode.BottomUp;
2430
public bool StopOnDataTemplate => false;
2531
public bool StopOnResourceDictionary => false;
@@ -63,7 +69,7 @@ public void Visit(ListNode node, INode parentNode)
6369
INode? ParseExpression(ref string expression, IXmlNamespaceResolver nsResolver, IXmlLineInfo xmlLineInfo, INode node, INode parentNode)
6470
{
6571
if (expression.StartsWith("{}", StringComparison.Ordinal))
66-
return new ValueNode(expression.Substring(2), null);
72+
return new ValueNode(expression.Substring(2), null, xmlLineInfo);
6773

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

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

99-
class SGContextProvider
100-
{
101-
public SGContextProvider(SourceGenContext context) => Context = context;
102-
103-
public SourceGenContext Context { get; }
104-
}
105107

106108
public class MarkupExpansionParser : MarkupExpressionParser, IExpressionParser<INode>
107109
{
@@ -110,7 +112,7 @@ public class MarkupExpansionParser : MarkupExpressionParser, IExpressionParser<I
110112

111113
public INode Parse(string match, ref string remaining, IServiceProvider serviceProvider)
112114
{
113-
if (!(serviceProvider.GetService(typeof(IXmlNamespaceResolver)) is IXmlNamespaceResolver nsResolver))
115+
if (serviceProvider.GetService(typeof(IXmlNamespaceResolver)) is not IXmlNamespaceResolver nsResolver)
114116
throw new ArgumentException();
115117
IXmlLineInfo? xmlLineInfo = null;
116118
if (serviceProvider.GetService(typeof(IXmlLineInfoProvider)) is IXmlLineInfoProvider xmlLineInfoProvider)
@@ -174,11 +176,11 @@ public INode Parse(string match, ref string remaining, IServiceProvider serviceP
174176
if (childname == XmlName.xTypeArguments)
175177
{
176178
typeArguments = TypeArgumentsParser.ParseExpression(parsed.strValue, nsResolver, xmlLineInfo);
177-
childnodes.Add((childname, new ValueNode(typeArguments, nsResolver)));
179+
childnodes.Add((childname, new ValueNode(typeArguments, nsResolver, xmlLineInfo)));
178180
}
179181
else
180182
{
181-
var childnode = parsed.value as INode ?? new ValueNode(parsed.strValue, nsResolver);
183+
var childnode = parsed.value as INode ?? new ValueNode(parsed.strValue, nsResolver, xmlLineInfo);
182184
childnodes.Add((childname, childnode));
183185
}
184186
}

src/Controls/src/Xaml/XamlNode.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ abstract class BaseNode(IXmlNamespaceResolver namespaceResolver, int linenumber
4949
[DebuggerDisplay("{Value}")]
5050
class ValueNode(object value, IXmlNamespaceResolver namespaceResolver, int linenumber = -1, int lineposition = -1) : BaseNode(namespaceResolver, linenumber, lineposition), IValueNode
5151
{
52+
public ValueNode(object value, IXmlNamespaceResolver namespaceResolver, IXmlLineInfo lineInfo) : this(value, namespaceResolver, lineInfo != null ? lineInfo.LineNumber : -1, lineInfo != null ? lineInfo.LinePosition : -1)
53+
{
54+
}
55+
5256
public object Value { get; set; } = value;
5357

5458
public override void Accept(IXamlNodeVisitor visitor, INode parentNode)

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/CompiledBindings.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public partial class TestPage
7373
private partial void InitializeComponent()
7474
{
7575
var bindingExtension = new global::Microsoft.Maui.Controls.Xaml.BindingExtension();
76-
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingExtension!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), -1, -1);
76+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingExtension!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 8, 5);
7777
var __root = this;
7878
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 2, 2);
7979
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
@@ -82,12 +82,12 @@ private partial void InitializeComponent()
8282
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
8383
global::Microsoft.Maui.Controls.Internals.NameScope.SetNameScope(__root, iNameScope);
8484
#endif
85-
#line 1 "{{testXamlFilePath}}"
85+
#line 8 "{{testXamlFilePath}}"
8686
bindingExtension.Path = "Foo.Bar.Title";
8787
#line default
8888
var bindingBase = CreateTypedBindingFrom_bindingExtension(bindingExtension);
8989
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(bindingBase!) == null)
90-
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingBase!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), -1, -1);
90+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingBase!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 8, 5);
9191
__root.SetBinding(global::Microsoft.Maui.Controls.Page.TitleProperty, bindingBase);
9292
static global::Microsoft.Maui.Controls.BindingBase CreateTypedBindingFrom_bindingExtension(global::Microsoft.Maui.Controls.Xaml.BindingExtension extension)
9393
{

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/DoesNotFail.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public void Test()
2323
<x:Int32>32</x:Int32>
2424
</x:Array>
2525
</ContentPage.Resources>
26+
<Label Text="{Binding Path=., Converter={StaticResource reverseConverter}}" x:DataType="x:String"/>
2627
</ContentPage>
2728
""";
2829

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/LineInfoTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using NUnit.Framework;
55

6-
namespace Microsoft.Maui.Controls.SourceGen.UnitTests.InitializeComponent;
6+
namespace Microsoft.Maui.Controls.SourceGen.UnitTests;
77

88
public class LineInfoTests : SourceGenXamlInitializeComponentTestBase
99
{

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/SetBinding.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public partial class TestPage
5959
private partial void InitializeComponent()
6060
{
6161
var bindingExtension = new global::Microsoft.Maui.Controls.Xaml.BindingExtension();
62-
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingExtension!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), -1, -1);
62+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingExtension!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 6, 5);
6363
var __root = this;
6464
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(__root!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 2, 2);
6565
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
@@ -68,12 +68,12 @@ private partial void InitializeComponent()
6868
#if !_MAUIXAML_SG_NAMESCOPE_DISABLE
6969
global::Microsoft.Maui.Controls.Internals.NameScope.SetNameScope(__root, iNameScope);
7070
#endif
71-
#line 1 "{{testXamlFilePath}}"
71+
#line 6 "{{testXamlFilePath}}"
7272
bindingExtension.Path = "Title";
7373
#line default
7474
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 };
7575
if (global::Microsoft.Maui.VisualDiagnostics.GetSourceInfo(bindingBase!) == null)
76-
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingBase!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), -1, -1);
76+
global::Microsoft.Maui.VisualDiagnostics.RegisterSourceInfo(bindingBase!, new global::System.Uri(@"Test.xaml;assembly=SourceGeneratorDriver.Generated", global::System.UriKind.Relative), 6, 5);
7777
__root.SetBinding(global::Microsoft.Maui.Controls.Page.TitleProperty, bindingBase);
7878
}
7979
}

src/Controls/tests/SourceGen.UnitTests/InitializeComponent/SimplifyOnPlatform.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Linq;
44
using NUnit.Framework;
55

6-
namespace Microsoft.Maui.Controls.SourceGen.UnitTests.InitializeComponent;
6+
namespace Microsoft.Maui.Controls.SourceGen.UnitTests;
77

88
public class SimplifyOnPlatform : SourceGenXamlInitializeComponentTestBase
99
{
@@ -149,7 +149,7 @@ private partial void InitializeComponent()
149149
xamlServiceProvider2.Add(typeof(global::Microsoft.Maui.Controls.Xaml.IXamlTypeResolver), new global::Microsoft.Maui.Controls.Xaml.Internals.XamlTypeResolver(xmlNamespaceResolver2, typeof(global::Test.TestPage).Assembly));
150150
setter1.Property = ((global::Microsoft.Maui.Controls.IExtendedTypeConverter)new global::Microsoft.Maui.Controls.BindablePropertyConverter()).ConvertFromInvariantString("IsVisible", xamlServiceProvider2) as global::Microsoft.Maui.Controls.BindableProperty;
151151
#line default
152-
#line 1 "{{testXamlFilePath}}"
152+
#line 9 "{{testXamlFilePath}}"
153153
setter1.Value = "True";
154154
#line default
155155
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")!};

0 commit comments

Comments
 (0)