Skip to content

Commit

Permalink
Add basic util to split big texts
Browse files Browse the repository at this point in the history
  • Loading branch information
satabin committed Jan 30, 2024
1 parent 99c513d commit e5ed37a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion text/shared/src/main/scala/fs2/data/text/render/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down

0 comments on commit e5ed37a

Please sign in to comment.