Browse Source
* Do not defer resources with name registration on them * Fix transformers order * Make NameScopeRegistrationVisitor usage more clear * Reuse NameScopeRegistrationVisitor * Make NameScopeRegistrationVisitor usage more intuitivepull/14793/head
committed by
GitHub
5 changed files with 105 additions and 47 deletions
@ -0,0 +1,51 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; |
||||
|
using XamlX.Ast; |
||||
|
using XamlX.TypeSystem; |
||||
|
|
||||
|
namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Visitors; |
||||
|
|
||||
|
internal class NameScopeRegistrationVisitor : Dictionary<string, (IXamlType type, IXamlLineInfo line)>, IXamlAstVisitor |
||||
|
{ |
||||
|
private readonly int _targetMetadataScopeLevel; |
||||
|
private readonly Stack<IXamlAstNode> _parents = new(); |
||||
|
private int _metadataScopeLevel; |
||||
|
|
||||
|
public NameScopeRegistrationVisitor( |
||||
|
int initialMetadataScopeLevel = 0, |
||||
|
int targetMetadataScopeLevel = 1) |
||||
|
{ |
||||
|
_metadataScopeLevel = initialMetadataScopeLevel; |
||||
|
_targetMetadataScopeLevel = targetMetadataScopeLevel; |
||||
|
} |
||||
|
|
||||
|
IXamlAstNode IXamlAstVisitor.Visit(IXamlAstNode node) |
||||
|
{ |
||||
|
if (_metadataScopeLevel == _targetMetadataScopeLevel |
||||
|
&& node is AvaloniaNameScopeRegistrationXamlIlNode nameScopeRegistration |
||||
|
&& nameScopeRegistration.Name is XamlAstTextNode textNode) |
||||
|
{ |
||||
|
this[textNode.Text] = (nameScopeRegistration.TargetType, textNode); |
||||
|
} |
||||
|
|
||||
|
return node; |
||||
|
} |
||||
|
|
||||
|
void IXamlAstVisitor.Push(IXamlAstNode node) |
||||
|
{ |
||||
|
_parents.Push(node); |
||||
|
if (node is NestedScopeMetadataNode) |
||||
|
{ |
||||
|
_metadataScopeLevel++; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void IXamlAstVisitor.Pop() |
||||
|
{ |
||||
|
var oldParent = _parents.Pop(); |
||||
|
if (oldParent is NestedScopeMetadataNode) |
||||
|
{ |
||||
|
_metadataScopeLevel--; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue