Repository: typst/typst
File: crates/typst-library/src/model/par.rs (Rust)

Code before:
    /// #let sample-text = [A]
    ///
    /// // The wide margin is for annotations
    /// #set page(width: 350pt, margin: (x: 20%))
    ///
    /// #context {
    ///   let text-height = measure(sample-text).height
    ///   let line-height = text-height + par.leading.to-absolute()
    ///
    ///   // Number of lines in each paragraph
    ///   let n-lines = {
    ///     // Calculate `n-lines` by querying `par.line`.
    ///     let positions = query(<par-line>).map(it => it.location().position().y)
    ///
    ///     // Determine it's leading or spacing
    ///     let kinds = positions
    ///       .enumerate()
    ///       .slice(1)
    ///       .map(((i, y)) => {
    ///         // Displacement from the last line relative to the line height
    ///         let jump = (positions.at(i) - positions.at(i - 1)) / line-height
    ///         if calc.abs(jump - 1) < 0.1 { "leading" } else { "spacing" }
    ///       })
    ///
    ///     kinds.split("spacing").map(x => x.len() + 1)
    ///   }
    ///
    ///   let jumps = n-lines
    ///     .map(n => ((text-height,) * n).intersperse(par.leading))
    ///     .intersperse(par.spacing)
    ///     .flatten()
    ///
    ///   place(grid(
    ///     ..jumps
    ///       .enumerate()
    ///       .map(((i, h)) => if calc.even(i) {
    ///         // Draw a stripe for the line
    ///         block(
    ///           height: h,
    ///           width: 100%,
    ///           fill: c.par-line,
    ///         )
    ///       } else {
    ///         // Put an annotation for the gap
    ///
    ///         let sw(value-for-leading, value-for-spacing) = if h == par.leading {
    ///           value-for-leading
    ///         } else {
    ///           value-for-spacing
    ///         }
    ///

Review comment (suggestion):
> But it's the 79 line context that might be shrinkable. 

Yes, I'm also worried about this.

For example, the whole `let n-lines = { … }` can be replaced with `let n-lines = (4, 4, 2)`.
Not sure which way is better…

Code after:
    /// #let sample-text = [A]
    ///
    /// // Number of lines in each paragraph
    /// #let n-lines = (4, 4, 2)
    /// #let annotated-lines = (4, 8)
    ///
    /// // The wide margin is for annotations
    /// #set page(width: 350pt, margin: (x: 20%))
    ///
    /// #context {
    ///   let text-height = measure(sample-text).height
    ///   let line-height = text-height + par.leading.to-absolute()
    ///
    ///   let jumps = n-lines
    ///     .map(n => ((text-height,) * n).intersperse(par.leading))
    ///     .intersperse(par.spacing)
    ///     .flatten()
    ///
    ///   place(grid(
    ///     ..jumps
    ///       .enumerate()
    ///       .map(((i, h)) => if calc.even(i) {
    ///         // Draw a stripe for the line
    ///         block(height: h, width: 100%, fill: c.par-line)
    ///       } else {
    ///         // Put an annotation for the gap
    ///         let sw(a, b) = if h == par.leading { a } else { b }
    ///
    ///         align(end, block(
    ///           height: h,
    ///           outset: (right: sw(0.5em, 1em)),
    ///           stroke: (
    ///             left: none,
    ///             rest: 0.5pt + sw(c.leading-line, c.spacing-line),
    ///           ),
    ///           if i / 2 <= sw(..annotated-lines) {
    ///             place(horizon, dx: 1.3em, text(
    ///               0.8em,
    ///               sw(c.leading-text, c.spacing-text),
    ///               sw([leading], [spacing]),
    ///             ))
    ///           },
    ///         ))
    ///       })
    ///   ))
    ///
    ///   // Mark top and bottom edges
    ///   place(
    ///     // pos: top/bottom edge
    ///     // dy: Δy to the last mark
    ///     // kind: leading/spacing