mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Count a stripping run only between its non-blank leaves, so the trim takes two passes over alternating code and emphasized blanks
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
|
||||
import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import { runsSharedWithNext } from './inline-runs.ts'
|
||||
|
||||
const em: AdfMark = { type: 'em' }
|
||||
const strong: AdfMark = { type: 'strong' }
|
||||
const marked = (text: string, ...marks: AdfMark[]): AdfNode => ({ marks, text, type: 'text' })
|
||||
|
||||
test('counts a run crossing a code span to its next holder where the line is spanned, and none where it is parted', () => {
|
||||
const line = [marked('Run ', strong), marked('npm ci', { type: 'code' }), marked(' first', strong)]
|
||||
assert.deepEqual(runsSharedWithNext(line, 'spanned'), [1, 1, 0])
|
||||
assert.deepEqual(runsSharedWithNext(line, 'parted'), [0, 0, 0])
|
||||
})
|
||||
|
||||
test('counts every nested run crossing a hard break, and none crossing a hard break holding an empty marks', () => {
|
||||
assert.deepEqual(runsSharedWithNext([marked('a', em, strong), { type: 'hardBreak' }, marked('b', em, strong)], 'spanned'), [2, 2, 0])
|
||||
assert.deepEqual(runsSharedWithNext([marked('a', em), { marks: [], type: 'hardBreak' }, marked('b', em)], 'spanned'), [0, 0, 0])
|
||||
})
|
||||
@@ -55,19 +55,3 @@ function runCrosses(node: AdfNode, mark: AdfMark): boolean {
|
||||
const kind = markSpelling(mark.type)?.kind
|
||||
return (kind === 'emphasis' || kind === 'link') && !takesMark(node, mark.type) && node.marks?.length !== 0
|
||||
}
|
||||
|
||||
// For each leaf but the last, how many runs hold both it and the next leaf, grouped as the writers group them, so the reducer trims edge whitespace by runs they spell.
|
||||
export function runsSharedWithNext(leaves: readonly AdfNode[], grouping: RunGrouping): number[] {
|
||||
const shared = leaves.map(() => 0)
|
||||
countSharedRuns(leaves.map((node) => ({ carried: false, node, outer: 0 })), 0, grouping, shared)
|
||||
return shared
|
||||
}
|
||||
|
||||
// Recurses once per mark of a leaf, and the reducer's leaves hold one mark of each kept type.
|
||||
function countSharedRuns(nodes: readonly RunNode[], firstIndex: number, grouping: RunGrouping, shared: number[]): void {
|
||||
for (const run of inlineRuns(nodes, firstIndex, grouping)) {
|
||||
if (run.kind === 'plain') continue
|
||||
for (let index = run.index; index < run.index + run.nodes.length - 1; index += 1) shared[index] = (shared[index] ?? 0) + 1
|
||||
countSharedRuns(run.nodes, run.index, grouping, shared)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ import assert from 'node:assert/strict'
|
||||
import fc from 'fast-check'
|
||||
import test from 'node:test'
|
||||
|
||||
import type { AdfNode } from '../../adf/document.ts'
|
||||
import type { AdfMark, AdfNode } from '../../adf/document.ts'
|
||||
import type { LineContainer } from '../line-container.ts'
|
||||
import type { PortableLineFallback } from '../emit/inline-line.ts'
|
||||
import type { RunGrouping, RunNode } from '../emit/inline-runs.ts'
|
||||
import { inlineRuns } from '../emit/inline-runs.ts'
|
||||
import { lineContainer, markedLine, propertyRuns } from '../../conformance/property-harness.ts'
|
||||
import { portableJudgement, portableLeaves, reduceInline, withoutFallback } from './inline-reduction.ts'
|
||||
import { portableJudgement, portableLeaves, reduceInline, trimmedEdges, trimmingRuns, trimPass, withoutFallback } from './inline-reduction.ts'
|
||||
import { success, type Result } from '../../result.ts'
|
||||
|
||||
test('reduces a line alike dropping every run a pass may or the first alone', () => {
|
||||
@@ -58,6 +60,88 @@ test('refuses an unspellable-run fallback that takes no mark', () => {
|
||||
assert.equal(refused.ok ? 'kept' : refused.error.code, 'unsupported-node-shape')
|
||||
})
|
||||
|
||||
const groupings: readonly RunGrouping[] = ['parted', 'spanned']
|
||||
const link: AdfMark = { attrs: { href: 'u' }, type: 'link' }
|
||||
const marked = (text: string, ...marks: AdfMark[]): AdfNode => ({ marks, text, type: 'text' })
|
||||
const trimMarks = fc.oneof(
|
||||
fc.uniqueArray(fc.constantFrom<AdfMark>({ type: 'backgroundColor' }, { type: 'em' }, link, { attrs: { href: 'v' }, type: 'link' }, { type: 'strike' }, { type: 'strong' }), { maxLength: 3, selector: (held) => held.type }),
|
||||
fc.constantFrom<AdfMark[]>([{ type: 'code' }], [link, { type: 'code' }]),
|
||||
)
|
||||
const trimLeaf = fc.oneof(
|
||||
{ arbitrary: fc.record({ marks: trimMarks, text: fc.string({ maxLength: 3, minLength: 1, unit: fc.constantFrom(' ', ' ', '\t', 'x') }) }).map((held): AdfNode => ({ ...held, type: 'text' })), weight: 6 },
|
||||
{ arbitrary: fc.constant<AdfNode>({ type: 'hardBreak' }), weight: 1 },
|
||||
)
|
||||
const trimLine = fc.oneof(
|
||||
fc.array(trimLeaf, { maxLength: 12, minLength: 1 }),
|
||||
fc.tuple(fc.array(trimLeaf, { maxLength: 2 }), fc.array(trimLeaf, { maxLength: 3, minLength: 1 }), fc.integer({ max: 8, min: 2 }), fc.array(trimLeaf, { maxLength: 2 })).map(([head, unit, repeats, tail]) => [...head, ...Array.from({ length: repeats }, () => unit).flat(), ...tail]),
|
||||
)
|
||||
|
||||
for (const grouping of groupings) {
|
||||
test(`trims a line ${grouping} as peeling each run's end leaf by leaf does, in no more passes`, () => {
|
||||
fc.assert(
|
||||
fc.property(trimLine, (line) => {
|
||||
const peeled = trimmedWith(line, (leaves) => runsSharedWithNext(leaves, grouping))
|
||||
const trimmed = trimmedWith(line, (leaves) => trimmingRuns(leaves, grouping))
|
||||
assert.deepEqual(trimmedEdges(line, grouping), peeled.line)
|
||||
assert.ok(trimmed.passes <= peeled.passes, `${trimmed.passes} passes against ${peeled.passes}`)
|
||||
}),
|
||||
propertyRuns(3000),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
test('counts a run crossing a code span to its next holder where the line is spanned, and none where it is parted', () => {
|
||||
const line = [marked('Run ', { type: 'strong' }), marked('npm ci', { type: 'code' }), marked(' first', { type: 'strong' })]
|
||||
assert.deepEqual(trimmingRuns(line, 'spanned'), [1, 1, 0])
|
||||
assert.deepEqual(trimmingRuns(line, 'parted'), [0, 0, 0])
|
||||
})
|
||||
|
||||
test('counts every nested run crossing a hard break, and none crossing a hard break holding an empty marks', () => {
|
||||
assert.deepEqual(trimmingRuns([marked('a', { type: 'em' }, { type: 'strong' }), { type: 'hardBreak' }, marked('b', { type: 'em' }, { type: 'strong' })], 'spanned'), [2, 2, 0])
|
||||
assert.deepEqual(trimmingRuns([marked('a', { type: 'em' }), { marks: [], type: 'hardBreak' }, marked('b', { type: 'em' })], 'spanned'), [0, 0, 0])
|
||||
})
|
||||
|
||||
test('counts an emphasis run from its first to its last leaf holding more than spaces and tabs, and a link run to its last leaf', () => {
|
||||
const line = [marked(' \t', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked('a', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked('b', link, { type: 'em' }), marked('x', link, { type: 'code' }), marked(' ', link, { type: 'em' })]
|
||||
assert.deepEqual(trimmingRuns(line, 'spanned'), [1, 1, 2, 2, 1, 1, 0])
|
||||
})
|
||||
|
||||
const alternatingLines: readonly [string, AdfNode, AdfNode][] = [
|
||||
['a code space beside an emphasized space', marked(' ', { type: 'code' }), marked(' ', { type: 'em' })],
|
||||
['code x beside an emphasized space', marked('x', { type: 'code' }), marked(' ', { type: 'em' })],
|
||||
['linked code x beside a linked emphasized space', marked('x', link, { type: 'code' }), marked(' ', link, { type: 'em' })],
|
||||
]
|
||||
for (const [name, code, emphasized] of alternatingLines) {
|
||||
test(`trims a line of ${name}, 1 000 leaves long, in the passes its 6-leaf form takes`, () => {
|
||||
const alternating = (pairs: number) => Array.from({ length: pairs }, () => [code, emphasized]).flat()
|
||||
assert.equal(trimmedWith(alternating(500), (leaves) => trimmingRuns(leaves, 'spanned')).passes, trimmedWith(alternating(3), (leaves) => trimmingRuns(leaves, 'spanned')).passes)
|
||||
})
|
||||
}
|
||||
|
||||
// The trim's passes and line where each pass judges every leaf against `shared`'s counts.
|
||||
function trimmedWith(leaves: readonly AdfNode[], shared: (leaves: readonly AdfNode[]) => readonly number[]): { line: AdfNode[]; passes: number } {
|
||||
for (let current = leaves, passes = 1; ; passes += 1) {
|
||||
const pass = trimPass(current, shared)
|
||||
if (pass.settled) return { line: pass.line, passes }
|
||||
current = pass.line
|
||||
}
|
||||
}
|
||||
|
||||
// The reference counts: every run reaches its last leaf holding its mark, so the trim peels a run's end leaves one pass each.
|
||||
function runsSharedWithNext(leaves: readonly AdfNode[], grouping: RunGrouping): number[] {
|
||||
const shared = leaves.map(() => 0)
|
||||
countSharedRuns(leaves.map((node) => ({ carried: false, node, outer: 0 })), 0, grouping, shared)
|
||||
return shared
|
||||
}
|
||||
|
||||
function countSharedRuns(nodes: readonly RunNode[], firstIndex: number, grouping: RunGrouping, shared: number[]): void {
|
||||
for (const run of inlineRuns(nodes, firstIndex, grouping)) {
|
||||
if (run.kind === 'plain') continue
|
||||
for (let index = run.index; index < run.index + run.nodes.length - 1; index += 1) shared[index] = (shared[index] ?? 0) + 1
|
||||
countSharedRuns(run.nodes, run.index, grouping, shared)
|
||||
}
|
||||
}
|
||||
|
||||
// Judges each pass spanned, else parted, dropping only the first run it finds, with every misflanked highlight, and takes no cap.
|
||||
function firstRunLine(nodes: readonly AdfNode[], container: LineContainer): Result<AdfNode[]> {
|
||||
const leaves = portableLeaves(nodes, container, { depth: 0, path: [], sources: new Map() })
|
||||
|
||||
@@ -7,7 +7,7 @@ import { codeTextMarkTypes } from '../../adf/inline-nodes.ts'
|
||||
import { failure, success, type ConvertErrorPath, type Result } from '../../result.ts'
|
||||
import { identicalMark, mergeAdjacentText, nodeAttrs, nodeContent, nodeMarks } from '../../adf/document.ts'
|
||||
import { imageAside } from '../image-aside.ts'
|
||||
import { insideRun, runsSharedWithNext } from '../emit/inline-runs.ts'
|
||||
import { inlineRuns, insideRun, type RunNode } from '../emit/inline-runs.ts'
|
||||
import { isEmphasis } from '../mark-spellings.ts'
|
||||
import { joinsWhenEditorNormal, sourceIndex, type SourceIndexes } from './editor-normal.ts'
|
||||
import { largestNesting } from '../../nesting.ts'
|
||||
@@ -21,8 +21,6 @@ type Beside = { lineEdge: boolean; sharedRuns: number }
|
||||
// `keep`: how many of the leaf's marks its edge whitespace keeps; `drop`: the whitespace goes, at a line edge.
|
||||
type EdgeOutcome = { kind: 'drop' } | { depth: number; kind: 'keep' }
|
||||
|
||||
type PlacedLeaf = { after: Beside; before: Beside; leaf: AdfNode }
|
||||
|
||||
// Where a reduction stands: its path, counted in the caller's document by `sources`, and its level as the emitter counts it.
|
||||
export type ReductionPlace = { depth: number; path: ConvertErrorPath; sources: SourceIndexes }
|
||||
|
||||
@@ -187,23 +185,30 @@ function withMarks(leaf: AdfNode, marks: readonly AdfMark[]): AdfNode {
|
||||
export function trimmedEdges(leaves: readonly AdfNode[], grouping: RunGrouping): AdfNode[] {
|
||||
// Terminates: a pass that changes a leaf drops whitespace from the leaf's edge or takes a mark off that whitespace, and merging adds no whitespace or mark.
|
||||
for (let current = leaves; ; ) {
|
||||
const merged = withoutEdgeBreaks(mergeAdjacentText(current, joinsWhenEditorNormal))
|
||||
let changed = false
|
||||
const trimmed: AdfNode[] = []
|
||||
for (const { after, before, leaf } of placedLeaves(merged, grouping)) {
|
||||
const edges = leafEdges(leaf, before, after)
|
||||
if (edges === undefined) {
|
||||
trimmed.push(leaf)
|
||||
continue
|
||||
}
|
||||
changed = true
|
||||
for (const edge of edges) trimmed.push(edge)
|
||||
}
|
||||
if (!changed) return merged
|
||||
current = trimmed
|
||||
const pass = trimPass(current, (line) => trimmingRuns(line, grouping))
|
||||
if (pass.settled) return pass.line
|
||||
current = pass.line
|
||||
}
|
||||
}
|
||||
|
||||
// Judges every leaf against one snapshot of `shared`'s counts; `settled`, with the merged line, where no leaf changes.
|
||||
export function trimPass(leaves: readonly AdfNode[], shared: (line: readonly AdfNode[]) => readonly number[]): { line: AdfNode[]; settled: boolean } {
|
||||
const merged = withoutEdgeBreaks(mergeAdjacentText(leaves, joinsWhenEditorNormal))
|
||||
const counts = shared(merged)
|
||||
let changed = false
|
||||
const trimmed: AdfNode[] = []
|
||||
for (const [index, leaf] of merged.entries()) {
|
||||
const edges = leafEdges(leaf, beside(merged[index - 1], counts[index - 1]), beside(merged[index + 1], counts[index]))
|
||||
if (edges === undefined) {
|
||||
trimmed.push(leaf)
|
||||
continue
|
||||
}
|
||||
changed = true
|
||||
for (const edge of edges) trimmed.push(edge)
|
||||
}
|
||||
return changed ? { line: trimmed, settled: false } : { line: merged, settled: true }
|
||||
}
|
||||
|
||||
function withoutEdgeBreaks(leaves: readonly AdfNode[]): AdfNode[] {
|
||||
let first = 0
|
||||
let last = leaves.length - 1
|
||||
@@ -212,11 +217,6 @@ function withoutEdgeBreaks(leaves: readonly AdfNode[]): AdfNode[] {
|
||||
return leaves.slice(first, last + 1)
|
||||
}
|
||||
|
||||
function placedLeaves(leaves: readonly AdfNode[], grouping: RunGrouping): PlacedLeaf[] {
|
||||
const shared = runsSharedWithNext(leaves, grouping)
|
||||
return leaves.map((leaf, index) => ({ after: beside(leaves[index + 1], shared[index]), before: beside(leaves[index - 1], shared[index - 1]), leaf }))
|
||||
}
|
||||
|
||||
function beside(neighbour: AdfNode | undefined, sharedRuns = 0): Beside {
|
||||
return { lineEdge: neighbour === undefined || neighbour.type === 'hardBreak', sharedRuns }
|
||||
}
|
||||
@@ -250,6 +250,27 @@ function edgeOutcome(marks: readonly AdfMark[], { lineEdge, sharedRuns }: Beside
|
||||
return depth === 0 && lineEdge ? { kind: 'drop' } : { depth, kind: 'keep' }
|
||||
}
|
||||
|
||||
// For each leaf but the last, how many of `grouping`'s runs hold it and the next leaf; an edge-stripping mark's run spans only its leaves holding more than spaces and tabs, as the trim strips the mark off blank leaves past them, else one pass each.
|
||||
export function trimmingRuns(leaves: readonly AdfNode[], grouping: RunGrouping): number[] {
|
||||
const shared = leaves.map(() => 0)
|
||||
countTrimmingRuns(leaves.map((node) => ({ carried: false, node, outer: 0 })), 0, grouping, shared)
|
||||
return shared
|
||||
}
|
||||
|
||||
// Recurses once per mark of a leaf, and the reducer's leaves hold one mark of each kept type.
|
||||
function countTrimmingRuns(nodes: readonly RunNode[], firstIndex: number, grouping: RunGrouping, shared: number[]): void {
|
||||
for (const run of inlineRuns(nodes, firstIndex, grouping)) {
|
||||
if (run.kind === 'plain') continue
|
||||
const holds = edgeStrippingMarks.includes(run.mark.type) ? ({ node }: RunNode) => nodeMarks(node).some((mark) => identicalMark(mark, run.mark)) && /[^ \t]/.test(node.text ?? '') : () => true
|
||||
const holders = run.nodes.flatMap((runNode, offset) => (holds(runNode) ? [offset] : []))
|
||||
const first = holders[0]
|
||||
const last = holders.at(-1)
|
||||
if (first === undefined || last === undefined) continue
|
||||
for (let index = first; index < last; index += 1) shared[run.index + index] = (shared[run.index + index] ?? 0) + 1
|
||||
countTrimmingRuns(run.nodes.slice(first, last + 1), run.index + first, grouping, shared)
|
||||
}
|
||||
}
|
||||
|
||||
// Trims the line by `grouping`'s runs and judges it; a fallback's indexes name the trimmed line.
|
||||
export function portableJudgement(leaves: readonly AdfNode[], grouping: RunGrouping, container: LineContainer, path: ConvertErrorPath): Result<LineJudgement<readonly AdfNode[], PortableLineFallback, AdfNode[]>> {
|
||||
const line = trimmedEdges(leaves, grouping)
|
||||
|
||||
Reference in New Issue
Block a user