use std::borrow::Cow;
use arrayvec::ArrayVec;
use bumpalo::collections::{String as BumpString, Vec as BumpVec};
use comemo::Track;
use ecow::EcoString;
use once_cell::unsync::Lazy;
use crate::diag::{bail, At, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
Content, Context, ContextElem, Element, NativeElement, Recipe, RecipeIndex, Selector,
SequenceElem, Show, ShowSet, Style, StyleChain, StyleVec, StyledElem, Styles,
Synthesize, Transformation,
};
use crate::introspection::{Locatable, SplitLocator, Tag, TagElem};
use crate::layout::{
AlignElem, BoxElem, HElem, InlineElem, PageElem, PagebreakElem, VElem,
};
use crate::math::{EquationElem, LayoutMath};
use crate::model::{
CiteElem, CiteGroup, DocumentElem, DocumentInfo, EnumElem, ListElem, ListItemLike,
ListLike, ParElem, ParbreakElem, TermsElem,
};
use crate::syntax::Span;
use crate::text::{LinebreakElem, SmartQuoteElem, SpaceElem, TextElem};
use crate::utils::{SliceExt, SmallBitSet};
pub type Pair<'a> = (&'a Content, StyleChain<'a>);
#[typst_macros::time(name = "realize")]
pub fn realize<'a>(
kind: RealizationKind,
engine: &mut Engine,
locator: &mut SplitLocator,
arenas: &'a Arenas,
content: &'a Content,
styles: StyleChain<'a>,
) -> SourceResult<Vec<Pair<'a>>> {
let mut s = State {
engine,
locator,
arenas,
rules: match kind {
RealizationKind::Root(_) | RealizationKind::Container => NORMAL_RULES,
RealizationKind::Math => MATH_RULES,
},
sink: vec![],
groupings: ArrayVec::new(),
outside: matches!(kind, RealizationKind::Root(_)),
may_attach: false,
kind,
};
visit(&mut s, content, styles)?;
finish(&mut s)?;
Ok(s.sink)
}
pub enum RealizationKind<'a> {
Root(&'a mut DocumentInfo),
Container,
Math,
}
#[derive(Default)]
pub struct Arenas {
pub content: typed_arena::Arena<Content>,
pub styles: typed_arena::Arena<Styles>,
pub bump: bumpalo::Bump,
}
struct State<'a, 'x, 'y, 'z> {
kind: RealizationKind<'x>,
engine: &'x mut Engine<'y>,
locator: &'x mut SplitLocator<'z>,
arenas: &'a Arenas,
sink: Vec<Pair<'a>>,
rules: &'x [&'x GroupingRule],
groupings: ArrayVec<Grouping<'x>, MAX_GROUP_NESTING>,
outside: bool,
may_attach: bool,
}
struct GroupingRule {
priority: u8,
tags: bool,
trigger: fn(Element) -> bool,
inner: fn(Element) -> bool,
interrupt: fn(Element) -> bool,
finish: fn(Grouped) -> SourceResult<()>,
}
struct Grouping<'a> {
start: usize,
rule: &'a GroupingRule,
}
struct Grouped<'a, 'x, 'y, 'z, 's> {
s: &'s mut State<'a, 'x, 'y, 'z>,
start: usize,
}
struct Verdict<'a> {
prepared: bool,
map: Styles,
step: Option<ShowStep<'a>>,
}
enum ShowStep<'a> {
Recipe(&'a Recipe, RecipeIndex),
Builtin,
}
struct RegexMatch<'a> {
offset: usize,
text: EcoString,
styles: StyleChain<'a>,
id: RecipeIndex,
recipe: &'a Recipe,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum SpaceState {
Destructive,
Supportive,
Space(usize),
}
impl<'a> State<'a, '_, '_, '_> {
fn store(&self, content: Content) -> &'a Content {
self.arenas.content.alloc(content)
}
fn store_slice(&self, pairs: &[Pair<'a>]) -> BumpVec<'a, Pair<'a>> {
let mut vec = BumpVec::new_in(&self.arenas.bump);
vec.extend_from_slice_copy(pairs);
vec
}
}
impl<'a, 'x, 'y, 'z, 's> Grouped<'a, 'x, 'y, 'z, 's> {
fn get(&self) -> &[Pair<'a>] {
&self.s.sink[self.start..]
}
fn get_mut(&mut self) -> (&mut Vec<Pair<'a>>, usize) {
(&mut self.s.sink, self.start)
}
fn end(self) -> &'s mut State<'a, 'x, 'y, 'z> {
self.s.sink.truncate(self.start);
self.s
}
}
fn visit<'a>(
s: &mut State<'a, '_, '_, '_>,
content: &'a Content,
styles: StyleChain<'a>,
) -> SourceResult<()> {
if content.is::<TagElem>() {
s.sink.push((content, styles));
return Ok(());
}
if visit_math_rules(s, content, styles)? {
return Ok(());
}
if visit_show_rules(s, content, styles)? {
return Ok(());
}
if let Some(sequence) = content.to_packed::<SequenceElem>() {
for elem in &sequence.children {
visit(s, elem, styles)?;
}
return Ok(());
}
if let Some(styled) = content.to_packed::<StyledElem>() {
return visit_styled(s, &styled.child, Cow::Borrowed(&styled.styles), styles);
}
if visit_grouping_rules(s, content, styles)? {
return Ok(());
}
if visit_filter_rules(s, content, styles)? {
return Ok(());
}
s.sink.push((content, styles));
Ok(())
}
fn visit_math_rules<'a>(
s: &mut State<'a, '_, '_, '_>,
content: &'a Content,
styles: StyleChain<'a>,
) -> SourceResult<bool> {
if let RealizationKind::Math = s.kind {
if let Some(elem) = content.to_packed::<EquationElem>() {
visit(s, &elem.body, styles)?;
return Ok(true);
}
if let Some(elem) = content.to_packed::<TextElem>() {
if let Some(m) = find_regex_match_in_str(&elem.text, styles) {
visit_regex_match(s, &[(content, styles)], m)?;
return Ok(true);
}
}
} else {
if content.can::<dyn LayoutMath>() && !content.is::<EquationElem>() {
let eq = EquationElem::new(content.clone()).pack().spanned(content.span());
visit(s, s.store(eq), styles)?;
return Ok(true);
}
}
Ok(false)
}
fn visit_show_rules<'a>(
s: &mut State<'a, '_, '_, '_>,
content: &'a Content,
styles: StyleChain<'a>,
) -> SourceResult<bool> {
let Some(Verdict { prepared, mut map, step }) = verdict(s.engine, content, styles)
else {
return Ok(false);
};
let mut output = Cow::Borrowed(content);
let mut tags = None;
if !prepared {
tags = prepare(s.engine, s.locator, output.to_mut(), &mut map, styles)?;
}
if let Some(step) = step {
let chained = styles.chain(&map);
let result = match step {
ShowStep::Recipe(recipe, guard) => {
let context = Context::new(output.location(), Some(chained));
recipe.apply(
s.engine,
context.track(),
output.into_owned().guarded(guard),
)
}
ShowStep::Builtin => {
output.with::<dyn Show>().unwrap().show(s.engine, chained)
}
};
output = Cow::Owned(s.engine.delay(result));
}
let realized = match output {
Cow::Borrowed(realized) => realized,
Cow::Owned(realized) => s.store(realized),
};
let (start, end) = tags.unzip();
if let Some(tag) = start {
visit(s, s.store(TagElem::packed(tag)), styles)?;
}
let prev_outside = s.outside;
s.outside &= content.is::<ContextElem>();
s.engine.route.increase();
s.engine.route.check_show_depth().at(content.span())?;
visit_styled(s, realized, Cow::Owned(map), styles)?;
s.outside = prev_outside;
s.engine.route.decrease();
if let Some(tag) = end {
visit(s, s.store(TagElem::packed(tag)), styles)?;
}
Ok(true)
}
fn verdict<'a>(
engine: &mut Engine,
target: &'a Content,
styles: StyleChain<'a>,
) -> Option<Verdict<'a>> {
let prepared = target.is_prepared();
let mut map = Styles::new();
let mut step = None;
let mut target = target;
let mut slot;
if !prepared && target.can::<dyn Synthesize>() {
slot = target.clone();
slot.with_mut::<dyn Synthesize>()
.unwrap()
.synthesize(engine, styles)
.ok();
target = &slot;
}
let depth = Lazy::new(|| styles.recipes().count());
for (r, recipe) in styles.recipes().enumerate() {
if !recipe
.selector()
.is_some_and(|selector| selector.matches(target, Some(styles)))
{
continue;
}
if let Transformation::Style(transform) = recipe.transform() {
if !prepared {
map.apply(transform.clone());
}
continue;
}
if step.is_some() {
continue;
}
let index = RecipeIndex(*depth - r);
if target.is_guarded(index) {
continue;
}
step = Some(ShowStep::Recipe(recipe, index));
if prepared {
break;
}
}
if step.is_none() && target.can::<dyn Show>() {
step = Some(ShowStep::Builtin);
}
if step.is_none()
&& map.is_empty()
&& (prepared || {
target.label().is_none()
&& target.location().is_none()
&& !target.can::<dyn ShowSet>()
&& !target.can::<dyn Locatable>()
&& !target.can::<dyn Synthesize>()
})
{
return None;
}
Some(Verdict { prepared, map, step })
}
fn prepare(
engine: &mut Engine,
locator: &mut SplitLocator,
target: &mut Content,
map: &mut Styles,
styles: StyleChain,
) -> SourceResult<Option<(Tag, Tag)>> {
let key = crate::utils::hash128(&target);
if target.location().is_none()
&& (target.can::<dyn Locatable>() || target.label().is_some())
{
let loc = locator.next_location(engine.introspector, key);
target.set_location(loc);
}
if let Some(show_settable) = target.with::<dyn ShowSet>() {
map.apply(show_settable.show_set(styles));
}
if let Some(synthesizable) = target.with_mut::<dyn Synthesize>() {
synthesizable.synthesize(engine, styles.chain(map))?;
}
target.materialize(styles.chain(map));
let tags = target
.location()
.map(|loc| (Tag::Start(target.clone()), Tag::End(loc, key)));
target.mark_prepared();
Ok(tags)
}
fn visit_styled<'a>(
s: &mut State<'a, '_, '_, '_>,
content: &'a Content,
mut local: Cow<'a, Styles>,
outer: StyleChain<'a>,
) -> SourceResult<()> {
if local.is_empty() {
return visit(s, content, outer);
}
let mut pagebreak = false;
for style in local.iter() {
let Some(elem) = style.element() else { continue };
if elem == DocumentElem::elem() {
let RealizationKind::Root(info) = &mut s.kind else {
let span = style.span();
bail!(span, "document set rules are not allowed inside of containers");
};
info.populate(&local);
} else if elem == PageElem::elem() {
let RealizationKind::Root(_) = s.kind else {
let span = style.span();
bail!(span, "page configuration is not allowed inside of containers");
};
pagebreak = true;
s.outside = true;
}
}
if s.outside {
local = Cow::Owned(local.into_owned().outside());
}
let outer = s.arenas.bump.alloc(outer);
let local = match local {
Cow::Borrowed(map) => map,
Cow::Owned(owned) => &*s.arenas.styles.alloc(owned),
};
if pagebreak {
let relevant = local
.as_slice()
.trim_end_matches(|style| style.element() != Some(PageElem::elem()));
visit(s, PagebreakElem::shared_weak(), outer.chain(relevant))?;
}
finish_interrupted(s, local)?;
visit(s, content, outer.chain(local))?;
finish_interrupted(s, local)?;
if pagebreak {
visit(s, PagebreakElem::shared_boundary(), *outer)?;
}
Ok(())
}
fn visit_grouping_rules<'a>(
s: &mut State<'a, '_, '_, '_>,
content: &'a Content,
styles: StyleChain<'a>,
) -> SourceResult<bool> {
let elem = content.elem();
let matching = s.rules.iter().find(|&rule| (rule.trigger)(elem));
while let Some(active) = s.groupings.last() {
if matching.is_some_and(|rule| rule.priority > active.rule.priority) {
break;
}
if (active.rule.trigger)(elem) || (active.rule.inner)(elem) {
s.sink.push((content, styles));
return Ok(true);
}
finish_innermost_grouping(s)?;
}
if let Some(rule) = matching {
let start = s.sink.len();
s.groupings.push(Grouping { start, rule });
s.sink.push((content, styles));
return Ok(true);
}
Ok(false)
}
fn visit_filter_rules<'a>(
s: &mut State<'a, '_, '_, '_>,
content: &'a Content,
styles: StyleChain<'a>,
) -> SourceResult<bool> {
if content.is::<SpaceElem>() && !matches!(s.kind, RealizationKind::Math) {
return Ok(true);
} else if content.is::<ParbreakElem>() {
s.may_attach = false;
return Ok(true);
} else if !s.may_attach
&& content.to_packed::<VElem>().is_some_and(|elem| elem.attach(styles))
{
return Ok(true);
}
s.may_attach = content.is::<ParElem>();
Ok(false)
}
fn finish(s: &mut State) -> SourceResult<()> {
finish_grouping_while(s, |s| !s.groupings.is_empty())?;
if let RealizationKind::Math = s.kind {
collapse_spaces(&mut s.sink, 0);
}
Ok(())
}
fn finish_interrupted(s: &mut State, local: &Styles) -> SourceResult<()> {
let mut last = None;
for elem in local.iter().filter_map(|style| style.element()) {
if last == Some(elem) {
continue;
}
finish_grouping_while(s, |s| {
s.groupings.iter().any(|grouping| (grouping.rule.interrupt)(elem))
})?;
last = Some(elem);
}
Ok(())
}
fn finish_grouping_while<F>(s: &mut State, f: F) -> SourceResult<()>
where
F: Fn(&State) -> bool,
{
let mut i = 0;
while f(s) {
finish_innermost_grouping(s)?;
i += 1;
if i > 512 {
bail!(Span::detached(), "maximum grouping depth exceeded");
}
}
Ok(())
}
fn finish_innermost_grouping(s: &mut State) -> SourceResult<()> {
let Grouping { start, rule } = s.groupings.pop().unwrap();
let trimmed = s.sink[start..].trim_end_matches(|(c, _)| !(rule.trigger)(c.elem()));
let end = start + trimmed.len();
let tail = s.store_slice(&s.sink[end..]);
s.sink.truncate(end);
let mut tags = BumpVec::<Pair>::new_in(&s.arenas.bump);
if !rule.tags {
let mut k = start;
for i in start..end {
if s.sink[i].0.is::<TagElem>() {
tags.push(s.sink[i]);
continue;
}
if k < i {
s.sink[k] = s.sink[i];
}
k += 1;
}
s.sink.truncate(k);
}
(rule.finish)(Grouped { s, start })?;
for &(content, styles) in tags.iter().chain(&tail) {
visit(s, content, styles)?;
}
Ok(())
}
const MAX_GROUP_NESTING: usize = 3;
static NORMAL_RULES: &[&GroupingRule] = &[&TEXTUAL, &PAR, &CITES, &LIST, &ENUM, &TERMS];
static MATH_RULES: &[&GroupingRule] = &[&CITES, &LIST, &ENUM, &TERMS];
static TEXTUAL: GroupingRule = GroupingRule {
priority: 3,
tags: true,
trigger: |elem| {
elem == TextElem::elem()
|| elem == LinebreakElem::elem()
|| elem == SmartQuoteElem::elem()
},
inner: |elem| elem == SpaceElem::elem(),
interrupt: |_| true,
finish: finish_textual,
};
static PAR: GroupingRule = GroupingRule {
priority: 1,
tags: true,
trigger: |elem| {
elem == TextElem::elem()
|| elem == HElem::elem()
|| elem == LinebreakElem::elem()
|| elem == SmartQuoteElem::elem()
|| elem == InlineElem::elem()
|| elem == BoxElem::elem()
},
inner: |elem| elem == SpaceElem::elem(),
interrupt: |elem| elem == ParElem::elem() || elem == AlignElem::elem(),
finish: finish_par,
};
static CITES: GroupingRule = GroupingRule {
priority: 2,
tags: false,
trigger: |elem| elem == CiteElem::elem(),
inner: |elem| elem == SpaceElem::elem(),
interrupt: |elem| elem == CiteGroup::elem(),
finish: finish_cites,
};
static LIST: GroupingRule = list_like_grouping::<ListElem>();
static ENUM: GroupingRule = list_like_grouping::<EnumElem>();
static TERMS: GroupingRule = list_like_grouping::<TermsElem>();
const fn list_like_grouping<T: ListLike>() -> GroupingRule {
GroupingRule {
priority: 2,
tags: false,
trigger: |elem| elem == T::Item::elem(),
inner: |elem| elem == SpaceElem::elem() || elem == ParbreakElem::elem(),
interrupt: |elem| elem == T::elem(),
finish: finish_list_like::<T>,
}
}
fn finish_textual(Grouped { s, mut start }: Grouped) -> SourceResult<()> {
if visit_textual(s, start)? {
return Ok(());
}
if in_non_par_grouping(s) {
let elems = s.store_slice(&s.sink[start..]);
s.sink.truncate(start);
finish_grouping_while(s, in_non_par_grouping)?;
start = s.sink.len();
s.sink.extend(elems);
}
if s.groupings.is_empty() {
s.groupings.push(Grouping { start, rule: &PAR });
}
Ok(())
}
fn in_non_par_grouping(s: &State) -> bool {
s.groupings
.last()
.is_some_and(|grouping| !std::ptr::eq(grouping.rule, &PAR))
}
fn finish_par(mut grouped: Grouped) -> SourceResult<()> {
let (sink, start) = grouped.get_mut();
collapse_spaces(sink, start);
let elems = grouped.get();
let span = select_span(elems);
let (children, trunk) = StyleVec::create(elems);
let s = grouped.end();
let elem = ParElem::new(children).pack().spanned(span);
visit(s, s.store(elem), trunk)
}
fn finish_cites(grouped: Grouped) -> SourceResult<()> {
let elems = grouped.get();
let span = select_span(elems);
let trunk = elems[0].1;
let children = elems
.iter()
.filter_map(|(c, _)| c.to_packed::<CiteElem>())
.cloned()
.collect();
let s = grouped.end();
let elem = CiteGroup::new(children).pack().spanned(span);
visit(s, s.store(elem), trunk)
}
fn finish_list_like<T: ListLike>(grouped: Grouped) -> SourceResult<()> {
let elems = grouped.get();
let span = select_span(elems);
let tight = !elems.iter().any(|(c, _)| c.is::<ParbreakElem>());
let styles = elems.iter().filter(|(c, _)| c.is::<T::Item>()).map(|&(_, s)| s);
let trunk = StyleChain::trunk(styles).unwrap();
let trunk_depth = trunk.links().count();
let children = elems
.iter()
.copied()
.filter_map(|(c, s)| {
let item = c.to_packed::<T::Item>()?.clone();
let local = s.suffix(trunk_depth);
Some(T::Item::styled(item, local))
})
.collect();
let s = grouped.end();
let elem = T::create(children, tight).pack().spanned(span);
visit(s, s.store(elem), trunk)
}
fn visit_textual(s: &mut State, start: usize) -> SourceResult<bool> {
if let Some(m) = find_regex_match_in_elems(s, &s.sink[start..]) {
collapse_spaces(&mut s.sink, start);
let elems = s.store_slice(&s.sink[start..]);
s.sink.truncate(start);
visit_regex_match(s, &elems, m)?;
return Ok(true);
}
Ok(false)
}
fn find_regex_match_in_elems<'a>(
s: &State,
elems: &[Pair<'a>],
) -> Option<RegexMatch<'a>> {
let mut buf = BumpString::new_in(&s.arenas.bump);
let mut base = 0;
let mut leftmost = None;
let mut current = StyleChain::default();
let mut space = SpaceState::Destructive;
for &(content, styles) in elems {
if content.is::<TagElem>() {
continue;
}
let linebreak = content.is::<LinebreakElem>();
if linebreak {
if let SpaceState::Space(_) = space {
buf.pop();
}
}
if styles != current && !buf.is_empty() {
leftmost = find_regex_match_in_str(&buf, current);
if leftmost.is_some() {
break;
}
base += buf.len();
buf.clear();
}
current = styles;
space = if content.is::<SpaceElem>() {
if space != SpaceState::Supportive {
continue;
}
buf.push(' ');
SpaceState::Space(0)
} else if linebreak {
buf.push('\n');
SpaceState::Destructive
} else if let Some(elem) = content.to_packed::<SmartQuoteElem>() {
buf.push(if elem.double(styles) { '"' } else { '\'' });
SpaceState::Supportive
} else if let Some(elem) = content.to_packed::<TextElem>() {
buf.push_str(&elem.text);
SpaceState::Supportive
} else {
panic!("tried to find regex match in non-textual elements");
};
}
if leftmost.is_none() {
leftmost = find_regex_match_in_str(&buf, current);
}
leftmost.map(|m| RegexMatch { offset: base + m.offset, ..m })
}
fn find_regex_match_in_str<'a>(
text: &str,
styles: StyleChain<'a>,
) -> Option<RegexMatch<'a>> {
let mut r = 0;
let mut revoked = SmallBitSet::new();
let mut leftmost: Option<(regex::Match, RecipeIndex, &Recipe)> = None;
let depth = Lazy::new(|| styles.recipes().count());
for entry in styles.entries() {
let recipe = match &**entry {
Style::Recipe(recipe) => recipe,
Style::Property(_) => continue,
Style::Revocation(index) => {
revoked.insert(index.0);
continue;
}
};
r += 1;
let Some(Selector::Regex(regex)) = recipe.selector() else { continue };
let Some(m) = regex.find(text) else { continue };
if m.range().is_empty() {
continue;
}
if leftmost.is_some_and(|(p, ..)| p.start() <= m.start()) {
continue;
}
let index = RecipeIndex(*depth - (r - 1));
if revoked.contains(index.0) {
continue;
}
leftmost = Some((m, index, recipe));
}
leftmost.map(|(m, id, recipe)| RegexMatch {
offset: m.start(),
text: m.as_str().into(),
id,
recipe,
styles,
})
}
fn visit_regex_match<'a>(
s: &mut State<'a, '_, '_, '_>,
elems: &[Pair<'a>],
m: RegexMatch<'a>,
) -> SourceResult<()> {
let match_range = m.offset..m.offset + m.text.len();
let piece = TextElem::packed(m.text);
let context = Context::new(None, Some(m.styles));
let output = m.recipe.apply(s.engine, context.track(), piece)?;
let mut cursor = 0;
let mut output = Some(output);
let mut visit_unconsumed_match = |s: &mut State<'a, '_, '_, '_>| -> SourceResult<()> {
if let Some(output) = output.take() {
let revocation = Style::Revocation(m.id).into();
let outer = s.arenas.bump.alloc(m.styles);
let chained = outer.chain(s.arenas.styles.alloc(revocation));
visit(s, s.store(output), chained)?;
}
Ok(())
};
for &(content, styles) in elems {
if content.is::<TagElem>() {
visit(s, content, styles)?;
continue;
}
let len = content.to_packed::<TextElem>().map_or(1, |elem| elem.text.len());
let elem_range = cursor..cursor + len;
if elem_range.start < match_range.start {
if elem_range.end <= match_range.start {
visit(s, content, styles)?;
} else {
let mut elem = content.to_packed::<TextElem>().unwrap().clone();
elem.text = elem.text[..match_range.start - elem_range.start].into();
visit(s, s.store(elem.pack()), styles)?;
}
}
if match_range.start < elem_range.end {
visit_unconsumed_match(s)?;
}
if elem_range.end > match_range.end {
if elem_range.start >= match_range.end {
visit(s, content, styles)?;
} else {
let mut elem = content.to_packed::<TextElem>().unwrap().clone();
elem.text = elem.text[match_range.end - elem_range.start..].into();
visit(s, s.store(elem.pack()), styles)?;
}
}
cursor = elem_range.end;
}
visit_unconsumed_match(s)?;
Ok(())
}
fn collapse_spaces(buf: &mut Vec<Pair>, start: usize) {
let mut state = SpaceState::Destructive;
let mut k = start;
for i in start..buf.len() {
let (content, styles) = buf[i];
if content.is::<TagElem>() {
} else if content.is::<SpaceElem>() {
if state != SpaceState::Supportive {
continue;
}
state = SpaceState::Space(k);
} else if content.is::<LinebreakElem>() {
destruct_space(buf, &mut k, &mut state);
} else if let Some(elem) = content.to_packed::<HElem>() {
if elem.amount.is_fractional() || elem.weak(styles) {
destruct_space(buf, &mut k, &mut state);
}
} else {
state = SpaceState::Supportive;
};
if k < i {
buf[k] = buf[i];
}
k += 1;
}
destruct_space(buf, &mut k, &mut state);
buf.truncate(k);
}
fn destruct_space(buf: &mut [Pair], end: &mut usize, state: &mut SpaceState) {
if let SpaceState::Space(s) = *state {
buf.copy_within(s + 1..*end, s);
*end -= 1;
}
*state = SpaceState::Destructive;
}
fn select_span(children: &[Pair]) -> Span {
Span::find(children.iter().map(|(c, _)| c.span()))
}