Keep structural units out of text edit reflow

This commit is contained in:
gloamxun committed 2026-10-20 12:49:15 +08:00
1 parent 753aac632d
commit c98ec0056f
2 files changed
+42 -2

No files matched your search

+39 -2
View File
@@ -138,6 +138,11 @@ pub struct Unit {
pub id: UnitId,
pub source: String,
pub items: Vec<VerticalItem>,
/// Present when this unit was built from text and can be re-flowed by
/// []. Structural units (halign, floats, insertions) are not
/// editable through a plain source string.
#[serde(default)]
pub paragraph_style: Option<ParagraphStyle>,
}
impl Unit {
@@ -192,6 +197,7 @@ impl Unit {
Self {
id,
source: "halign".to_owned(),
paragraph_style: None,
items: vec![VerticalItem::Halign(Halign {
rows,
column_widths,
@@ -222,6 +228,7 @@ impl Unit {
width: paragraph.width.max(style.line_width),
height: paragraph.height,
})],
paragraph_style: Some(style),
}
}
@@ -231,6 +238,7 @@ impl Unit {
id,
source: String::new(),
items,
paragraph_style: None,
}
}
@@ -537,8 +545,12 @@ impl Engine {
return;
};
let mut next = document.clone();
next.units[unit_index].source = source.into();
next.units[unit_index] = Unit::paragraph(unit_id, next.units[unit_index].source.clone());
let source = source.into();
let Some(style) = next.units[unit_index].paragraph_style.clone() else {
next.units[unit_index].source = source;
return;
};
next.units[unit_index] = Unit::paragraph_with_style(unit_id, source, style);
let mut checkpoint =
self.layout
@@ -754,6 +766,7 @@ mod tests {
let unit = Unit {
id: 7,
source: "footnote layout".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Insertion(Insertion {
class: 0,
@@ -785,6 +798,7 @@ mod tests {
let unit = |id| Unit {
id,
source: "boxes".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -816,6 +830,7 @@ mod tests {
let unit = Unit {
id: 0,
source: "penalty".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -842,6 +857,7 @@ mod tests {
vec![Unit {
id: 0,
source: text.to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -899,6 +915,7 @@ mod tests {
let unit = Unit {
id: 0,
source: "float".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -927,6 +944,7 @@ mod tests {
let unit = Unit {
id: 0,
source: "bottom float".to_owned(),
paragraph_style: None,
items: vec![
VerticalItem::Box(VerticalBox {
width: 10,
@@ -950,6 +968,25 @@ mod tests {
assert_eq!(engine.layout.pages[1].items.len(), 1);
}
#[test]
fn edit_ignores_structural_units() {
let rows = vec![HalignRow {
cells: vec![HalignCell {
text: "A".to_owned(),
span: 1,
alignment: CellAlignment::Left,
}],
rule: false,
}];
let document = Document::new(100, vec![Unit::halign(0, rows, 1, 0)]);
let mut engine = Engine::layout(&document);
let before: Vec<_> = engine.pages().iter().map(|page| page.items.len()).collect();
engine.edit(&document, 0, "replacement text");
assert_eq!(engine.recomputed_pages(), 0);
let after: Vec<_> = engine.pages().iter().map(|page| page.items.len()).collect();
assert_eq!(before, after);
}
#[test]
fn insertion_reserves_page_height() {
let boxed = |height: i64| VerticalItem::Box(VerticalBox { width: 100, height });
+3
View File
@@ -30,6 +30,9 @@ operation from the start. It does **not** embed pdfTeX, XeTeX or Tectonic.
- `Engine::edit` which restores the checkpoint before the edited unit,
re-executes only the suffix, compares page hashes, and reports
`recomputed_pages`;
- `Unit::paragraph_style` records whether a unit is text and can be
re-flowed by `Engine::edit`; structural units (halign, insertion, float)
are never rewritten from a plain source string.
- serde round-trip for checkpoints.
M0 proves the incremental contract; M1 starts replacing the placeholder with