diff --git a/packages/cli/src/repl/fitting.ts b/packages/cli/src/repl/fitting.ts index edb7c210e..c6ef19095 100644 --- a/packages/cli/src/repl/fitting.ts +++ b/packages/cli/src/repl/fitting.ts @@ -154,8 +154,8 @@ export interface ReplFitGeometry { * The geometry one running REPL is holding on to. * * Typing moves the draft, not the reading beside it, and a reading is the - * expensive thing to fit: a two-hundred-line entry costs an engine render per - * line plus a binary search per overflowing row, and a keystroke used to pay + * expensive thing to fit: a two-hundred-line entry costs a binary search per + * overflowing row, its lines searched together, and a keystroke used to pay * that twice. This keeps the last answer and the exact inputs it was computed * from, so a frame whose reading has not moved reuses it. * @@ -186,9 +186,8 @@ export interface ReplFitStore { * {@link ReplFitStore}. The chat's newest line grows on its own and a new * message is appended on its own, so a store that could only answer for a whole * list would remeasure every older message for every delta — and a wrapping - * message costs an engine render per line plus a binary search per row it - * overflows into. Twenty of them turned one typed key into 1,284 measurement - * renders. + * message costs a binary search per row it overflows into. Twenty of them + * turned one typed key into 1,284 measurement renders. * * A cut is an answer for one exact text at one exact room and size, so that is * what it is keyed by. Two lines spelled the same are the same question and @@ -349,6 +348,18 @@ const WIDTHS_PER_RENDER = 1024; */ const WORDS_PER_RENDER = 8192; +/** + * How many space-separated parts one render of a many-line search may carry. + * + * The engine's own figure scales with the terminal it was opened at: measured + * on a fresh engine, about 16,383 parts at 120x40 and above, 7,677 at 80x24, + * 4,797 at 60x20 and 4,095 at 40x12. A search step asks a question of every + * line still being cut, so on a long reading it fills whatever budget it is + * given, and at 80x24 a full one was refused and the reading cut line by line. + * This sits below the smallest of those figures. + */ +const SEARCH_WORDS_PER_RENDER = 2048; + /** What one text costs that budget, counted the way the engine counts it. */ function wordsIn(text: string): number { let parts = 1; @@ -392,12 +403,14 @@ function windowOf(text: string, room: number): number { * renders as the count needs — and every answer copied out of each batch * before the next render, because the engine's geometry is only valid until * the next measurement. The answers come back in the order they were asked - * for, one per text, empty ones included. + * for, one per text, empty ones included. `words` is the part budget each + * render is held to. */ function* widths( renderer: ReplRenderer, size: ReplTerminalSize, texts: readonly string[], + words = WORDS_PER_RENDER, ): Operation> { if (texts.length === 0) { return Ok([]); @@ -406,15 +419,15 @@ function* widths( let from = 0; while (from < texts.length) { let to = from; - let words = 0; + let carried = 0; while (to < texts.length && to - from < WIDTHS_PER_RENDER) { const cost = wordsIn(texts[to]); // A text that outgrows the budget on its own still goes, alone, so the // engine's refusal names that text rather than this loop never advancing. - if (to > from && words + cost > WORDS_PER_RENDER) { + if (to > from && carried + cost > words) { break; } - words += cost; + carried += cost; to += 1; } const batch = yield* batchWidths(renderer, size, texts.slice(from, to)); @@ -645,17 +658,24 @@ function* fitGeometry( return reserved; } const reservation = reserved.value; - const cuts: (readonly ReplRowCut[])[] = []; // Nesting is drawn inside the content region rather than beside it, so a // deeply nested element loses text room rather than pushing the status column // off the pane it was measured against. - for (const line of lines) { - const indent = Math.min(line.depth * 2, Math.max(0, reservation.content - 1)); - const fitted = yield* fitLine(renderer, size, line, reservation.content - indent); - if (!fitted.ok) { - return fitted; + const rooms = lines.map((line) => ({ + text: line.text, + room: reservation.content - Math.min(line.depth * 2, Math.max(0, reservation.content - 1)), + })); + let cuts = yield* cutTogether(renderer, size, rooms); + if (cuts === undefined) { + const inOrder: (readonly ReplRowCut[])[] = []; + for (const one of rooms) { + const fitted = yield* cutLines(renderer, size, [one]); + if (!fitted.ok) { + return fitted; + } + inOrder.push(fitted.value[0]); } - cuts.push(fitted.value); + cuts = inOrder; } // The finished candidates, measured again as the text they ended up being. // The search above narrowed over prefixes; this is the row, and a row that @@ -788,6 +808,22 @@ export function* fitPlain( return Ok([]); } const fitted: ReplFittedLine[] = []; + // Every distinct line the store does not hold, cut together where that asks + // the same questions. The loop below still owns the store and the order, so a + // line this could not answer is cut there exactly as it would be alone. + const missing = [ + ...new Set( + lines.filter((one) => store?.held(one.text, room, size) === undefined).map((one) => one.text), + ), + ]; + const together = yield* cutTogether( + renderer, + size, + missing.map((text) => ({ text, room })), + ); + const batched = new Map( + together === undefined ? [] : missing.map((text, at) => [text, together[at]] as const), + ); for (const one of lines) { const runs = one.runs ?? tokenRuns([{ text: one.text, token: "source" }]); const line = Object.freeze({ @@ -802,11 +838,16 @@ export function* fitPlain( const retained = store?.held(one.text, room, size); let pieces: readonly ReplRowCut[]; if (retained === undefined) { - const cuts = yield* fitLine(renderer, size, line, room); - if (!cuts.ok) { - return cuts; + const answered = batched.get(one.text); + if (answered === undefined) { + const cuts = yield* cutLines(renderer, size, [{ text: line.text, room }]); + if (!cuts.ok) { + return cuts; + } + pieces = cuts.value[0]; + } else { + pieces = answered; } - pieces = cuts.value; store?.keep(one.text, room, size, pieces); } else { pieces = retained; @@ -841,53 +882,181 @@ export function* fitPlain( return Ok(Object.freeze(fitted)); } +/** One logical line to cut, and the cells each of its rows may take. */ +interface ReplLineRoom { + readonly text: string; + readonly room: number; +} + +/** + * Many lines cut in one search, or nothing where that is not the same search. + * + * Each measuring render costs about the same whether it carries one text or a + * thousand, and a reading of a thousand wrapping lines asked one text per + * render: about fifteen renders per line. Cut together, every line's question + * for the same step shares a render, so the count follows the depth of the + * deepest search rather than the number of lines. + * + * Only below the measuring terminal's own width. A text wider than the columns + * the engine was given is answered from the layout around it — alone a + * breakable one reports the terminal's width, beside a wider unbreakable one it + * reports its own — so a room that reaches the columns is cut one line at a + * time, as a lone question. + * + * And only where nothing failed. A failure found here is the one some line + * reached first in the search, which need not be the first line that fails, + * so it is discarded and the caller cuts its lines one at a time again: in + * written order, reaching the first failure and saying what it says. + */ +function* cutTogether( + renderer: ReplRenderer, + size: ReplTerminalSize, + lines: readonly ReplLineRoom[], +): Operation { + if (lines.length < 2 || lines.some((one) => one.room >= size.columns)) { + return undefined; + } + const cut = yield* cutLines(renderer, size, lines); + return cut.ok ? cut.value : undefined; +} + +/** Where one line's search stands: the row it is cutting, and how far that row has narrowed. */ +interface ReplOpenLine { + readonly index: number; + readonly text: string; + readonly room: number; + readonly hanging: number; + readonly cuts: ReplRowCut[]; + at: number; + rest: string; + rowRoom: number; + stops: readonly number[]; + low: number; + high: number; +} + /** - * One logical line as the pieces it is cut into. + * Logical lines as the pieces they are cut into, every line searched at once. + * + * Each line asks what it would ask alone, in the same order: one window probe, + * then for each row a search over that row's grapheme boundaries. What lines + * share is the render a step's questions travel in, so one line given alone is + * the sequential cut, one render per question. * * Widths are filled in by the pass that measures every finished piece at once; * nothing reads them in between. */ -function* fitLine( +function* cutLines( renderer: ReplRenderer, size: ReplTerminalSize, - line: ReplReadingLine, - room: number, -): Operation> { + lines: readonly ReplLineRoom[], +): Operation> { + const cut: (readonly ReplRowCut[] | undefined)[] = lines.map(() => undefined); // A blank line is a row. Measuring it would ask the engine the width of // nothing, and dropping it would edit the shape of somebody's document. - if (line.text.length === 0) { - return Ok([piece(0, 0, false, 0)]); + const asked: number[] = []; + for (const [index, one] of lines.entries()) { + if (one.text.length === 0) { + cut[index] = [piece(0, 0, false, 0)]; + } else { + asked.push(index); + } } - // Asked of as much of the line as the region could still hold. One row is a + // Asked of as much of each line as the region could still hold. One row is a // line the engine measured as fitting, and a line held back from the question // is longer than the answer it would have given, so it is cut. - const reach = windowOf(line.text, room); - const reaching = yield* widths(renderer, size, [line.text.slice(0, reach)]); + const reaches = asked.map((index) => windowOf(lines[index].text, lines[index].room)); + const reaching = yield* widths( + renderer, + size, + asked.map((index, at) => lines[index].text.slice(0, reaches[at])), + SEARCH_WORDS_PER_RENDER, + ); if (!reaching.ok) { return reaching; } - if (reach === line.text.length && reaching.value[0] <= room) { - return Ok([piece(0, line.text.length, false, 0)]); + let open: ReplOpenLine[] = []; + for (const [at, index] of asked.entries()) { + const { text, room } = lines[index]; + if (reaches[at] === text.length && reaching.value[at] <= room) { + cut[index] = [piece(0, text.length, false, 0)]; + continue; + } + open.push({ + index, + text, + room, + // Where this line begins, so its continuations begin there too. Bounded + // well inside the region: an indent that left no room for text would make + // a row that consumes nothing, and this search would not terminate. + hanging: Math.min(text.length - text.trimStart().length, Math.max(0, Math.floor(room / 2))), + cuts: [], + at: 0, + rest: text, + rowRoom: room, + stops: [], + low: 0, + high: 0, + }); } - // Where this line begins, so its continuations begin there too. Bounded well - // inside the region: an indent that left no room for text would make a row - // that consumes nothing, and this loop would not terminate. - const hanging = Math.min( - line.text.length - line.text.trimStart().length, - Math.max(0, Math.floor(room / 2)), - ); - const cuts: ReplRowCut[] = []; - let at = 0; - while (at < line.text.length) { - const rest = line.text.slice(at); - const cut = yield* cutAt(renderer, size, rest, at > 0 ? room - hanging : room); - if (!cut.ok) { - return cut; + while (open.length > 0) { + for (const one of open) { + one.rest = one.text.slice(one.at); + one.rowRoom = one.at > 0 ? one.room - one.hanging : one.room; + // Over the boundaries of the prefix that could still fit, which is what + // keeps every candidate inside one render's budget and keeps this from + // segmenting the whole remainder once per row of a very long line. + one.stops = graphemeStops(one.rest.slice(0, windowOf(one.rest, one.rowRoom))); + one.low = 0; + one.high = one.stops.length - 1; } - cuts.push(piece(at, cut.value, at > 0, at > 0 ? hanging : 0)); - at += cut.value; + // Narrow every row still searching by halves over grapheme boundaries, all + // of their candidates for one step in one batch. + for ( + let searching = open.filter((one) => one.low < one.high); + searching.length > 0; + searching = open.filter((one) => one.low < one.high) + ) { + const middles = searching.map((one) => Math.ceil((one.low + one.high) / 2)); + const measured = yield* widths( + renderer, + size, + searching.map((one, at) => one.rest.slice(0, one.stops[middles[at]])), + SEARCH_WORDS_PER_RENDER, + ); + if (!measured.ok) { + return measured; + } + for (const [at, one] of searching.entries()) { + if (measured.value[at] <= one.rowRoom) { + one.low = middles[at]; + } else { + one.high = middles[at] - 1; + } + } + } + for (const one of open) { + if (one.low === 0) { + // Not one grapheme fits. Clipping it would show the reader a row that + // is not what their file says, so the frame refuses instead. + return Err( + new ReplFitRefusal( + `${one.rowRoom} columns hold no grapheme of ${JSON.stringify( + one.rest.slice(0, one.stops[1] ?? 1), + )}`, + ), + ); + } + const length = rowLength(one.rest, one.stops[one.low]); + one.cuts.push(piece(one.at, length, one.at > 0, one.at > 0 ? one.hanging : 0)); + one.at += length; + if (one.at >= one.text.length) { + cut[one.index] = Object.freeze(one.cuts); + } + } + open = open.filter((one) => one.at < one.text.length); } - return Ok(Object.freeze(cuts)); + return Ok(Object.freeze(cut.map((one) => one ?? []))); } /** One cut, before the pass that measures it. */ @@ -896,14 +1065,14 @@ function piece(from: number, length: number, continuation: boolean, indent: numb } /** - * How many code units of `rest` fit in `room`, cut where a reader would cut. + * How many code units of `rest` one row takes, given the longest prefix the + * engine measured as fitting: cut where a reader would cut. * - * The longest prefix the engine measures as fitting, then back to its last - * whitespace boundary if it has one — whitespace kept in the prefix, because it - * is part of the line and the next row does not begin with somebody else's - * space. A word longer than the region has no such boundary and is broken at - * the longest fitting grapheme instead, which is the only way text wider than - * its pane can be read at all. + * Back to the prefix's last whitespace boundary if it has one — whitespace kept + * in the prefix, because it is part of the line and the next row does not begin + * with somebody else's space. A word longer than the region has no such + * boundary and is broken at the longest fitting grapheme instead, which is the + * only way text wider than its pane can be read at all. * * Backing off is for a prefix, and what is left of a line is not one. Where the * measured prefix reaches the end of the whole remainder, that remainder is the @@ -913,47 +1082,11 @@ function piece(from: number, length: number, continuation: boolean, indent: numb * The end of the *remainder* is what decides it, never the end of the bounded * window the search was asked over, which can stop short of it. */ -function* cutAt( - renderer: ReplRenderer, - size: ReplTerminalSize, - rest: string, - room: number, -): Operation> { - // Over the boundaries of the prefix that could still fit, which is what keeps - // every candidate inside one render's budget and keeps this from segmenting - // the whole remainder once per row of a very long line. - const stops = graphemeStops(rest.slice(0, windowOf(rest, room))); - // Narrow by halves over grapheme boundaries, asking the engine for one batch - // of candidates per round rather than one render per candidate. - let low = 0; - let high = stops.length - 1; - while (low < high) { - const middle = Math.ceil((low + high) / 2); - const measured = yield* widths(renderer, size, [rest.slice(0, stops[middle])]); - if (!measured.ok) { - return measured; - } - if (measured.value[0] <= room) { - low = middle; - } else { - high = middle - 1; - } - } - if (low === 0) { - // Not one grapheme fits. Clipping it would show the reader a row that is - // not what their file says, so the frame refuses instead. - return Err( - new ReplFitRefusal( - `${room} columns hold no grapheme of ${JSON.stringify(rest.slice(0, stops[1] ?? 1))}`, - ), - ); - } - const longest = stops[low]; +function rowLength(rest: string, longest: number): number { if (longest === rest.length) { - return Ok(longest); + return longest; } - const preferred = wordBoundary(rest, longest); - return Ok(preferred === undefined ? longest : preferred); + return wordBoundary(rest, longest) ?? longest; } /** diff --git a/packages/cli/tests/repl-presentation-style.test.ts b/packages/cli/tests/repl-presentation-style.test.ts index c6ad0766f..d2b012a58 100644 --- a/packages/cli/tests/repl-presentation-style.test.ts +++ b/packages/cli/tests/repl-presentation-style.test.ts @@ -3172,7 +3172,9 @@ function sourceOf(lines: number): string { * first frame is warmup; the number returned is what the *second* frame costs * when the only thing that changed is the draft beside the reading. */ -function* frameCost(lines: number): Operation<{ warmup: number; typed: number }> { +function* frameCost( + lines: number, +): Operation<{ warmup: number; warmupTexts: number; typed: number; typedTexts: number }> { return yield* scoped(function* () { const renderer = yield* useCountingRenderer(WIDE); const tree = yield* useReplTree(); @@ -3207,6 +3209,7 @@ function* frameCost(lines: number): Operation<{ warmup: number; typed: number }> throw first.error; } const warmup = renderer.measures(); + const warmupTexts = renderer.texts(); renderer.reset(); const second = yield* commitReplFrame( tree, @@ -3220,7 +3223,7 @@ function* frameCost(lines: number): Operation<{ warmup: number; typed: number }> if (!second.ok) { throw second.error; } - return { warmup, typed: renderer.measures() }; + return { warmup, warmupTexts, typed: renderer.measures(), typedTexts: renderer.texts() }; }); } @@ -3231,9 +3234,11 @@ describe("REPL responsiveness: a complete frame after a keystroke", () => { const long = yield* frameCost(222); // The first frame of each pays for its own reading, and the long one - // pays far more — so these two fixtures really do differ in the work - // the cache is about. - expect(long.warmup).toBeGreaterThan(short.warmup * 4); + // asks the engine about far more text — so these two fixtures really do + // differ in the work the cache is about. Its lines share renders, so the + // difference is in the texts those renders carry. + expect(long.warmupTexts).toBeGreaterThan(short.warmupTexts * 4); + expect(long.warmup).toBeGreaterThan(0); // The second frame changed only the draft. What it costs is the fixed // measurement a complete frame always performs — three structural @@ -3245,7 +3250,8 @@ describe("REPL responsiveness: a complete frame after a keystroke", () => { // fitter on its own. expect(short.typed).toBeGreaterThan(0); // And it is a fraction of what the reading cost to fit. - expect(long.typed).toBeLessThan(long.warmup / 10); + expect(long.typedTexts).toBe(short.typedTexts); + expect(long.typedTexts).toBeLessThan(long.warmupTexts / 10); })); }); diff --git a/packages/cli/tests/repl-source-reading.test.ts b/packages/cli/tests/repl-source-reading.test.ts index 393891781..065c5a4c0 100644 --- a/packages/cli/tests/repl-source-reading.test.ts +++ b/packages/cli/tests/repl-source-reading.test.ts @@ -24,19 +24,20 @@ import { describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { scoped } from "effection"; -import type { Operation } from "effection"; +import { Ok, scoped } from "effection"; +import type { Operation, Result } from "effection"; import { entryReading, NO_READING, READING_STATUSES } from "../src/repl/source-reading.ts"; import type { ReplReadingLine } from "../src/repl/source-reading.ts"; import { createFitStore, + createPlainFitStore, fitPlain, prepareReading, readingRuns, reserveFor, } from "../src/repl/fitting.ts"; -import type { ReplPreparedReading } from "../src/repl/fitting.ts"; +import type { ReplFittedLine, ReplPreparedReading } from "../src/repl/fitting.ts"; import { useCountingRenderer } from "./fixtures/repl/presentation.ts"; import type { ReplCountingRenderer } from "./fixtures/repl/presentation.ts"; import { useReplRenderer } from "../src/repl/renderer.ts"; @@ -1022,9 +1023,8 @@ describe("S5 a reading is measured once for the inputs it was measured for", () const lines = linesOf(longSource(222)); const first = yield* prepareReading(renderer, WIDE, lines, 100, store); expect(first.ok).toBe(true); - // The reading really is the expensive thing: this is the work a - // keystroke used to repeat. - expect(renderer.measures()).toBeGreaterThan(100); + // The reading was measured: this is the work a keystroke used to repeat. + expect(renderer.measures()).toBeGreaterThan(0); // Three more frames, as three keystrokes into the draft would ask for. // The reading is rebuilt from the model every frame, so these are fresh @@ -1362,3 +1362,290 @@ describe("S7 a reading with more words than one measuring render is still measur } })); }); + +/** + * Tier S8 — lines fitted together are cut exactly as each is cut alone. + * + * A reading's lines and a plain list's unheld lines share their measuring + * renders: each search step for every line travels in one batch. The oracle is + * the same public call given one line at a time, which always searches alone — + * so every row, width and refusal below is compared with the real engine's + * sequential answer rather than with numbers written down here. + */ +describe("S8 lines fitted together are cut as each is cut alone", () => { + function fixed(key: string, text: string, depth: number): ReplReadingLine { + return Object.freeze({ + key, + text, + runs: tokenRuns([{ text, token: "source" }]), + rail: "rail-pending" as const, + depth, + badge: undefined, + style: Object.freeze({ role: "source" as const, selected: false, inspected: false }), + }); + } + + /** Everything a row says, so two rows can only be equal by being the same row. */ + function rowsOf(prepared: ReplPreparedReading) { + return prepared.rows.map((one) => ({ + key: one.key, + text: one.text, + runs: one.runs, + line: one.line, + continuation: one.continuation, + indent: one.indent, + depth: one.depth, + width: one.width, + })); + } + + /** Each line prepared on its own, which searches it alone. */ + function* alone( + renderer: ReplRenderer, + size: ReplTerminalSize, + lines: readonly ReplReadingLine[], + room: number, + ): Operation>> { + const rows: ReturnType = []; + for (const one of lines) { + const prepared = yield* prepareReading(renderer, size, [one], room); + if (!prepared.ok) { + return prepared; + } + rows.push(...rowsOf(prepared.value)); + } + return Ok(rows); + } + + /** The texts that make a cut hard: every case the measurement contract names. */ + function corpus(exact: number): readonly string[] { + return [ + "", + "short", + "x".repeat(exact), + "y".repeat(exact + 1), + "mixed widths: ascii, 界界界 wide, é combining, and a closing clause that wraps", + "\tTabbed\tline with runs of spaces that continue past one row", + " indented text whose continuations hang at the column it was written at", + "é".repeat(90), + "👩‍💻🇺🇦✨".repeat(40), + "u".repeat(3000), + "界".repeat(150), + Array.from({ length: 240 }, (_, at) => `w${at}`).join(" "), + ' File "m.py", line 12, in call(*args, **kwargs)', + "repeated line that wraps once the room is narrow enough to need it", + "repeated line that wraps once the room is narrow enough to need it", + " ", + "end", + ]; + } + + it("cuts mixed depths, widths and graphemes as each line alone at several rooms", () => + withRenderer(function* (renderer) { + const reserved = yield* reserveFor(renderer, WIDE, 100); + if (!reserved.ok) { + throw reserved.error; + } + const overhead = 100 - reserved.value.content; + // Rooms from the measured reservation: the content widths named are what + // the text gets, the deepest line losing six of them to its indent. + for (const content of [80, 33, 12, 8]) { + const lines = corpus(content).map((text, at) => fixed(`c${at}`, text, at % 4)); + const room = overhead + content; + const together = yield* prepareReading(renderer, WIDE, lines, room); + const oracle = yield* alone(renderer, WIDE, lines, room); + if (!together.ok || !oracle.ok) { + throw new Error(`content ${content} refused`); + } + expect(rowsOf(together.value)).toEqual(oracle.value); + const whole = recovered(together.value); + for (const one of lines) { + expect([content, one.key, whole.get(one.key)]).toEqual([content, one.key, one.text]); + } + } + })); + + it("cuts a plain list's unheld lines as each alone, and keeps what the store holds", () => + withRenderer(function* (renderer) { + // Down to five cells, which every grapheme here fits: a joined emoji + // sequence measures as wide as its parts. + for (const room of [80, 33, 12, 5]) { + const lines = corpus(room).map((text, at) => ({ key: `p${at}`, text })); + const store = createPlainFitStore(); + // Half of them held already, so the batch is only what the store lacks. + const warm = yield* fitPlain( + renderer, + WIDE, + lines.filter((_, at) => at % 2 === 0), + room, + store, + ); + expect(warm.ok).toBe(true); + const together = yield* fitPlain(renderer, WIDE, lines, room, store); + const oracle: ReplFittedLine[] = []; + for (const one of lines) { + const single = yield* fitPlain(renderer, WIDE, [one], room); + if (!single.ok) { + throw single.error; + } + oracle.push(...single.value); + } + if (!together.ok) { + throw together.error; + } + expect([room, together.value]).toEqual([room, oracle]); + } + })); + + it("reports the refusal the first refusing line reaches, wherever the batch found one", () => + withRenderer(function* (renderer) { + const reserved = yield* reserveFor(renderer, WIDE, 100); + if (!reserved.ok) { + throw reserved.error; + } + // Three cells of content; a depth-1 line keeps one, which holds no wide + // grapheme. The first line refuses on its third row, the second at once: + // a search over both reaches the second line's refusal first. + const room = 100 - reserved.value.content + 3; + const lines = [fixed("first", "ab界", 1), fixed("second", "🙂x", 1)]; + const together = yield* prepareReading(renderer, WIDE, lines, room); + const first = yield* prepareReading(renderer, WIDE, [lines[0]], room); + expect(together.ok).toBe(false); + expect(first.ok).toBe(false); + if (together.ok || first.ok) { + return; + } + expect(together.error.message).toBe(first.error.message); + expect(together.error.message).toContain("界"); + + // A plain list keeps what it had already cut before the refusing line, + // exactly as cutting the lines in order does. + const store = createPlainFitStore(); + const plain = yield* fitPlain( + renderer, + WIDE, + [ + { key: "fits", text: "ok" }, + { key: "first", text: "ab界" }, + { key: "second", text: "🙂x" }, + ], + 1, + store, + ); + const firstPlain = yield* fitPlain(renderer, WIDE, [{ key: "first", text: "ab界" }], 1); + expect(plain.ok).toBe(false); + expect(firstPlain.ok).toBe(false); + if (plain.ok || firstPlain.ok) { + return; + } + expect(plain.error.message).toBe(firstPlain.error.message); + expect(store.held("ok", 1, WIDE)).not.toBe(undefined); + expect(store.held("ab界", 1, WIDE)).toBe(undefined); + })); + + it("cuts one line at a time where a room reaches the measuring columns", () => + scoped(function* () { + // A text wider than the engine's columns is answered from the layout it + // shares: alone a breakable one reports the columns, beside a wider + // unbreakable one its own width. So these rooms must not share renders. + // The engine is opened at these columns, as the REPL's own is. + const size: ReplTerminalSize = { columns: 60, rows: 20 }; + const renderer = yield* useReplRenderer(size); + const texts = [ + Array.from({ length: 30 }, (_, at) => `word${at}`).join(" "), + "z".repeat(150), + "a breakable line of ordinary prose that is a good deal wider than sixty cells in all", + "short", + ]; + for (const room of [60, 61, 100]) { + const lines = texts.map((text, at) => ({ key: `n${at}`, text })); + const together = yield* fitPlain(renderer, size, lines, room); + const oracle: ReplFittedLine[] = []; + for (const one of lines) { + const single = yield* fitPlain(renderer, size, [one], room); + expect(single.ok).toBe(true); + if (single.ok) { + oracle.push(...single.value); + } + } + expect(together.ok).toBe(true); + if (together.ok) { + expect([room, together.value]).toEqual([room, oracle]); + } + } + const reserved = yield* reserveFor(renderer, size, 100); + if (!reserved.ok) { + throw reserved.error; + } + // A reading's final pass measures every finished row in one batch, as it + // always has, so at these rooms a row's reported width depends on its + // neighbours. The cut is what the search decides, and what this pins. + const cutsOf = (rows: ReturnType) => rows.map(({ width: _, ...cut }) => cut); + const overhead = 100 - reserved.value.content; + for (const room of [overhead + 60, overhead + 80]) { + const lines = texts.map((text, at) => fixed(`r${at}`, text, 0)); + const together = yield* prepareReading(renderer, size, lines, room); + const oracle = yield* alone(renderer, size, lines, room); + expect([room, together.ok, oracle.ok]).toEqual([room, true, true]); + if (together.ok && oracle.ok) { + expect([room, cutsOf(rowsOf(together.value))]).toEqual([room, cutsOf(oracle.value)]); + } + } + })); + + it("fits a cold 1,000-line wrapping entry in a bounded number of renders", () => + withCounting(function* (renderer) { + const text = Array.from( + { length: 1000 }, + (_, at) => ` File "m.py", line ${at}, in call(*args, **kwargs)`, + ).join("\n"); + const reading = entryReading({ + entry: entryOf(text, { terminal: settled("ok", text), settled: true }), + lifecycle: NO_LIFECYCLE, + live: "", + inspected: false, + }); + const lines = [...reading.output.lines, ...reading.source.lines]; + expect(lines.length).toBeGreaterThanOrEqual(2000); + renderer.reset(); + // The reading pane's room at 160x36, where every one of these lines wraps. + const prepared = yield* prepareReading(renderer, WIDE, lines, 49); + if (!prepared.ok) { + throw prepared.error; + } + expect(prepared.value.rows.length).toBeGreaterThanOrEqual(lines.length * 2); + // Searched alone, each line costs a render per search step: thousands. + // Together the count follows the search depth and the batch limits. + expect(renderer.measures()).toBeLessThan(150); + const oracle = yield* alone(renderer, WIDE, lines, 49); + if (!oracle.ok) { + throw oracle.error; + } + expect(rowsOf(prepared.value)).toEqual(oracle.value); + })); + + it("keeps the batched search within what a small terminal's engine holds", () => + scoped(function* () { + // An engine opened at 80x24 holds fewer measured parts than one at + // 160x36, so a search step carrying every line of a long reading can + // overrun it, be refused, and leave the reading to be cut line by line. + const size: ReplTerminalSize = { columns: 80, rows: 24 }; + const renderer = yield* useCountingRenderer(size); + const lines = Array.from({ length: 1000 }, (_, at) => + fixed(`t${at}`, ` File "m.py", line ${at}, in call(*args, **kwargs)`, 0), + ); + renderer.reset(); + const prepared = yield* prepareReading(renderer, size, lines, 49); + if (!prepared.ok) { + throw prepared.error; + } + expect(prepared.value.rows.length).toBeGreaterThanOrEqual(lines.length * 2); + // Cut line by line this reading takes about twelve thousand renders. + expect(renderer.measures()).toBeLessThan(150); + const oracle = yield* alone(renderer, size, lines, 49); + if (!oracle.ok) { + throw oracle.error; + } + expect(rowsOf(prepared.value)).toEqual(oracle.value); + })); +});