-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Do not defer resources with name registration on them (#14703)
* Do not defer resources with name registration on them * Fix transformers order * Make NameScopeRegistrationVisitor usage more clear * Reuse NameScopeRegistrationVisitor * Make NameScopeRegistrationVisitor usage more intuitive
- Loading branch information
Showing
5 changed files
with
105 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...p/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Visitors/NameScopeRegistrationVisitor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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--; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters