Skip to content

Commit bedb2ec

Browse files
UnrealiterKimHenrikOtteMrJul
authored
add support for CreateSourceInfo in design window loader (#21972)
* feat: add support for CreateSourceInfo in design window loader * Move XAML SourceInfo default to RuntimeXamlLoaderConfiguration Applies to all designer/runtime load paths now; newer VS and VS Code designers do not use DesignWindowLoader. --------- Co-authored-by: Kim Henrik <KimHenrik@outlook.de> Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
1 parent a56f8c1 commit bedb2ec

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ internal static CompileResult Compile(IBuildEngine engine,
157157
var arg1 = new CustomAttributeArgument(strType, "AvaloniaUseCompiledBindingsByDefault");
158158
var arg2 = new CustomAttributeArgument(strType, defaultCompileBindings.ToString());
159159
asm.CustomAttributes.Add(new CustomAttribute(ctor) { ConstructorArguments = { arg1, arg2 } });
160+
if (createSourceInfo)
161+
{
162+
var arg3 = new CustomAttributeArgument(strType, "AvaloniaXamlCreateSourceInfo");
163+
var arg4 = new CustomAttributeArgument(strType, createSourceInfo.ToString());
164+
asm.CustomAttributes.Add(new CustomAttribute(ctor) { ConstructorArguments = { arg3, arg4 } });
165+
}
160166
}
161167

162168
TypeDefinition AddClass(string name, TypeAttributes extraAttributes = 0)

src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderConfiguration.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
using System;
2+
using System.Linq;
23
using System.Reflection;
34

45
namespace Avalonia.Markup.Xaml;
56

67
public class RuntimeXamlLoaderConfiguration
78
{
9+
/// <summary>
10+
/// When enabled, the XAML compiler embeds SourceInfo metadata (file path, line, and column) into generated code.
11+
/// </summary>
12+
private bool? _createSourceInfo = null;
13+
814
/// <summary>
915
/// Default assembly for clr-namespace:.
1016
/// </summary>
@@ -24,9 +30,24 @@ public class RuntimeXamlLoaderConfiguration
2430

2531
/// <summary>
2632
/// When enabled, the XAML compiler embeds SourceInfo metadata (file path, line, and column) into generated code.
27-
/// Default is 'false'.
33+
/// When not explicitly set, the 'AvaloniaXamlCreateSourceInfo' from the LocalAssembly will be used to determine the value.
2834
/// </summary>
29-
public bool CreateSourceInfo { get; set; } = false;
35+
public bool CreateSourceInfo
36+
{
37+
get {
38+
if(_createSourceInfo == null)
39+
{
40+
if(LocalAssembly is {} asm)
41+
{
42+
var createSourceInfo = asm.GetCustomAttributes<AssemblyMetadataAttribute>()
43+
.FirstOrDefault(a => a.Key == "AvaloniaXamlCreateSourceInfo")?.Value;
44+
return bool.TryParse(createSourceInfo, out var parsedCreateSourceInfo) && parsedCreateSourceInfo;
45+
}
46+
}
47+
return _createSourceInfo ?? false;
48+
}
49+
set => _createSourceInfo = value;
50+
}
3051

3152
/// <summary>
3253
/// XAML diagnostics handler.

0 commit comments

Comments
 (0)