Skip to content

Commit

Permalink
some useful texttree helpers from baboon
Browse files Browse the repository at this point in the history
  • Loading branch information
pshirshov committed Dec 30, 2024
1 parent 2f91adb commit 798167a
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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("")
}
}
}

}
}
}
}

0 comments on commit 798167a

Please sign in to comment.