From 798167aa499617f1d571f4e94857e04b1df87866 Mon Sep 17 00:00:00 2001 From: Pavel Shirshov Date: Mon, 30 Dec 2024 18:47:55 +0000 Subject: [PATCH] some useful texttree helpers from baboon --- .../platform/strings/TextTree.scala | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/fundamentals/fundamentals-platform/src/main/scala/izumi/fundamentals/platform/strings/TextTree.scala b/fundamentals/fundamentals-platform/src/main/scala/izumi/fundamentals/platform/strings/TextTree.scala index 850567c11e..b6b2679a1e 100644 --- a/fundamentals/fundamentals-platform/src/main/scala/izumi/fundamentals/platform/strings/TextTree.scala +++ b/fundamentals/fundamentals-platform/src/main/scala/izumi/fundamentals/platform/strings/TextTree.scala @@ -48,9 +48,25 @@ object TextTree { } q"$begin$middle$end" } + + def joinN(): TextTree[T] = { + target.join("\n") + } + + def joinNN(): TextTree[T] = { + target.join("\n\n") + } } implicit final class TextTreeGenericOps[T](private val target: TextTree[T]) { + def last: Option[Char] = target match { + case ValueNode(_) => None + case StringNode(value) => value.lastOption + case Node(chunks) => chunks.toList.filter(_.nonEmpty).lastOption.flatMap(_.last) + case Shift(nested, _) => nested.last + case Trim(nested) => nested.last + } + def as[W](implicit conv: T => W): TextTree[W] = { target.map(conv) } @@ -196,4 +212,44 @@ object TextTree { override def asNode: TextTree[T] = node } } + + object style { + object c { + implicit class TextTreeCStyleOps[T](target: TextTree[T]) { + def endC(): TextTree[T] = { + target.last match { + case Some('}') => target + case None => target + case _ => q"$target;" + } + } + } + + implicit class TextTreeSeqCStyleOps[T](target: Seq[TextTree[T]]) { + def joinCN(): TextTree[T] = { + if (target.isEmpty) { + StringNode("") + } else { + val withSeparators = target.flatMap { + t => + if (t.last.contains('}')) { + Seq(t, StringNode("\n")) + } else { + Seq(t, StringNode(";\n")) + } + + }.init + + NEList.from(withSeparators) match { + case Some(value) => + Node(value) + case None => + StringNode("") + } + } + } + + } + } + } }