DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge #177: Memory: release retired layouts and reserve text growth exactly
aiongg9f0b34e876da
diff --cc crates/engine/src/cmd/text.rsindex 159e3956d,7f9bf9df0..a428962fa--- a/crates/engine/src/cmd/text.rs+++ b/crates/engine/src/cmd/text.rs@@@ -503,22 -418,18 +510,26 @@@ fn insert(s: &mut Session, p: &Value) -if tracking && t.cell.is_none() {// Track Changes: the replaced text is marked deleted, the typing inserted after it.let at = super::changes::mark_deleted(st, r.clone());+ reserve_command_text(st, text.len())?;let mut fmt = st.char_format_at(at).clone();+ if let Some(f) = &typing {+ apply_typing_format(&mut fmt, f);+ }fmt.over.change = Some(designcraft_doc::ChangeMark::Inserted);st.insert_with(at, &text, fmt);let pos = at + text.len() - trail;sel.text = Some(TextSel { anchor: pos, focus: pos, ..t });return Ok(json!({"pos": pos}));}+ let a = floor_char_boundary(&st.text, r.start.min(st.len()));+ let b = floor_char_boundary(&st.text, r.end.min(st.len())).max(a);+ reserve_command_text(st, text.len().saturating_sub(b - a))?;st.replace(r.clone(), &text);+ if let Some(f) = &typing+ && !text.is_empty()+ {+ st.format_chars(r.start..r.start + text.len(), |fmt| apply_typing_format(fmt, f));+ }let pos = r.start + text.len() - trail;sel.text = Some(TextSel { anchor: pos, focus: pos, ..t });Ok(json!({"pos": pos}))@@@ -1956,18 -1650,40 +1967,56 @@@ mod vertical_caret_tests}}+#[cfg(test)]+mod story_query_contract_tests {+ use serde_json::json;+ #[test]+ fn missing_story_errors_but_valid_empty_and_selected_stories_succeed() {+ let mut s = crate::Session::new();+ s.execute("file.new", &json!({})).unwrap();+ assert!(s.execute("story.get", &json!({"story":999})).unwrap_err().to_string().contains("no such story"));+ let f = s.execute("frame.create", &json!({"rect":[36,36,300,200],"content":"text"})).unwrap();+ assert_eq!(s.execute("story.get", &json!({"story":f["story"]})).unwrap()["text"], "");+ assert_eq!(s.execute("story.get", &json!({"frame":f["id"]})).unwrap()["text"], "");+ s.execute("text.select", &json!({"story":f["story"],"anchor":0})).unwrap();+ assert_eq!(s.execute("story.get", &json!({})).unwrap()["text"], "");+ }+}+++ #[cfg(test)]+ mod reserve_tests {+ use super::*;++ #[test]+ fn failed_reservation_rolls_back_the_edit_even_after_local_text_was_removed() {+ let mut s = Session::new();+ s.execute("file.new", &json!({})).unwrap();+ let result = s.execute("frame.create", &json!({"rect":[36,36,300,300],"content":"text","text":"Keep this text."})).unwrap();+ let sid = StoryId(result["story"].as_u64().unwrap());+ s.execute("text.select", &json!({"story":sid.0,"anchor":1,"focus":4})).unwrap();+ let before = s.doc().unwrap().clone();+ assert!(before.selection.text.is_some_and(|t| !t.range().is_empty()));+ let journal = s.journal.clone();+ let error = s+ .edit(|d, sel| {+ let st = d.story_mut(sid).unwrap();+ st.format_chars(5..9, |f| f.over.change = Some(designcraft_doc::ChangeMark::Inserted));+ let at = super::super::changes::mark_deleted(st, 0..9);+ assert_eq!(at, 5);+ assert_eq!(st.text, "Keep text.");+ assert_eq!(st.format_after(0).over.change, Some(designcraft_doc::ChangeMark::Deleted));+ sel.text = None;+ reserve_command_text(st, usize::MAX)?;+ ok()+ })+ .unwrap_err();+ assert!(error.to_string().contains("could not reserve text insertion"));+ let after = s.doc().unwrap();+ assert!(std::sync::Arc::ptr_eq(&after.doc, &before.doc));+ assert_eq!(after.selection, before.selection);+ assert_eq!(after.revision, before.revision);+ assert_eq!(after.history.undo.len(), before.history.undo.len());+ assert_eq!(after.history.redo.len(), before.history.redo.len());+ assert_eq!(s.journal, journal);+ }+ }