From 05f2a84400cb1d0407fada0926ee57e963a48c11 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 24 Oct 2026 00:44:36 -0700 Subject: [PATCH] Text: text.insert reports the caret where AutoCorrect leaves it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit text.insert returned the caret from before AutoCorrect ran. After a typed space started a list, it reported (0, 2) for `* ` while the caret was at (0, 0) in the emptied paragraph, and after `(c) ` it reported (0, 4) for the two-character `© `. It now reports the caret from the selection after AutoCorrect. --- crates/engine/src/cmd/text.rs | 2 ++ crates/engine/src/tests.rs | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/text.rs b/crates/engine/src/cmd/text.rs index a7d0b71d..23bc0713 100644 --- a/crates/engine/src/cmd/text.rs +++ b/crates/engine/src/cmd/text.rs @@ -425,6 +425,8 @@ pub(crate) fn insert_text(s: &mut Session, text: &str, props: Option) c })?; autocorrect(s, text.chars().count() == 1)?; + // AutoCorrect can move the caret: a replaced word, or a list lead it removed. + let pos = s.doc()?.selection.text.as_ref().map_or(pos, |t| t.caret); Ok(json!({"caret": [pos.0, pos.1]})) } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index f9717717..ca9203bb 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3335,6 +3335,10 @@ fn autocorrect_options_as_you_type() { }; assert_eq!(typed(&mut s, "hello world. see you monday, i think. "), "Hello world. See you Monday, I think. "); assert_eq!(typed(&mut s, "THe tHIS 1/2 recieved... "), "The This ½ received… "); + // `text.insert` reports the caret after the correction. + assert_eq!(typed(&mut s, "(c)"), "(c)"); + assert_eq!(s.execute("text.insert", &json!({"text": " "})).unwrap()["caret"], json!([0, 2])); + assert_eq!(s.execute("text.get", &json!({"id": title})).unwrap()["text"], "© "); // Options turn rules off. s.execute("text.autocorrect", &json!({"sentenceCaps": false, "days": false})).unwrap(); assert_eq!(typed(&mut s, "hello monday "), "hello monday "); @@ -3437,8 +3441,11 @@ fn autoformat_starts_bulleted_and_numbered_lists_as_you_type() { assert_eq!(typed(&mut s, "*, x"), ("*, x".to_string(), none.clone())); assert_eq!(typed(&mut s, "2. x"), ("2. x".to_string(), none.clone())); assert_eq!(typed(&mut s, "b) x"), ("b) x".to_string(), none.clone())); + // The space that starts the list reports the caret at the start of the emptied paragraph. + assert_eq!(typed(&mut s, "*"), ("*".to_string(), none.clone())); + assert_eq!(s.execute("text.insert", &json!({"text": " "})).unwrap()["caret"], json!([0, 0])); + assert_eq!(para(&s), (String::new(), ch("•"))); // Backspace right after puts the lead and the space back, with the paragraph as it was. - assert_eq!(typed(&mut s, "* "), (String::new(), ch("•"))); s.execute("text.delete", &json!({"dir": "backward"})).unwrap(); assert_eq!(para(&s), ("* ".to_string(), none.clone())); s.execute("text.insert", &json!({"text": "x"})).unwrap();