Instead of using a buffer, I would create 3 immutable Array/Seq (regions before, template regions, regions after), and 2 options for the optional splits. Then concatenate everything in a Seq.
And use `span(tr => Boolean)` to generate the before and after region Seq.