Something went wrong. Try again.
Local-first knowledge base made for Typst and the Atmosphere. typbase.at
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444//! Tooltip extraction: tooltips are the per-equation frame groups the editor//! positions under the cursor.
use serde::Serialize;
use crate::{ renderer::paged::{FrameItemsChunk, svg::render_svgs_by_items_with_options}, source::{RenderTarget, sync_source_state}, tests::{fixtures, harness},};
#[derive(Debug, Serialize)]struct TooltipSummary { range: [usize; 2], items: usize,}
fn summarize(chunks: &[FrameItemsChunk]) -> Vec<TooltipSummary> { chunks .iter() .map(|chunk| { TooltipSummary { range: [chunk.range.start, chunk.range.end], items: chunk.items.len(), } }) .collect()}
#[test]fn math_fixture_has_equation_tooltips() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let fixture = fixtures::get("clean", "math_inline"); let mut state = harness::state(); let id = harness::page(&mut state, &fixture.name); let render = harness::compile(&mut state, &id, &fixture.source);
assert!(render.document.is_some()); assert!( !render.tooltips.is_empty(), "no equation tooltips for {}", fixture.source, );
for tooltip in &render.tooltips { assert!(!tooltip.items.is_empty(), "empty tooltip chunk"); }}
#[test]fn tooltips_can_be_skipped() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let fixture = fixtures::get("clean", "math_nested"); let mut state = harness::state(); let id = harness::page(&mut state, "no_tooltips"); state.insert_source(&id, fixture.source.clone());
let without = render_svgs_by_items_with_options(&id, &fixture.source, "", None, false, &mut state); assert!(without.tooltips.is_empty());
let with = render_svgs_by_items_with_options(&id, &fixture.source, "", None, true, &mut state); assert!(!with.tooltips.is_empty());}
/// An equation's overlay is built from the synth's AST ranges, so its range is/// the equation's own text: the `$` delimiters included, and nothing past them./// The editor matches the cursor against this range, so a wider range would pop/// the overlay up over the prose beside the equation, and a narrower one would/// miss a cursor resting on a delimiter.////// The fixture is ASCII, so its byte offsets are its UTF-16 offsets.#[test]fn equation_tooltips_are_exactly_the_equation() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let fixture = fixtures::get("clean", "math_nested"); let mut state = harness::state(); let id = harness::page(&mut state, &fixture.name);
let synth = sync_source_state(&id, &fixture.source, "", RenderTarget::Svg, &mut state); let render = harness::compile(&mut state, &id, &fixture.source);
assert_eq!(render.tooltips.len(), synth.equation_ranges.len());
let ranges: Vec<_> = render.tooltips.iter().map(|t| &t.range).collect(); for equation in &synth.equation_ranges { let source = &fixture.source[equation.clone()]; assert!( ranges.contains(&equation), "no overlay is exactly {:?} (got {ranges:?})", source, ); }}
/// One equation is one overlay. A nested introspectable element used to open a/// second bucket in the tag stack, which split the equation in two and left the/// editor rendering whichever half it found first.#[test]fn a_nested_introspectable_element_keeps_one_overlay() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let fixture = fixtures::get("clean", "math_nested"); let mut state = harness::state(); let id = harness::page(&mut state, &fixture.name);
let render = harness::compile(&mut state, &id, &fixture.source);
let nested = fixture .source .find("#strong") .expect("fixture has the nested element"); let matching: Vec<_> = render .tooltips .iter() .filter(|tooltip| { let text = &fixture.source[tooltip.range.start..tooltip.range.end]; text.contains("#strong") }) .collect();
assert_eq!( matching.len(), 1, "the equation with a nested element produced {} overlays", matching.len(), ); assert!( matching[0].range.start <= nested && nested < matching[0].range.end, "the overlay does not cover the nested element: {:?}", matching[0].range, );}
/// A `let`-bound equation renders at every use, so its tag span points at the/// definition while the items sit at the use. Folding item ranges against that/// span produced one range covering the definition and every use, which made/// the overlay pop up over whatever text lay between them.#[test]fn a_let_bound_equation_anchors_at_its_definition() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let source = String::from("#let xn = $(x_n)_(n>=0)$\n\nmiddle\n\n$ xn->l quad \"for some\" l in RR $\n"); let mut state = harness::state(); let id = harness::page(&mut state, "let_bound_equation"); let render = harness::compile(&mut state, &id, &source);
let between = source.find("middle").expect("fixture has the gap"); let shows_over_gap = render .tooltips .iter() .any(|tooltip| tooltip.range.start <= between && between < tooltip.range.end);
assert!( !shows_over_gap, "an overlay covers the text between the definition and the use: {:#?}", render .tooltips .iter() .map(|t| &source[t.range.start..t.range.end]) .collect::<Vec<_>>(), );}
/// The editor shows one equation overlay at a time, so a caret narrows the work/// to that equation. Rendering every equation on every keystroke is what made a/// math-heavy note cost several times a prose note of the same length.#[test]fn a_caret_renders_only_the_equation_under_it() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let fixture = fixtures::get("clean", "math_nested"); let mut state = harness::state(); let id = harness::page(&mut state, &fixture.name);
let synth = sync_source_state(&id, &fixture.source, "", RenderTarget::Svg, &mut state); let every = synth.equation_ranges.len(); assert!(every > 1, "fixture needs several equations");
// A caret inside the second equation, in the editor's raw UTF-16 offsets. let target = synth.equation_ranges[1].clone(); let caret = { let context = state.source_context_map.get(&id).unwrap(); let byte = context.map_repaired_to_raw(target.start); let lines = context.raw_source(&state.world).unwrap().lines(); lines.byte_to_utf16(byte).unwrap() };
let focused = crate::renderer::paged::items::chunk_by_items( &id, &fixture.source, "", Some(caret), RenderTarget::Svg, &mut state, );
assert_eq!( focused.tooltips.len(), 1, "a caret inside one equation rendered {} overlays", focused.tooltips.len(), ); let overlay = focused.tooltips[0].range.clone(); assert!( overlay.start <= target.start && target.end <= overlay.end, "the overlay does not cover the equation under the caret: {overlay:?} vs {target:?}", );
// Without a focus, every equation still gets an overlay. let all = crate::renderer::paged::items::chunk_by_items( &id, &fixture.source, "", None, RenderTarget::Svg, &mut state, ); assert_eq!( all.tooltips.len(), every, "no focus should build every equation" );}
/// A `let`-bound equation is laid out at each use, and the items it produces/// have the spans of its *definition*. Grouping items by source range therefore/// filed them under the definition and left the equation doing the using with an/// overlay missing that content, so the popup showed `sum_(k=0)^n` and then a gap.////// The overlay is drawn from what the tag stack saw, so the popup for the bound/// form comes out identical to the popup for the same math written in place.////// The binding itself gets no popup. Nothing is rendered at the definition, so an/// overlay anchored there would show one arbitrary use of it.#[test]fn a_let_bound_equation_is_drawn_inside_the_equation_that_uses_it() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let overlay_of = |source: &str, name: &str| { let mut state = harness::state(); let id = harness::page(&mut state, name);
let render = crate::renderer::paged::svg::render_svgs_by_items(&id, source, "", None, &mut state);
assert_eq!( render.tooltips.len(), 1, "{name}: {} overlays, expected the one equation that is on the page", render.tooltips.len(), );
let tooltip = &render.tooltips[0];
tooltip.render.svg.clone() };
let inline = overlay_of("$ sum_(k=0)^n (x_n)_(n>=0) $\n", "inline_math"); let bound = overlay_of( "#let xn = $(x_n)_(n>=0)$\n\n$ sum_(k=0)^n xn $\n", "bound_math", );
assert_eq!( bound, inline, "the popup for a bound equation differs from the same math written in place", );}
/// The binding is where the math is written but nothing is rendered there, so no/// overlay may be anchored to it. Two uses each get their own complete popup/// rather than sharing one that absorbed both.#[test]fn each_use_of_a_binding_gets_its_own_overlay() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let source = String::from("#let xn = $(x_n)_(n>=0)$\n\n$ xn $\n\n$ 2 xn $\n"); let mut state = harness::state(); let id = harness::page(&mut state, "shared_binding");
let render = crate::renderer::paged::svg::render_svgs_by_items(&id, &source, "", None, &mut state);
let ranges: Vec<_> = render .tooltips .iter() .map(|tooltip| &source[tooltip.range.start..tooltip.range.end]) .collect();
assert_eq!(ranges, ["$ xn $", "$ 2 xn $"], "one popup per use site");
// The binding spans the first nine bytes of the note. Nothing may be anchored // there, or the popup would appear while the user is editing the `let`. let definition = 0..source.find("$ xn $").unwrap(); for tooltip in &render.tooltips { assert!( tooltip.range.start >= definition.end || tooltip.range.end <= definition.start, "an overlay covers the binding: {:?}", &source[tooltip.range.start..tooltip.range.end], ); }}
/// Tooltips exist per equation the renderer groups, so the snapshot covers/// fixtures with and without them. An empty list is the pin that a fixture/// grew no tooltips. A repaired equation can legitimately produce none.#[test]fn tooltip_snapshots() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let fixtures = fixtures::clean() .into_iter() .map(|fixture| ("clean", fixture)) .chain( fixtures::broken() .into_iter() .map(|fixture| ("broken", fixture)), );
for (group, fixture) in fixtures { let mut state = harness::state(); let id = harness::page(&mut state, &fixture.name);
let render = harness::compile(&mut state, &id, &fixture.source);
insta::assert_json_snapshot!( format!("{group}_{}_tooltips", fixture.name), summarize(&render.tooltips), ); }}
/// The synth lists every equation in the note, including one a binding stores/// but nothing renders. Such an equation gets no layout overlay, so the/// equation index and the overlay index drift apart. The focused-equation/// narrowing used the equation index against the overlay list, which made the/// caret find no overlay to draw.#[test]fn an_unused_math_binding_does_not_hide_later_popups() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
// With and without the trailing newline: typing the closing `$` leaves the // caret at the equation's end, which is also the end of the note. for source in ["#let m = $a+b$\n\n$x^2$\n", "#let m = $a+b$\n\n$x^2$"] { let mut state = harness::state(); let id = harness::page(&mut state, "unused_math_binding");
let start = source.find("$x^2$").expect("fixture has the equation"); let end = start + "$x^2$".len();
for (where_, caret) in [("start", start), ("middle", start + 2), ("end", end)] { let focused = crate::renderer::paged::items::chunk_by_items( &id, source, "", Some(caret), RenderTarget::Svg, &mut state, );
let drawn: Vec<_> = focused .tooltips .iter() .map(|tooltip| &source[tooltip.range.start..tooltip.range.end]) .collect();
assert_eq!( drawn, ["$x^2$"], "a caret at the equation's {where_} produced no popup for {source:?}", ); } }}
/// A caret inside an equation that uses a `let`-bound one must still get that/// equation's popup. The overlay's tag span points at the binding's definition,/// so narrowing by the equation's own range alone would find nothing.#[test]fn a_caret_inside_a_bound_use_gets_the_use_popup() { if !harness::fonts_available() { eprintln!("skipping: bundled fonts missing"); return; }
let source = "#let xn = $(x_n)_(n>=0)$\n\n$ sum_(k=0)^n xn $\n"; let mut state = harness::state(); let id = harness::page(&mut state, "focused_bound_use"); state.resize(&id, Some(600.0), None);
let caret = source.find("sum").expect("fixture has the use") + 1; let focused = crate::renderer::paged::items::chunk_by_items( &id, source, "", Some(caret), RenderTarget::Svg, &mut state, );
let drawn: Vec<_> = focused .tooltips .iter() .map(|tooltip| &source[tooltip.range.start..tooltip.range.end]) .collect();
assert_eq!(drawn, ["$ sum_(k=0)^n xn $"], "no popup for the use");}