Skip to content

Commit

Permalink
Add support for the new order customization.
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Feb 2, 2024
1 parent e8db742 commit 4ae5c09
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
8 changes: 5 additions & 3 deletions Tsu.Trees.RedGreen/src/DebugGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ public static void RegisterDebugOutput(this IncrementalGeneratorInitializationCo

builder.AppendLine($"// {indent} Children:");
foreach (var child in node.Item2.Children)
builder.AppendLine($"// {indent} {child.Type.ToCSharpString()} (IsList = {child.IsList}, Name = {child.FieldName}, IsOptional = {child.IsOptional}, PassToBase = {child.PassToBase})");
builder.AppendLine($"// {indent} {child.Type.ToCSharpString()} (IsList = {child.IsList}, Name = {child.FieldName}, IsOptional = {child.IsOptional}, PassToBase = {child.PassToBase}, Order = {child.Order})");

builder.AppendLine($"// {indent} ExtraData:");
foreach (var child in node.Item2.ExtraData)
builder.AppendLine($"// {indent} {child.Type.ToCSharpString()} (IsList = {child.IsList}, Name = {child.FieldName}, IsOptional = {child.IsOptional}, PassToBase = {child.PassToBase})");
builder.AppendLine($"// {indent} {child.Type.ToCSharpString()} (IsList = {child.IsList}, Name = {child.FieldName}, IsOptional = {child.IsOptional}, PassToBase = {child.PassToBase}, Order = {child.Order})");

builder.AppendLine($"// {indent} Descendants:");

foreach (var derived in node.Item2.Descendants)
queue.Push((node.Item1 + 1, derived));
queue.Push((node.Item1 + 2, derived));
}

ctx.AddSource($"{tree.Suffix}/Debug.g.cs", builder.ToSourceText());
Expand Down
4 changes: 3 additions & 1 deletion Tsu.Trees.RedGreen/src/Model/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public sealed record Component(
ITypeSymbol Type,
string FieldName,
bool IsOptional,
bool PassToBase
bool PassToBase,
int Order
)
{
public int SortOrder => Order < 0 ? int.MaxValue + Order : Order;
public string ParameterName { get; } = FieldName.TrimStart('_').ToCamelCase();
public string PropertyName { get; } = FieldName.TrimStart('_').ToPascalCase();
}
2 changes: 1 addition & 1 deletion Tsu.Trees.RedGreen/src/Model/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed record Node(
ImmutableArray<Component> ExtraData
)
{
public IEnumerable<Component> Components => ExtraData.Concat(Children);
public IEnumerable<Component> Components => ExtraData.Concat(Children).OrderBy(x => x.SortOrder);

public IEnumerable<Component> ParentComponents => Components.Where(x => x.PassToBase);

Expand Down
2 changes: 2 additions & 0 deletions Tsu.Trees.RedGreen/src/Model/Script/ScriptComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ internal sealed class ScriptComponent(Component component)
public string PropertyName => component.PropertyName;
public bool IsOptional => component.IsOptional;
public bool PassToBase => component.PassToBase;
public int Order => component.Order;
public int SortOrder => component.SortOrder;
}
2 changes: 1 addition & 1 deletion Tsu.Trees.RedGreen/src/Model/Script/ScriptNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal sealed class ScriptNode(Node node)
public ImmutableArray<ScriptComponent> Children { get; } = node.Children.Select(x => new ScriptComponent(x)).ToImmutableArray();
public ImmutableArray<ScriptComponent> ExtraData { get; } = node.ExtraData.Select(x => new ScriptComponent(x)).ToImmutableArray();

public IEnumerable<ScriptComponent> Components => ExtraData.Concat(Children);
public IEnumerable<ScriptComponent> Components => ExtraData.Concat(Children).OrderBy(x => x.SortOrder);

public IEnumerable<ScriptComponent> ParentComponents => Components.Where(x => x.PassToBase);

Expand Down
48 changes: 41 additions & 7 deletions Tsu.Trees.RedGreen/src/TreeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,45 @@ private static Node ProcessNode(
IEnumerable<Component> parentExtraData)
{
var fields = node.NodeType.GetMembers().OfType<IFieldSymbol>().Where(f => f.IsReadOnly);
var nodeChildren = fields.Where(x => x.Type.DerivesFrom(tree.GreenBase)).Select(toComponent);
var nodeExtraData = fields.Where(x => !x.Type.DerivesFrom(tree.GreenBase)).Select(toComponent);

// Starting order for this node
var order = 1;
if (parentNodes.Any())
order = Math.Max(order, parentNodes.Select(x => x.Order).Max() + 1);
if (parentExtraData.Any())
order = Math.Max(order, parentExtraData.Select(x => x.Order).Max() + 1);

var nodeChildren = fields.Where(x => x.Type.DerivesFrom(tree.GreenBase))
.Select(x =>
{
var ret = toComponent(x, order);
if (ret.Order == order)
order++;
return ret;
})
.ToArray();
var nodeExtraData = fields.Where(x => !x.Type.DerivesFrom(tree.GreenBase))
.Select(x =>
{
var ret = toComponent(x, order);
if (ret.Order == order)
order++;
return ret;
})
.ToArray();

var children = parentNodes.Select(x => x with { PassToBase = true })
.Concat(nodeChildren)
.OrderBy(x => x.SortOrder)
.ToImmutableArray();

var extraData = parentExtraData.Select(x => x with { PassToBase = true })
.Concat(nodeExtraData)
.OrderBy(x => x.SortOrder)
.ToImmutableArray();

if (SymbolEqualityComparer.Default.Equals(node.NodeType, tree.GreenBase))
extraData = extraData.Add(new Component(false, tree.KindEnum, "_kind", false, false));
extraData = extraData.Add(new Component(false, tree.KindEnum, "_kind", false, false, 0));

return new Node(
node.BaseType,
Expand All @@ -142,23 +168,31 @@ private static Node ProcessNode(
children,
extraData);

static Component toComponent(IFieldSymbol fieldSymbol)
static Component toComponent(IFieldSymbol fieldSymbol, int order)
{
var isList = false;
var type = fieldSymbol.Type;

if (fieldSymbol.GetAttributes().SingleOrDefault(x => x.AttributeClass?.ToCSharpString(false) == "global::Tsu.Trees.RedGreen.GreenListAttribute") is AttributeData attr)
if (fieldSymbol.GetAttributes().SingleOrDefault(x => x.AttributeClass?.ToCSharpString(false) == "global::Tsu.Trees.RedGreen.GreenListAttribute") is AttributeData listAttr)
{
isList = true;
type = (INamedTypeSymbol) attr.ConstructorArguments.Single().Value!;
type = (INamedTypeSymbol) listAttr.ConstructorArguments.Single().Value!;
}

if (fieldSymbol.GetAttributes().SingleOrDefault(x => x.AttributeClass?.ToCSharpString(false) == "global::Tsu.Trees.RedGreen.NodeComponentAttribute") is AttributeData custAttr)
{
var val = custAttr.NamedArguments.SingleOrDefault(x => x.Key == "Order").Value;
if (val.Value is int num)
order = num;
}

return new(
isList,
type,
fieldSymbol.Name,
fieldSymbol.Type.NullableAnnotation == NullableAnnotation.Annotated,
false);
false,
order);
}
}

Expand Down

0 comments on commit 4ae5c09

Please sign in to comment.