Browse Source

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>
pull/22014/head
Unrealiter 2 months ago
committed by GitHub
parent
commit
bedb2ecf47
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 6
      src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs
  2. 25
      src/Markup/Avalonia.Markup.Xaml/RuntimeXamlLoaderConfiguration.cs

6
src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs

@ -157,6 +157,12 @@ namespace Avalonia.Build.Tasks
var arg1 = new CustomAttributeArgument(strType, "AvaloniaUseCompiledBindingsByDefault");
var arg2 = new CustomAttributeArgument(strType, defaultCompileBindings.ToString());
asm.CustomAttributes.Add(new CustomAttribute(ctor) { ConstructorArguments = { arg1, arg2 } });
if (createSourceInfo)
{
var arg3 = new CustomAttributeArgument(strType, "AvaloniaXamlCreateSourceInfo");
var arg4 = new CustomAttributeArgument(strType, createSourceInfo.ToString());
asm.CustomAttributes.Add(new CustomAttribute(ctor) { ConstructorArguments = { arg3, arg4 } });
}
}
TypeDefinition AddClass(string name, TypeAttributes extraAttributes = 0)

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

@ -1,10 +1,16 @@
using System;
using System.Linq;
using System.Reflection;
namespace Avalonia.Markup.Xaml;
public class RuntimeXamlLoaderConfiguration
{
/// <summary>
/// When enabled, the XAML compiler embeds SourceInfo metadata (file path, line, and column) into generated code.
/// </summary>
private bool? _createSourceInfo = null;
/// <summary>
/// Default assembly for clr-namespace:.
/// </summary>
@ -24,9 +30,24 @@ public class RuntimeXamlLoaderConfiguration
/// <summary>
/// When enabled, the XAML compiler embeds SourceInfo metadata (file path, line, and column) into generated code.
/// Default is 'false'.
/// When not explicitly set, the 'AvaloniaXamlCreateSourceInfo' from the LocalAssembly will be used to determine the value.
/// </summary>
public bool CreateSourceInfo { get; set; } = false;
public bool CreateSourceInfo
{
get {
if(_createSourceInfo == null)
{
if(LocalAssembly is {} asm)
{
var createSourceInfo = asm.GetCustomAttributes<AssemblyMetadataAttribute>()
.FirstOrDefault(a => a.Key == "AvaloniaXamlCreateSourceInfo")?.Value;
return bool.TryParse(createSourceInfo, out var parsedCreateSourceInfo) && parsedCreateSourceInfo;
}
}
return _createSourceInfo ?? false;
}
set => _createSourceInfo = value;
}
/// <summary>
/// XAML diagnostics handler.

Loading…
Cancel
Save