docs(cli-rs): describe the LaTeX renderer in its own terms

Keep the source and cell budgets, streaming fallback and styling rules. Drop migration notes and descriptions that repeat the layout tests. The layout oracle and 21 renderer tests passed.
This commit is contained in:
Simon Klee
2026-11-18 20:36:39 +01:00
Unverified
parent ab2d4d1fed
commit 6d804352b1
8 changed files with 41 additions and 125 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "opencode-latex"
description = "The LaTeX plugin (P.2)."
description = "Terminal renderer for fenced and inline LaTeX."
version.workspace = true
edition.workspace = true
license.workspace = true
@@ -42,13 +42,11 @@ pub fn linear(node: Node) -> Node {
.unwrap_or_else(|| Node::Row(vec![group(linear(*body)), symbol(spacing(accent))])),
Node::Variant { variant, body } => Node::Variant { variant, body: Box::new(linear(*body)) },
Node::Color { color, body } => Node::Color { color, body: Box::new(linear(*body)) },
// Limits above and below would take rows: they are scripts.
Node::Operator { value, .. } => Node::Operator { value, limits: Limits::Never },
node @ (Node::Symbol { .. } | Node::Text(_) | Node::Space(_)) => node,
}
}
/// Scripts the layout draws compact, or `^` and `_` with the script beside the base.
fn scripts(base: Node, superscript: Option<Node>, subscript: Option<Node>) -> Node {
let compact = |node: &Option<Node>, superscript: bool| {
node.as_ref().is_none_or(|node| layout::script_text(node, superscript, true).is_some())
@@ -70,7 +68,6 @@ fn scripts(base: Node, superscript: Option<Node>, subscript: Option<Node>) -> No
Node::Row(row)
}
/// The radical with its index: `∛` and `∜`, a superscript index, or the index in parentheses before `√`.
fn radical(index: Option<Node>) -> Node {
let Some(index) = index else { return symbol("√") };
match layout::script_text(&index, true, true).as_deref() {
@@ -133,7 +130,6 @@ fn symbol(value: &str) -> Node {
Node::Symbol { value: value.into(), role: Some(Role::Ordinary) }
}
/// `node` with `mark` on each grapheme of its text, or `None` when it is more than styled text.
fn marked(node: &Node, mark: char) -> Option<Node> {
let marks = |text: &str| text.graphemes(true).map(|grapheme| format!("{grapheme}{mark}")).collect::<String>();
Some(match node {
@@ -160,7 +156,6 @@ fn mark(accent: Accent, body: &Node) -> char {
}
}
/// The spacing form of `accent`, after a body the marks cannot go on.
fn spacing(accent: Accent) -> &'static str {
match accent {
Accent::Hat | Accent::Widehat => "^",
+12 -23
View File
@@ -1,6 +1,4 @@
//! Terminal-cell layout of a parsed formula, a port of `packages/latex/src/layout.ts` with opentui-tex's
//! `math-layout.ts`: a radical `√` with its overbar, matrix rows without blank rows between them, limits in three
//! ways, and blocks of at most [`BLOCK_CELLS`] cells.
//! Terminal-cell layout for parsed TeX formulas.
use unicode_segmentation::UnicodeSegmentation;
@@ -22,8 +20,7 @@ pub struct Cell {
pub style: Style,
}
/// A laid-out formula (TS `MathLayout`): rows of cells, `None` where nothing is drawn. A wide grapheme leaves the
/// cell after it empty.
/// Rows of cells. Empty cells also occupy the trailing columns of a wide grapheme.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Layout {
pub width: usize,
@@ -34,7 +31,7 @@ pub struct Layout {
}
impl std::fmt::Display for Layout {
/// The rows as text with trailing blanks trimmed (TS `toString`).
/// Rows as text, without trailing blanks.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let rows: Vec<String> = self
.cells
@@ -61,7 +58,7 @@ impl std::fmt::Display for Layout {
}
}
/// Layout options (TS `RenderLatexOptions`); the plugin uses the defaults.
/// Options for display and inline layout.
#[derive(Clone, Debug)]
pub struct Options {
/// Operator limits above and below, as display math sets them.
@@ -69,7 +66,7 @@ pub struct Options {
/// Unicode super- and subscript characters where every script character has one.
pub compact_scripts: bool,
pub color: Option<String>,
/// Inline math: compact scripts take every super- and subscript letter Unicode has, not only TS's.
/// Allow the extended inline script alphabet.
pub inline: bool,
}
@@ -79,12 +76,10 @@ impl Default for Options {
}
}
/// The cells one layout may allocate in all its intermediate blocks. The parser bounds the source, not the
/// rectangles: a short ragged matrix pays for its longest row on every row. TS leaves such input to a `RangeError`,
/// which `layoutLatex` turns into the source fallback.
/// Total cell budget across intermediate blocks. Even a short ragged matrix can occupy a large rectangle.
pub const MAX_CELLS: usize = 1 << 20;
/// The cells one block may take (opentui-tex's `CELL_LIMIT`): a wider or taller formula is not drawn.
/// Maximum cells in a single block.
pub const BLOCK_CELLS: usize = 16384;
thread_local! {
@@ -121,7 +116,6 @@ struct Context {
variant: Option<Variant>,
}
/// TS `Box`.
struct Block {
width: i32,
height: i32,
@@ -184,7 +178,6 @@ fn layout_fraction(numerator: &Node, denominator: &Node, bar: bool, align: Optio
// parentheses align between the two entries.
let gap = 1;
let height = numerator.height + denominator.height + gap;
// TeX places a fraction's math axis on its rule (or the equivalent empty axis row for a binomial).
let baseline = numerator.height;
let mut result = blank(width, height, baseline);
let numerator_x = match align {
@@ -222,7 +215,7 @@ fn layout_root(body: &Node, index: Option<&Node>, context: &Context) -> Block {
}
fn layout_scripts(base: &Node, superscript: Option<&Node>, subscript: Option<&Node>, context: &Context) -> Block {
// A script that lays out to nothing, such as `{}` or an empty `\textcolor`, adds no geometry (opentui-tex).
// An empty script adds no geometry.
let present = |node: &Node| layout_node(node, context).width > 0;
let (superscript, subscript) = (superscript.filter(|n| present(n)), subscript.filter(|n| present(n)));
let limits = match script_target(base) {
@@ -288,8 +281,7 @@ fn script_target(node: &Node) -> &Node {
}
}
/// `node` as compact script characters, when it is plain text that has them all: TS's characters, or with `inline`
/// every one Unicode has.
/// Returns compact script text only if the alphabet covers every character.
pub(crate) fn script_text(node: &Node, superscript: bool, inline: bool) -> Option<String> {
let (base, extra) = if superscript { (SUPERSCRIPTS, INLINE_SUPERSCRIPTS) } else { (SUBSCRIPTS, INLINE_SUBSCRIPTS) };
simple_text(node)?
@@ -434,7 +426,7 @@ fn delimiter_box(delimiter: &str, height: i32, baseline: i32, left: bool, style:
if height <= 1 {
return text_box(delimiter, style);
}
// Combining marks typed after a delimiter stay on its middle row (opentui-tex).
// Combining marks on a stretched delimiter stay on its middle row.
let split = delimiter.chars().next().map_or(0, char::len_utf8);
let (base, marks) = delimiter.split_at(split);
let glyphs = delimiter_glyphs(base);
@@ -558,11 +550,9 @@ fn set_cell(block: &mut Block, x: i32, y: i32, text: &str, style: &Style) {
fn node_role(node: &Node) -> Option<Role> {
match node {
Node::Symbol { role, .. } => *role,
// Tall constructs need a terminal-cell side bearing. Treating them like operators gives their fraction
// bars and radical hooks breathing room without adding padding inside the construct itself.
// Use operator spacing around tall constructs without padding inside them.
Node::Operator { .. } | Node::Fraction { .. } | Node::Root { .. } | Node::Matrix { .. } => Some(Role::Operator),
Node::Scripts { base, .. } => node_role(base),
// A styled operator spaces as one (opentui-tex): `\mathbf{\sin}x`.
Node::Variant { body, .. } | Node::Color { body, .. } => node_role(body),
_ => None,
}
@@ -637,8 +627,7 @@ fn apply_variant(value: &str, variant: Option<Variant>) -> String {
.collect()
}
/// The cells a grapheme takes, as opentui-tex's `string-width` counts them: East Asian wide and emoji graphemes take
/// two, combining and default-ignorable ones none.
/// Terminal width in cells, including wide and zero-width graphemes.
pub fn cell_width(value: &str) -> i32 {
unicode_width::UnicodeWidthStr::width(value) as i32
}
+8 -23
View File
@@ -1,7 +1,4 @@
//! The LaTeX plugin: `opencode.latex` draws `latex` and `math` Markdown fences as Unicode
//! math in terminal cells, as `packages/latex/src/plugin.ts` does, and inline Markdown math on one row, which TS
//! does not. [`parse`] and [`layout`] port the TypeScript parser and layout with opentui-tex's fixes; [`inline`]
//! writes math on one row; [`plugin`] renders both (`markdown.ts`), on the one plugin API (O-67).
//! Renders fenced LaTeX and inline math as Unicode terminal cells.
pub mod inline;
pub mod layout;
@@ -21,15 +18,14 @@ use opencode_plugin_abi::{Color, Fence, Fenced, Line, Plugin, Rgba, Setup, Span}
pub use layout::{Layout, Options};
pub use parse::{Mode, ParseError};
/// The longest source drawn, in UTF-8 bytes (opentui-tex's `UNICODE_TEX_SOURCE_LENGTH_MAX`).
/// Maximum input length in UTF-8 bytes.
pub const MAX_SOURCE_BYTES: usize = 4096;
/// The plugin, on the one plugin API (O-67).
/// Registers the fenced and inline math renderers.
pub fn plugin() -> Plugin {
Plugin { id: "opencode.latex", setup }
}
/// One renderer for both fence languages, as TS registers, and the inline math renderer.
fn setup(setup: &mut Setup) -> Result<(), String> {
register(setup, Rc::default());
Ok(())
@@ -48,13 +44,12 @@ fn register(setup: &mut Setup, shown: Shown) {
setup.inline_math(|_, tex| inline_line(tex));
}
/// Inline math's row in the styles of display math, without colors so it takes the text's, except `\textcolor`'s;
/// `None` when the TeX does not parse, so the TeX shows as typed.
/// Renders a single row, inheriting the surrounding color except for `\textcolor`. Returns `None` on invalid TeX.
pub fn inline_line(tex: &str) -> Option<Line> {
formula(&inline::render(tex).ok()?, true).into_iter().next()
}
/// Lays out `source` as display math (TS `renderLatex` in strict mode).
/// Lays out display math in strict mode.
pub fn render(source: &str) -> Result<Layout, ParseError> {
render_with(source, &Options::default())
}
@@ -63,9 +58,7 @@ pub fn render_with(source: &str, options: &Options) -> Result<Layout, ParseError
render_mode(source, options, Mode::default())
}
/// Lays out `source` read in `mode`. As opentui-tex's Unicode backend, a source of no or more than
/// [`MAX_SOURCE_BYTES`] bytes, a block of more than [`layout::BLOCK_CELLS`] cells, and a formula that draws nothing
/// are errors.
/// Lays out a formula in `mode`. Empty, oversized and invisible formulas are errors.
pub fn render_mode(source: &str, options: &Options, mode: Mode) -> Result<Layout, ParseError> {
check_source(source)?;
let layout = layout::layout(&parse::parse_with(source, mode)?, options)
@@ -87,16 +80,12 @@ fn fail(message: String) -> ParseError {
ParseError { message, position: 0 }
}
/// The source of each formula shown while streaming, by fence key, so its block can keep it.
type Shown = Rc<RefCell<HashMap<u64, String>>>;
/// The Markdown code-block renderer (TS `createLatexCodeBlockRenderer`): the formula in a horizontally scrolling
/// row, or the code view when it does not parse. While the document streams and the fence is open, a block whose
/// text extends the last formula that rendered keeps showing that formula.
/// An open fence keeps its last good render while more of the same formula arrives.
fn renderer(shown: Shown) -> impl Fn(&Fence) -> Fenced {
move |fence| {
let streaming = fence.streaming && !fence.closed;
// An open fence draws as far as it goes, with `□` for what has not come (opentui-tex's preview).
let incomplete = Mode { incomplete: true, ..Mode::default() };
let layout = render(&fence.code)
.or_else(
@@ -117,9 +106,7 @@ fn renderer(shown: Shown) -> impl Fn(&Fence) -> Fenced {
}
}
/// The formula's rows: structure in the muted text color, relations bold. The host draws them in a row that
/// scrolls sideways instead of wrapping. TS adds a row of top margin; the Rust Markdown already puts a blank row
/// between blocks. `inline` rows have no colors, so they take the text's.
/// Inline spans inherit the surrounding text color.
fn formula(layout: &Layout, inline: bool) -> Vec<Line> {
layout
.cells
@@ -137,7 +124,6 @@ fn formula(layout: &Layout, inline: bool) -> Vec<Line> {
|| ('\u{239b}'..='\u{23ad}').contains(&c)
|| ('\u{2500}'..='\u{257f}').contains(&c)
});
// `\textcolor` keeps its color (opentui-tex); TS draws every cell in the theme's.
let named = style.and_then(|style| color(style.color.as_deref()?));
let theme = (!inline).then(|| if structural { "text.muted" } else { "text.base" }.into());
let span = Span {
@@ -157,7 +143,6 @@ fn formula(layout: &Layout, inline: bool) -> Vec<Line> {
.collect()
}
/// Appends `text` to the last span when the style matches, so a row is a few spans rather than one per cell.
fn push(line: &mut Line, text: &str, span: Span) {
let same =
|last: &&mut Span| (&last.fg, last.bold, last.italic, last.dim) == (&span.fg, span.bold, span.italic, span.dim);
+12 -21
View File
@@ -1,17 +1,12 @@
//! The LaTeX math parser, a port of `packages/latex/src/parser.ts` and `limits.ts` in strict mode, the mode the
//! TypeScript plugin uses, with the fixes of opentui-tex's `math-parser.ts`: literals are whole graphemes, combining
//! marks after a command or delimiter stay on it, style directives and `\limits` make no atom, `\limits` reaches an
//! operator through styles, `\color` stops at a cell, comments end at any line break, operators place limits in
//! three ways, and the permissive and incomplete modes of `parseMath` and `parseMathIncomplete` ([`Mode`]). User
//! macros are not ported: the plugin never passes any.
//! Parses TeX math into a layout tree, optionally accepting an incomplete streaming prefix.
use unicode_segmentation::UnicodeSegmentation;
use crate::symbols;
/// Longest accepted source, in characters (`DEFAULT_MAX_SOURCE_LENGTH`).
/// Maximum input length in characters.
const MAX_SOURCE_LENGTH: usize = 100_000;
/// Deepest accepted brace or construct nesting (`DEFAULT_MAX_NESTING_DEPTH`).
/// Maximum depth of nested braces and constructs.
const MAX_DEPTH: usize = 256;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -73,7 +68,7 @@ pub enum Align {
Right,
}
/// Where an operator puts its scripts (opentui-tex's `limits: boolean | "display"`).
/// Where an operator places its scripts.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Limits {
/// Beside it: `\int`, `\sin`, `\nolimits`.
@@ -84,21 +79,19 @@ pub enum Limits {
Always,
}
/// How [`parse_with`] reads a source.
/// Parser options for unsupported commands and streaming input.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Mode {
/// An unsupported command is its name as text and an unsupported delimiter its name or character, instead of an
/// error (opentui-tex's default; the plugin parses strictly and shows the source).
/// Treat unsupported commands and delimiters as literal text.
pub permissive: bool,
/// The source is a prefix of a formula, as one streams: a construct the end cuts off takes `□` for what is
/// missing (`parseMathIncomplete`). Malformed syntax before the end still fails.
/// Fill a missing operand at end of input with `□`. Earlier syntax errors still fail.
pub incomplete: bool,
}
/// What an incomplete formula shows for a missing part.
pub const PLACEHOLDER: &str = "\u{25a1}";
/// A parsed formula (TS `MathNode`).
/// A parsed formula.
#[derive(Clone, Debug, PartialEq)]
pub enum Node {
Row(Vec<Node>),
@@ -118,7 +111,7 @@ pub enum Node {
Color { color: String, body: Box<Node> },
}
/// Why a source is not rendered (TS `LatexParseError`); the plugin then shows the source.
/// An error parsing or laying out a formula.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ParseError {
pub message: String,
@@ -175,7 +168,6 @@ fn boundaries(source: &[char]) -> Vec<bool> {
starts
}
/// Rejects brace nesting deeper than [`MAX_DEPTH`] before parsing (`assertNestingDepth`).
fn check_nesting(source: &[char]) -> Result<()> {
let mut depth = 0usize;
let mut slashes = 0usize;
@@ -392,7 +384,7 @@ impl Parser {
}
"not" => {
self.skip_whitespace();
// At the end TS negates an empty symbol.
// A trailing negation applies to the empty symbol.
if self.done() {
return Ok(Node::Symbol { value: negate(""), role: Some(infer_role("")) });
}
@@ -773,7 +765,6 @@ impl Parser {
self.source[..index].iter().rev().take_while(|&&c| c == '\\').count() % 2 == 1
}
/// A raw group with TeX's text escapes resolved (`readTextGroup`).
fn read_text_group(&mut self) -> Result<String> {
let (raw, closed) = self.read_raw(true)?;
let raw: Vec<char> =
@@ -782,7 +773,7 @@ impl Parser {
let mut index = 0;
while index < raw.len() {
let char = raw[index];
// TS's `\\([A-Za-z@]+|.)`: `.` does not match a line break.
// The escaped-character pattern excludes line breaks.
if char != '\\' || raw.get(index + 1).is_none_or(|&c| c == '\n' || c == '\r') {
text.push(if char == '~' { ' ' } else { char });
index += 1;
@@ -915,7 +906,7 @@ fn is_command_letter(char: char) -> bool {
char.is_ascii_alphabetic() || char == '@'
}
/// The spacing role of a literal character. As in TS, an empty value (read at the end) is binary.
/// An empty literal at end of input has binary spacing.
fn infer_role(value: &str) -> Role {
if "+-*/×÷±∓".contains(value) {
return Role::Binary;
+4 -5
View File
@@ -1,9 +1,8 @@
//! Command tables, as `packages/latex/src/symbols.ts` with opentui-tex's `math-symbols.ts`: TeX's `\epsilon` (`ϵ`)
//! and `\varepsilon` (`ε`), `•` for `\bullet`, `\degree`, and `/`, `<` and `>` as delimiters.
//! TeX command names, glyphs and spacing roles.
use crate::parse::Role;
/// The symbol a command names and its spacing role (`symbolTable`).
/// Looks up a symbol and its spacing role.
pub fn symbol(command: &str) -> Option<(&'static str, Role)> {
let role = |table: &[(&str, &'static str)], role: Role| {
table.iter().find(|(name, _)| *name == command).map(|(_, value)| (*value, role))
@@ -23,12 +22,12 @@ pub fn is_named_operator(command: &str) -> bool {
NAMED_OPERATORS.contains(&command)
}
/// The glyph of a delimiter token or command name (`delimiterTable`).
/// Looks up a delimiter glyph.
pub fn delimiter(name: &str) -> Option<&'static str> {
lookup(DELIMITERS, name)
}
/// The width in cells of a spacing command (`spacingCommands`).
/// Width of a spacing command in cells.
pub fn spacing(command: &str) -> Option<usize> {
SPACING.iter().find(|(name, _)| *name == command).map(|(_, width)| *width)
}
+3 -34
View File
@@ -9,11 +9,7 @@ use serde_json::Value;
use crate::layout::Options;
/// P-ORACLE: every case of `parity/fixtures/P.2/layouts.json` (`parity/oracle/P.2-latex.ts`, the TypeScript
/// `renderLatex` over the sources of `packages/latex/src/*.test.ts` and more) lays out to the same text, geometry,
/// and cell styles, or fails to parse in both, except where opentui-tex draws otherwise: then Rust lays out as
/// `oracle_rust.json` expects, which must differ from TS (`parity/deviations/P2-latex-opentui-tex.md`).
/// `OPENCODE_RS_LATEX_ORACLE=write` rewrites `oracle_rust.json` with every case that differs from TS.
/// `OPENCODE_RS_LATEX_ORACLE=write` records only layouts that differ from the TypeScript renderer.
#[test]
fn layouts_match_typescript() {
let cases: Vec<Value> = serde_json::from_str(include_str!("../../../parity/fixtures/P.2/layouts.json")).unwrap();
@@ -46,7 +42,6 @@ fn layouts_match_typescript() {
assert!(failures.is_empty(), "{} of {} differ:\n{}", failures.len(), cases.len(), failures.join("\n"));
}
/// `case` as Rust lays it out, in the oracle's record.
fn oracle_case(case: &Value) -> Value {
let source = case["source"].as_str().unwrap();
let options = Options {
@@ -94,7 +89,7 @@ fn styled(layout: &crate::Layout) -> Vec<String> {
.collect()
}
/// A test app in dark `opencode`, with the screen cleared as the TypeScript screen is (`crates/ui/tests/all/content.rs`).
/// Match the oracle's screen background.
fn app(width: u16, height: u16) -> (TestApp, Theme) {
let theme = Catalog::default().load("opencode", ThemeMode::Dark).unwrap().theme;
let options = AppOptions {
@@ -109,7 +104,6 @@ fn app(width: u16, height: u16) -> (TestApp, Theme) {
(TestApp::with_options(width, height, options), theme)
}
/// The host as the plugin reads it: dark.
struct Dark;
impl opencode_plugin_abi::Read for Dark {
@@ -122,8 +116,6 @@ impl opencode_plugin_abi::Read for Dark {
}
}
/// `TextPart`'s Markdown with the plugin's renderers for `latex` and `math` fences, through the host's fence adapter,
/// and for inline math, as the plugin host registers them.
fn markdown(app: &TestApp, theme: &Theme, streaming: bool) -> Markdown {
let mut setup = opencode_plugin_abi::Registered::new("opencode.latex", None);
(crate::plugin().setup)(&mut setup.setup()).unwrap();
@@ -145,8 +137,7 @@ fn markdown(app: &TestApp, theme: &Theme, streaming: bool) -> Markdown {
opencode_ui::markdown::view(app, theme, &syntax, &renderers, true, streaming)
}
/// P-COMP: the fences of `parity/component/P.2-latex.tsx` in the transcript's Markdown, every cell, but the cells
/// opentui-tex's radical and matrix rows change (`parity/deviations/P2-latex-opentui-tex.md`): exactly that many.
/// The radical and matrix layouts have known differences; all other fixtures match cell for cell.
#[test]
fn transcript_fences_match_typescript() {
let corpus: Value = serde_json::from_str(include_str!("../../../parity/fixtures/P.2/corpus.json")).unwrap();
@@ -170,7 +161,6 @@ fn transcript_fences_match_typescript() {
const ADAPTER_DIFFERS: usize = 197;
/// The frame of `source` in the transcript's Markdown, `width` columns wide.
fn frame(source: &str, width: u16) -> String {
let (app, theme) = app(width, 16);
let text = markdown(&app, &theme, false).set_markdown(source);
@@ -178,17 +168,13 @@ fn frame(source: &str, width: u16) -> String {
app.render().text()
}
/// Beyond TS (`parity/deviations/P2-latex-inline.md`): with the plugin, Markdown parses math (OpenTUI #2072).
/// `$…$` and `\(…\)` draw on one row in the text; `$$…$$` and `\[…\]` reach the `math` fence renderer.
#[test]
fn markdown_math_renders() {
for (source, shown) in [
("Energy is $E=mc^2$ here.", "Energy is E = mc² here."),
("Then \\(\\alpha \\le \\frac{1}{2}\\) holds.", "Then α ≤ 1/2 holds."),
("A root $\\sqrt{x+1}$, a sum $\\sum_{i=1}^n i$.", "A root √(x + 1), a sum ∑ⁿᵢ₌₁ i."),
// TeX that does not parse shows as typed, without its delimiters.
("Broken $\\frac{1}{x$ here.", "Broken \\frac{1}{x here."),
// opentui-tex's `document.test.ts`, where OpenTUI's parser takes `parseInline`'s place.
("cost \\$5 and $x^2$ now", "cost $5 and x² now"),
("Text $x \\$ y$ after", "Text x$y after"),
("an $open formula", "an $open formula"),
@@ -203,7 +189,6 @@ fn markdown_math_renders() {
}
}
/// Dollars that are not math stay text, as OpenTUI decides: currency, shell variables, a lone `$`.
#[test]
fn dollars_that_are_not_math_stay_text() {
for (source, shown) in [
@@ -219,8 +204,6 @@ fn dollars_that_are_not_math_stay_text() {
}
}
/// Streaming: a `$` shows as text until OpenTUI decides it is math, then the math shows and stays (its property:
/// shown math never reverts), so a half-typed `$x^` never draws as broken math.
#[test]
fn streaming_inline_math_shows_once_closed() {
let frames = stream(&["Energy $E=mc", "^", "2", "$ now", " and $x^", "$ too"], false);
@@ -230,11 +213,9 @@ fn streaming_inline_math_shows_once_closed() {
assert!(frame.contains("Energy E = mc² now"), "{frame}");
}
assert!(frames[4].contains("and $x^"), "{}", frames[4]);
// `x^` closed does not parse: its TeX shows as typed.
assert!(frames[5].contains("and x^ too"), "{}", frames[5]);
}
/// Streams `chunks` into one document, returning the frame text after each.
fn stream(chunks: &[&str], stop: bool) -> Vec<String> {
let (app, theme) = app(40, 10);
let view = markdown(&app, &theme, true);
@@ -253,9 +234,6 @@ fn stream(chunks: &[&str], stop: bool) -> Vec<String> {
frames
}
/// opentui-tex's streaming preview (`parseMathIncomplete`): an open fence draws as far as it goes, `□` for what has
/// not come, where TS shows the source until the formula parses (`markdown.test.ts`: "renders the next valid
/// formula after an incomplete streaming prefix").
#[test]
fn streaming_draws_an_incomplete_formula() {
let frames = stream(&["```latex\n\\frac{1}{", "2}", "\n```"], true);
@@ -279,13 +257,11 @@ fn ending_the_fence_or_stream_drops_the_preview() {
assert!(frames[1].contains("x²") && !frames[1].contains('\\'), "{}", frames[1]);
assert!(frames[2].contains("x^2 + \\frac{1}{"), "{}", frames[2]);
}
// The closing fence arrives with the broken math, so the source shows before the stream ends too.
let frames = stream(&["```latex\nx^2", " + \\unsupported{x}\n```\n"], true);
assert!(frames[1].contains("x^2 + \\unsupported{x}"), "{}", frames[1]);
assert!(frames[2].contains("x^2 + \\unsupported{x}"), "{}", frames[2]);
}
/// `markdown.test.ts`: "does not reuse another fence's preview or keep a removed fence's preview".
#[test]
fn a_preview_belongs_to_its_fence() {
let (app, theme) = app(40, 12);
@@ -300,7 +276,6 @@ fn a_preview_belongs_to_its_fence() {
assert!(app.render().text().contains("x^2 + \\fr"));
}
/// `markdown.test.ts`: "allows wide formulas to scroll horizontally without wrapping".
#[test]
fn wide_formulas_scroll_sideways() {
let (app, theme) = app(24, 6);
@@ -316,8 +291,6 @@ fn wide_formulas_scroll_sideways() {
assert!(frame.contains("Finish") && !frame.contains("Start"), "{frame}");
}
// Expected (bigboi notes): the formula a streaming fence keeps goes once the host frees its block, so N streaming
// fences drawn and removed leave nothing.
#[test]
fn removed_streaming_fences_leave_nothing_kept() {
let (app, theme) = app(40, 10);
@@ -351,15 +324,11 @@ fn removed_streaming_fences_leave_nothing_kept() {
assert!(shown.borrow().is_empty(), "{:?}", shown.borrow());
}
// Expected (r2 review, bigboi-ui-2 and carbon-ui-1): a short ragged matrix whose rectangle takes more cells than
// the layout allows fails like oversized input (TS `layoutLatex` returns nothing on `RangeError`), so the fence shows
// its source instead of allocating the grid on the UI thread.
#[test]
fn an_oversized_matrix_falls_back_to_the_source() {
let source = format!("\\begin{{matrix}}{}{}\\end{{matrix}}", "&".repeat(1500), "\\\\".repeat(1500));
assert!(source.len() < 10_000);
assert!(crate::render(&source).is_err());
// opentui-tex's block limit: 16 384 cells.
let small = format!("\\begin{{matrix}}{}{}\\end{{matrix}}", "x&".repeat(60), "x\\\\".repeat(60));
assert!(crate::render(&small).is_ok());
}
+1 -13
View File
@@ -1,8 +1,3 @@
//! Rows ported from opentui-tex (`src/math-parser.test.ts`, `math-layout.test.ts`, `unicode-tex-backend.test.ts`),
//! in each mode they name. Where the TS oracle disagrees, `oracle_rust.json` holds Rust's expectation
//! (`parity/deviations/P2-latex-opentui-tex.md`). `steps/latex-more.md` lists what is not ported. Then the inline
//! rows.
use crate::layout::{self, Options};
use crate::parse::{Accent, Environment, Limits, Mode, Node, PLACEHOLDER, Role, Variant, parse, parse_with};
@@ -435,12 +430,10 @@ fn inline_math_takes_one_row() {
] {
assert_eq!(inline(source), expected, "{source:?}");
}
// Styles stay: relations bold, as display math draws them.
let line = crate::inline_line("a \\le \\mathit{b}").unwrap();
assert!(line.iter().any(|span| span.text.contains('≤') && span.bold), "{line:?}");
assert!(line.iter().any(|span| span.text == "b" && span.italic), "{line:?}");
assert!(line.iter().all(|span| span.fg.is_none()), "{line:?}");
// TeX that does not parse shows as typed: the renderer returns nothing.
for source in ["\\frac{1}{", "x^", "\\unknown", "", " "] {
assert_eq!(crate::inline_line(source), None, "{source:?}");
}
@@ -452,7 +445,6 @@ fn accents_on_styled_text() {
assert_eq!(inline("\\tilde{\\mathbb{R}}"), "ℝ\u{303}");
}
/// `source` laid out inline-style (opentui-tex's `display: false`), as text.
fn text_mode(source: &str, display: bool, mode: Mode) -> String {
let options = Options { display, ..Options::default() };
crate::render_mode(source, &options, mode).unwrap_or_else(|error| panic!("{source:?}: {error}")).to_string()
@@ -648,7 +640,6 @@ fn incomplete_mode_draws_a_prefix_with_placeholders() {
let both = Mode { permissive: true, incomplete: true };
assert!(text_mode("\\left(x\\rightfoo", false, both).contains("\\rightfoo"));
assert!(parse("\\left(x\\rightfoo").is_err());
// Malformed syntax before the end fails in both modes.
for source in [
"a}b",
"\\frac}",
@@ -688,14 +679,12 @@ fn sources_and_blocks_are_bounded() {
};
assert!(layout::layout(&wide, &options).is_none());
assert!(layout::layout(&Node::Brace { body: Box::new(text(&"x".repeat(8193))), over: true }, &options).is_none());
// Widths as `string-width` counts them: a ZWJ sequence, a flag, and a keycap take two cells.
assert_eq!(crate::render("\u{1f469}\u{200d}\u{1f52c}^2+\u{1f1e9}\u{1f1f0}").unwrap().width, 8);
assert_eq!(
// `#` is no delimiter: permissive mode, as opentui-tex's row.
text_mode("\\left#\u{fe0f}\u{20e3}\\frac{1}{2}\\right#\u{fe0f}\u{20e3}", false, PERMISSIVE),
"# 1 #\n#\u{fe0f}\u{20e3}───#\u{fe0f}\u{20e3}\n# 2 #"
);
// A zero-width delimiter mark draws or fails, and never panics.
// Zero-width delimiter marks may fail to render, but must not panic.
for display in [false, true] {
for mode in [Mode::default(), INCOMPLETE] {
let _ = crate::render_mode(
@@ -799,7 +788,6 @@ fn alphabets_and_styles() {
assert_eq!(display("\\epsilon\\varepsilon\\phi\\varphi"), "ϵεϕφ");
}
/// `\textcolor` keeps its color in the drawn rows, in display and inline math (opentui-tex's spans).
#[test]
fn colors_reach_the_rows() {
use opencode_plugin_abi::{Color, Rgba};