typelevel / Laika

Site and E-book Generator and Customizable Text Markup Transformer for sbt, Scala and Scala.js
https://typelevel.org/Laika/
Apache License 2.0
410 stars 44 forks source link

reduce use of case classes in Document APIs #487

Closed jenshalm closed 1 year ago

jenshalm commented 1 year ago

This is the only PR for M3 where the removal of case classes is combined with a fairly big refactoring as it allows to remove some old limitations now that we do no longer have to support low-level copy operations on DocumentTree.

The Document instances added to such a tree have three properties which represent a hierarchical structure within the document tree:

In the past these values could go out of sync due to the existence of low-level copy methods on both, DocumentTree and Document. Now that both are regular classes the correct parent association can be managed internally, creating a much more robust representation of the virtual tree on top of being easier to evolve in a binary compatible manner. A Document instance can now safely be taken from one DocumentTree and inserted into another where internally a copy with adjusted parent context will be added instead.

Types within the laika.ast package which cease to be case classes:

TreeCursor DocumentCursor Document DocumentTree DocumentTreeRoot TemplateDocument

The removal of a copy method is balanced by the introduction of mutator methods, in the most simple case just the withFoo pattern, but in some cases with additional methods for convenience, e.g. modifyConfig(ConfigBuilder => ConfigBuilder) which helps with the common case of just adding to existing properties.