From e5ed37a4834b8bd2abfbdac159edab81629e26fc Mon Sep 17 00:00:00 2001 From: Lucas Satabin Date: Tue, 30 Jan 2024 23:44:33 +0100 Subject: [PATCH] Add basic util to split big texts --- .../scala/fs2/data/text/render/Renderer.scala | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/text/shared/src/main/scala/fs2/data/text/render/Renderer.scala b/text/shared/src/main/scala/fs2/data/text/render/Renderer.scala index 3e087ee4e..e3e2d5deb 100644 --- a/text/shared/src/main/scala/fs2/data/text/render/Renderer.scala +++ b/text/shared/src/main/scala/fs2/data/text/render/Renderer.scala @@ -16,10 +16,30 @@ package fs2.data.text.render -import fs2.{Stream, Pure} +import fs2.{Chunk, Pure, Stream} trait Renderer[Event] { + /** + * Splits words in the given text into a stream of document events. + * This is a utility method, when you want to reformat a text using + * the pretty printer. The pretty printing algorithm assumes that each + * `DocEvent.Text` is an atomic value, so if it contains new lines it + * can break the computations. + * + * Between 2 words, it adds a `DocEvent.Line` event. Empty lines are not + * Generated. + * + * @param text The text to split + * @param wordBoundary The regular expression on which to split words + */ + def words(text: String, wordBoundary: String = raw"\s+"): Stream[Pure, DocEvent] = + Stream + .chunk(Chunk.array(text.split(wordBoundary))) + .filter(_.nonEmpty) + .map(DocEvent.Text(_)) + .intersperse(DocEvent.Line) + /** Transforms the event into a stream of document events. * The stream may be partial (e.g. opening a group when the event describes a new tree node). */