Skip to content

Commit

Permalink
Add With method generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
GGG-KILLER committed Jan 29, 2024
1 parent 4711a09 commit 7bf06d4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ internal FunctionCallExpressionSample(global::Tsu.Trees.RedGreen.Sample.Internal

return this;
}

public global::Tsu.Trees.RedGreen.Sample.FunctionCallExpressionSample WithIdentifier(global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample identifier) => this.Update(identifier, this.FirstArg, this.SecondArg);

public global::Tsu.Trees.RedGreen.Sample.FunctionCallExpressionSample WithFirstArg(global::Tsu.Trees.RedGreen.Sample.ExpressionSample firstArg) => this.Update(this.Identifier, firstArg, this.SecondArg);

public global::Tsu.Trees.RedGreen.Sample.FunctionCallExpressionSample WithSecondArg(global::Tsu.Trees.RedGreen.Sample.ExpressionSample? secondArg) => this.Update(this.Identifier, this.FirstArg, secondArg);
}

public sealed partial class BinaryOperationExpressionSample : global::Tsu.Trees.RedGreen.Sample.ExpressionSample
Expand Down Expand Up @@ -220,6 +226,12 @@ internal BinaryOperationExpressionSample(global::Tsu.Trees.RedGreen.Sample.Inter

return this;
}

public global::Tsu.Trees.RedGreen.Sample.BinaryOperationExpressionSample WithKind(global::Tsu.Trees.RedGreen.Sample.SampleKind kind) => this.Update(kind, this.Left, this.Right);

public global::Tsu.Trees.RedGreen.Sample.BinaryOperationExpressionSample WithLeft(global::Tsu.Trees.RedGreen.Sample.ExpressionSample left) => this.Update(this.Kind, left, this.Right);

public global::Tsu.Trees.RedGreen.Sample.BinaryOperationExpressionSample WithRight(global::Tsu.Trees.RedGreen.Sample.ExpressionSample right) => this.Update(this.Kind, this.Left, right);
}

public sealed partial class NumericalLiteralExpressionSample : global::Tsu.Trees.RedGreen.Sample.ExpressionSample
Expand Down Expand Up @@ -247,6 +259,8 @@ internal NumericalLiteralExpressionSample(global::Tsu.Trees.RedGreen.Sample.Inte

return this;
}

public global::Tsu.Trees.RedGreen.Sample.NumericalLiteralExpressionSample WithValue(global::System.Double value) => this.Update(value);
}

public sealed partial class IdentifierExpressionSample : global::Tsu.Trees.RedGreen.Sample.ExpressionSample
Expand Down Expand Up @@ -274,6 +288,8 @@ internal IdentifierExpressionSample(global::Tsu.Trees.RedGreen.Sample.Internal.G

return this;
}

public global::Tsu.Trees.RedGreen.Sample.IdentifierExpressionSample WithName(global::System.String name) => this.Update(name);
}
}

34 changes: 34 additions & 0 deletions Tsu.Trees.RedGreen/sourcegen/RedTreeGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.CodeDom.Compiler;
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
using Tsu.Trees.RedGreen.SourceGenerator.Model;
Expand Down Expand Up @@ -458,6 +459,39 @@ private static void WriteRedNode(this IndentedTextWriter writer, Tree tree, Node
writer.WriteLine('}');
}
#endregion TNode Update(...)

#region TNode WithX(...)
if (node.Descendants.Length == 0 && node.RequiredComponents.Any())
{
var components = node.RequiredComponents.ToImmutableArray();
for (var targetIdx = 0; targetIdx < components.Length; targetIdx++)
{
var target = components[targetIdx];
var isNode = target.Type.DerivesFrom(tree.GreenBase);

writer.WriteLineNoTabs("");
writer.Write("public {0}.{1} With{2}({3}.{4}{5} {6}) => this.Update(",
tree.RedBase.ContainingNamespace.ToCSharpString(false),
node.TypeSymbol.Name,
target.PropertyName,
(isNode ? tree.RedBase.ContainingNamespace : target.Type.ContainingNamespace).ToCSharpString(false),
target.Type.Name,
target.IsOptional ? "?" : "",
target.ParameterName);
var first = true;
for (var idx = 0; idx < components.Length; idx++)
{
var component = components[idx];
if (!first) writer.Write(", ");
first = false;

if (targetIdx == idx) writer.Write(component.ParameterName);
else writer.Write("this.{0}", component.PropertyName);
}
writer.WriteLine(");");
}
}
#endregion TNode WithX(...)
}
writer.Indent--;
writer.WriteLine('}');
Expand Down

0 comments on commit 7bf06d4

Please sign in to comment.