Skip to content

feat(rune-kit): native code-block soft wrapping and text-wrap icon - #1292

Merged
Hona merged 3 commits into
devfrom
ws3/markdown-code-wrap
Nov 21, 2026
Merged

Hona merged 3 commits into
devfrom
ws3/markdown-code-wrap

Conversation

@Hona

@Hona Hona commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Native soft wrapping for Markdown code blocks (anomalyco/opencode#64545 / UP-060) plus the TEXT_WRAP icon in rune-theme-opencode.

Summary

  • Markdown::wrap_code(bool) (default false) and per-block Markdown::code_wrap(Fn(usize, &str) -> bool) wrap long code lines natively inside Renderer::code via .by_line().wrap_anywhere().wrap_greedy() while keeping #1133 line-by-line shaping, screen-reader TextRun line runs, #1130 settled-card memoisation, Find highlights, streaming reveal, .copy_breaks(2, 2), tab stops, and shell colours.
  • Under scroll_wide(true), both wrapped and unwrapped states keep the identical frame -> col("scroll") -> col(pad) -> body element path (NodeId preserved) and keep .wrap_anywhere().wrap_greedy() on body (no-op when max_width = None inside scroll_x()), so toggling wrap never replays word-reveal, never clears active text selection, and reuses already-shaped line layouts through Rune's existing paragraphs.rs rebreak path without reshaping.
  • CodeFrame::overflows() / CodeFrame::overflows_at(scale) compute overflow lazily on demand (zero parse work, zero per-token streaming work, zero work unless called), using exact border_layout() snapping at scale, lock-free Acquire/Release rune::text::font_epoch(), TextSystem::ascii_line advances + fixed_pitch, and a bounded 256-entry FIFO cache keyed on (PreBlock::digest, len, font, avail, font_epoch).
  • rune-theme-opencode::icons::TEXT_WRAP adds upstream's text-wrap SVG (packages/ui/src/icons/catalog.ts).
  • Zero test files added or edited; zero unsafe added; rune-text/src/paragraphs.rs untouched from dev.

Response to READER-5 (notes/review-1292.md)

Round / # Finding Resolution
R1 #1 Wrap toggle under scroll_wide(true) remounted body at a new path, minting a new NodeId, replaying word-reveal from byte 0 (~55 s on 2k lines), and wiping active selection. render/code.rs keeps frame -> col("scroll") -> col(pad) -> body in both states under scroll_wide(true) and sets .wrap_anywhere().wrap_greedy() on body under scroll_wide (visual no-op at max_width = None, keeping shape_key/style_key stable so Rune's existing rebreak reuses shaped lines without reshaping). Verified in verify-1292: height 40 -> 120 -> 40, selection intact, opacity == 1.0.
R1 #2 CodeFrame::overflows false negatives/positives (non-ASCII masking, - 1.0 border mismatch, fixed_pitch/proportional, avail <= 0.0, nested task checkbox inset). Lazy CodeFrame::overflows() / overflows_at(scale) (markdown.rs, render/code.rs) matching device::border snapping at scale, using real (advance, widest, fixed_pitch) from TextSystem::ascii_line via rune::text::ascii_metrics, independent per-line checks with early exit, avail <= 0.0 returning true for non-empty code, and nested task flow.inset fix (render/blocks.rs). Verified all 8 counterexamples in verify-1292.
R1 #3 Default wrap-off path regressed (longest_line_stats per parse/token; eager code_overflows locking global FONTS Mutex). Reverted markdown/parser.rs & markdown/parser/builder.rs (Source back to 40 B, zero parse/token scan); made font_epoch() lock-free; lazy cached overflows_at keyed on precomputed 128-bit PreBlock::digest. Note: scroll_wide(true) wrap-off sets wrap_anywhere().wrap_greedy() on body (timing verified via PROOF-FARM below).
R1 #4 wrap_code in MarkdownStyle entered compute_base, invalidating non-code blocks; dual digest in Markdown::render. Reverted markdown/style.rs (wrap_code on Markdown only; compute_base untouched); memo.rs hashes wrap_code_default() only for HAS_CODE; deduplicated Markdown::render. Verified block_builds == 1 in verify-1292.
R2 #1 (9927d4f1) Stale TextSystem.ascii / paragraphs / last_paragraphs after Tx::set_font_families or web follow_full_faces. TextSystem::drop_shaped() (rune-text/src/fonts/families.rs) now clears self.ascii, self.paragraphs, and self.last_paragraphs; RuntimeCore::follow_font_families() (rune/src/runtime/font_families.rs) clears n.text_layout = None across self.tree.nodes.
R2 #2–3 (9927d4f1) paragraphs.rs tighter / kept_fits bugs (widening from narrow Some(0.0), align change on by_line, trailing whitespace hanging advance) and scope creep. Reverted crates/rune-text/src/paragraphs.rs completely back to dev. Measured settled 2,000-line toggle latency WITH vs WITHOUT paragraphs.rs (verify-1292, release, 5% / 50% / 100% long lines): on_1: 2.8 / 7.0 / 11.7 ms with vs 5.9 / 8.3 / 13.4 ms without; off: 2.0 / 1.8 / 1.4 ms with vs 1.8 / 1.5 / 1.7 ms without; on_2: 1.5 / 1.5 / 0.9 ms with vs 1.2 / 1.3 / 1.2 ms without. Fast enough without touching framework-wide paragraph layout; keeping #1292 strictly scoped.
R2 #4 (9927d4f1) GLOBAL_EPOCH Relaxed ordering & stores on read-only with_state; control chars < 0x20 skipped in ub. measure.rs now stores GLOBAL_EPOCH with Ordering::Release only when epoch actually changes and loads with Ordering::Acquire; render/code.rs bounds all non-printable bytes (< 0x20 and >= 0x7f) with non_ascii_ub.

Verification (9927d4f1)

  • cargo check -p rune-text -p rune -p rune-kit -p rune-theme-opencode --features rune-kit/markdown,rune-kit/mermaid --all-targets: green.
  • cargo check --target wasm32-unknown-unknown -p rune-text -p rune -p rune-kit --features markdown,mermaid -p rune-theme-opencode: green.
  • cargo test -p rune-text --lib -- measure:: fonts::: 18 passed, 0 failed.
  • cargo test -p rune-kit --features markdown,mermaid --lib -- markdown::: 29 passed, 0 failed.
  • External release proof harness (verify-1292): all 8 overflows_at counterexamples, stable NodeId / selection / opacity == 1.0, and block_builds == 1 passed.

Shipping-build timings (PROOF-FARM)

  • Pending PROOF-FARM shipping-build run on rune-desktop measurement branch ws3/measure-wrap (pinned to Rune 0a33155a = dev 9553c9a7 + #1292 9927d4f1, vs dev built the same way).

Hona added 3 commits November 20, 2026 17:53
Add native code-block soft wrapping to `rune-kit::markdown` and the
`text-wrap` icon (`icons::TEXT_WRAP`) to `rune-theme-opencode` for
`anomalyco/opencode#64545`:

- `MarkdownStyle::wrap_code(bool)`, `Markdown::wrap_code(bool)`, and
  `Markdown::code_wrap(Fn(usize, &str) -> bool)` wrap long lines inside
  `Renderer::code` via `.by_line().wrap_anywhere().wrap_greedy()` while
  keeping `#1133` per-line shaping, screen-reader `TextRun` line runs,
  `#1130` `settled_code_card_digest` memoisation, Find highlights,
  streaming reveal, `.copy_breaks(2, 2)`, non-mono tab stops, and shell
  block colours.
- Unwrapped blocks (`!wrapped`) remain identical to before.
- `Source` computes longest-line tab-expanded stats once at parse time;
  `CodeFrame::overflows` answers overflow against `text_width - flow.inset`
  in O(1) on the ASCII monospace fast path (falling back to
  `rune::text::measure` only near the boundary or for non-ASCII/non-mono).
- `CodeFrame::wrapped` reports the block's active wrap state.
- `rune-theme-opencode::icons::TEXT_WRAP` adds Desktop's `text-wrap` SVG.
…exact overflows, lock-free font_epoch, line reuse, scoped memo)
…hs on font switch, Acquire/Release font_epoch, bound control chars
@Hona
Hona merged commit af4d9cd into dev Nov 21, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant