committed by
GitHub
7 changed files with 122 additions and 68 deletions
@ -1,54 +0,0 @@ |
|||||
using System.Collections.Generic; |
|
||||
using XamlX.Ast; |
|
||||
|
|
||||
namespace XamlNameReferenceGenerator.Infrastructure |
|
||||
{ |
|
||||
internal sealed class MiniNamedControlCollector : IXamlAstVisitor |
|
||||
{ |
|
||||
private readonly List<(string TypeName, string Name)> _items = new List<(string TypeName, string Name)>(); |
|
||||
|
|
||||
public IReadOnlyList<(string TypeName, string Name)> Controls => _items; |
|
||||
|
|
||||
public IXamlAstNode Visit(IXamlAstNode node) |
|
||||
{ |
|
||||
if (!(node is XamlAstConstructableObjectNode constructableObjectNode)) |
|
||||
return node; |
|
||||
|
|
||||
foreach (var child in constructableObjectNode.Children) |
|
||||
{ |
|
||||
var nameValue = ResolveNameDirectiveOrDefault(child); |
|
||||
if (nameValue == null) continue; |
|
||||
|
|
||||
var clrType = constructableObjectNode.Type.GetClrType(); |
|
||||
var typeNamePair = ($@"{clrType.Namespace}.{clrType.Name}", nameValue); |
|
||||
if (!_items.Contains(typeNamePair)) |
|
||||
{ |
|
||||
_items.Add(typeNamePair); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
return node; |
|
||||
} |
|
||||
|
|
||||
public void Push(IXamlAstNode node) { } |
|
||||
|
|
||||
public void Pop() { } |
|
||||
|
|
||||
private static string ResolveNameDirectiveOrDefault(IXamlAstNode node) => |
|
||||
node switch |
|
||||
{ |
|
||||
XamlAstXamlPropertyValueNode propertyValueNode when |
|
||||
propertyValueNode.Property is XamlAstClrProperty reference && |
|
||||
reference.Name == "Name" && |
|
||||
propertyValueNode.Values.Count > 0 && |
|
||||
propertyValueNode.Values[0] is XamlAstTextNode nameNode => nameNode.Text, |
|
||||
|
|
||||
XamlAstXmlDirective xmlDirective when |
|
||||
xmlDirective.Name == "Name" && |
|
||||
xmlDirective.Values.Count > 0 && |
|
||||
xmlDirective.Values[0] is XamlAstTextNode xNameNode => xNameNode.Text, |
|
||||
|
|
||||
_ => null |
|
||||
}; |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,29 @@ |
|||||
|
using XamlX; |
||||
|
using XamlX.Ast; |
||||
|
using XamlX.Transform; |
||||
|
|
||||
|
namespace XamlNameReferenceGenerator.Infrastructure |
||||
|
{ |
||||
|
public class NameDirectiveTransformer : IXamlAstTransformer |
||||
|
{ |
||||
|
public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) |
||||
|
{ |
||||
|
if (node is XamlAstObjectNode objectNode) |
||||
|
{ |
||||
|
for (var index = 0; index < objectNode.Children.Count; index++) |
||||
|
{ |
||||
|
var child = objectNode.Children[index]; |
||||
|
if (child is XamlAstXmlDirective directive && |
||||
|
directive.Namespace == XamlNamespaces.Xaml2006 && |
||||
|
directive.Name == "Name") |
||||
|
objectNode.Children[index] = new XamlAstXamlPropertyValueNode( |
||||
|
directive, |
||||
|
new XamlAstNamePropertyReference(directive, objectNode.Type, "Name", objectNode.Type), |
||||
|
directive.Values); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return node; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using XamlX.Ast; |
||||
|
|
||||
|
namespace XamlNameReferenceGenerator.Infrastructure |
||||
|
{ |
||||
|
internal sealed class NamedControlCollector : IXamlAstVisitor |
||||
|
{ |
||||
|
private readonly List<(string TypeName, string Name)> _items = new List<(string TypeName, string Name)>(); |
||||
|
|
||||
|
public IReadOnlyList<(string TypeName, string Name)> Controls => _items; |
||||
|
|
||||
|
public IXamlAstNode Visit(IXamlAstNode node) |
||||
|
{ |
||||
|
if (node is XamlAstConstructableObjectNode constructableObjectNode) |
||||
|
{ |
||||
|
foreach (var child in constructableObjectNode.Children) |
||||
|
{ |
||||
|
if (child is XamlAstXamlPropertyValueNode propertyValueNode && |
||||
|
propertyValueNode.Property is XamlAstClrProperty clrProperty && |
||||
|
clrProperty.Name == "Name" && |
||||
|
propertyValueNode.Values.Count > 0 && |
||||
|
propertyValueNode.Values[0] is XamlAstTextNode text) |
||||
|
{ |
||||
|
var clrType = constructableObjectNode.Type.GetClrType(); |
||||
|
var typeNamePair = ($@"{clrType.Namespace}.{clrType.Name}", text.Text); |
||||
|
|
||||
|
if (!_items.Contains(typeNamePair)) |
||||
|
{ |
||||
|
_items.Add(typeNamePair); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return node; |
||||
|
} |
||||
|
|
||||
|
return node; |
||||
|
} |
||||
|
|
||||
|
public void Push(IXamlAstNode node) { } |
||||
|
|
||||
|
public void Pop() { } |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue