Клинап

This commit is contained in:
2025-03-29 19:31:10 +03:00
parent 361b4cd3f6
commit 5cc53d72fd
18 changed files with 359 additions and 390 deletions
+54 -54
View File
@@ -1,70 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace mROA.CodegenTools
namespace mROA.Codegen.Tools
{
public class InsertTemplateSection : ITagged, IBaking
{
private object _insertedValue;
private string _tag;
public string Tag => _tag;
public TemplateDocument Context { get; set; }
public readonly List<string> Parameters;
private object _insertedValue;
public InsertTemplateSection(string tag, TemplateDocument context, params string[] parameters)
{
_tag = tag;
Tag = tag;
Parameters = parameters.ToList();
Context = context;
}
private bool IsInserted(object value)
{
if (Parameters.IndexOf("r") == -1) return false;
ProduceInserted(value);
return true;
}
private void ProduceInserted(object value)
{
var currentIndex = Context.Parts.IndexOf(this);
var clone = (InsertTemplateSection)MemberwiseClone();
clone._tag += "+";
clone._insertedValue = value;
Context.Parts.Insert(currentIndex, clone);
InsertSeparator(currentIndex + 1);
}
private void InsertSeparator(int currentIndex)
{
var sepIndex = Parameters.IndexOf("sep");
if (sepIndex == -1)
return;
Context.Parts.Insert(currentIndex, new LiteralTemplateSection(Context[Parameters[sepIndex + 1]].ToString(), Context));
}
public void Setup(object value)
{
if (!(value is string) && !(value is ITemplateSection) )
return;
if (IsInserted(value))
return;
_insertedValue = value;
}
public int TargetLength => 0;
public string Bake()
{
switch (_insertedValue)
@@ -78,9 +28,59 @@ namespace mROA.CodegenTools
}
}
public string Tag { get; private set; }
public TemplateDocument Context { get; set; }
public int TargetLength => 0;
public object Clone()
{
return new InsertTemplateSection(_tag, Context, Parameters.ToArray());
return new InsertTemplateSection(Tag, Context, Parameters.ToArray());
}
private bool IsInserted(object value)
{
if (Parameters.IndexOf("r") == -1) return false;
ProduceInserted(value);
return true;
}
private void ProduceInserted(object value)
{
var currentIndex = Context.Parts.IndexOf(this);
var clone = (InsertTemplateSection)MemberwiseClone();
clone.Tag += "+";
clone._insertedValue = value;
Context.Parts.Insert(currentIndex, clone);
InsertSeparator(currentIndex + 1);
}
private void InsertSeparator(int currentIndex)
{
var sepIndex = Parameters.IndexOf("sep");
if (sepIndex == -1)
return;
Context.Parts.Insert(currentIndex,
new LiteralTemplateSection(Context[Parameters[sepIndex + 1]].ToString(), Context));
}
public void Setup(object value)
{
if (!(value is string) && !(value is ITemplateSection))
return;
if (IsInserted(value))
return;
_insertedValue = value;
}
}
}