Compare commits

...
Author SHA1 Message Date
autocraft-bot[bot] f1ac9bf7cf Merge main (3a6082b4) into the pull request
I resolved the three conflicts in crates/engine/src/lib.rs and kept both

I resolved the three conflicts in crates/engine/src/lib.rs and kept both sides. The DocState field and its constructor now hold the PR's `autocorrected` and the default branch's `notes_run` and `notes_before`. In `execute`, the default branch's notes-joining logic is kept. The PR's extra "AutoCorrect" undo step is pushed only in the branch that records a normal undo entry, with `vertex_kind.clone()` as in the PR. The PR's new test `undo_takes_back_only_the_autocorrect` passes, as do the 9 notes tests, and `cargo fmt --check` is clean. I did not run clippy or the full engine test suite.
2026-12-02 19:08:25 +00:00
autocraft-bot[bot] 0b1399df94 Apply review fixes
- crates/engine/src/cmd/text/autocorrect.rs
- crates/engine/src/tests.rs
2026-11-17 09:04:28 +00:00
Luis 2359e5c066 AutoCorrect: Undo takes back only the correction (#2184)
Typing the space that set off AutoCorrect and the correction were one undo step, so Ctrl+Z removed the space too and typing it again corrected the word again. The correction (word replacement, sentence caps, ordinal superscripts) is now its own undo step on top of the typed character, as in PowerPoint.
2026-11-17 09:01:43 +01:00
3 changed files with 62 additions and 3 deletions
+4 -2
View File
@@ -168,9 +168,9 @@ pub(super) fn autocorrect(s: &mut Session, typing: bool) -> Result<()> {
while start > 0 && line.get(start - 1).is_some_and(|c| !c.is_whitespace()) {
start -= 1;
}
// "word," and "word." also count: the punctuation was typed before the space.
// "word," and "word." also count when the text came in one piece; a typed punctuation mark already ran AutoCorrect on the word, and an Undo of that correction stays (#2184).
let mut word: String = line.get(start..word_end).map(|w| w.iter().collect()).unwrap_or_default();
if replacement(&s.prefs, &word).is_none() {
if !typing && replacement(&s.prefs, &word).is_none() {
let trimmed = word.trim_end_matches(|c: char| ".,;:!?".contains(c));
if !trimmed.is_empty() && trimmed.len() < word.len() && !word.ends_with("...") {
word_end -= word.chars().count() - trimmed.chars().count();
@@ -195,6 +195,7 @@ pub(super) fn autocorrect(s: &mut Session, typing: bool) -> Result<()> {
};
let delta = rep.chars().count() as isize - word.chars().count() as isize;
let st = s.doc_mut()?;
st.autocorrected = Some((st.doc.clone(), st.selection.clone()));
let mut d = (*st.doc).clone();
let sel = st.selection.clone();
if let Some(b) = body_mut(&mut d, &sel, &t) {
@@ -315,6 +316,7 @@ fn superscript_suffix(s: &mut Session, t: &crate::TextSel, end: usize) -> Result
if body_of(st, t).is_none_or(|body| ed::props_at(body, b).baseline.unwrap_or(0.0) > 0.0) {
return Ok(());
}
st.autocorrected = Some((st.doc.clone(), st.selection.clone()));
let mut d = (*st.doc).clone();
let sel = st.selection.clone();
if let Some(body) = body_mut(&mut d, &sel, t) {
+13 -1
View File
@@ -237,6 +237,9 @@ pub struct DocState {
/// The list AutoFormat just started from a typed `* ` or `1. `, so Backspace right after
/// can put the typed text back.
pub auto_list: Option<cmd::text::AutoList>,
/// The presentation and selection between a typed character and the AutoCorrect it set off,
/// so the command's undo step splits in two and Undo takes back only the correction (#2184).
pub autocorrected: Option<(Arc<Presentation>, Selection)>,
/// The slide whose editor's consecutive notes changes share one undo step.
pub notes_run: Option<SlideId>,
notes_before: Option<Arc<Presentation>>,
@@ -269,6 +272,7 @@ impl DocState {
compare: None,
last_duplicate: None,
auto_list: None,
autocorrected: None,
notes_run: None,
notes_before: None,
}
@@ -729,6 +733,9 @@ impl Session {
}
if outer {
self.tool.vertex_kind = vertex_kind;
if let Some(st) = self.active_mut() {
st.autocorrected = None;
}
}
return Err(e);
}
@@ -736,6 +743,7 @@ impl Session {
if let Some(st) = self.active_mut() {
clamp_selection(st);
}
let autocorrected = if outer { self.active_mut().and_then(|st| st.autocorrected.take()) } else { None };
if let (Some((uid, old, sel)), Some(st)) = (before, self.active_mut())
&& st.uid == uid
&& !Arc::ptr_eq(&old, &st.doc)
@@ -753,8 +761,12 @@ impl Session {
}
} else {
let notes_before = (id == "slide.notes" && st.notes_run.is_some()).then(|| old.clone());
push_undo(st, HistoryEntry { label: spec.label.to_string(), doc: old, selection: sel, vertex_kind });
push_undo(st, HistoryEntry { label: spec.label.to_string(), doc: old, selection: sel, vertex_kind: vertex_kind.clone() });
st.notes_before = notes_before;
// The text as typed, before AutoCorrect changed it: its own step, undone first.
if let Some((doc, selection)) = autocorrected.filter(|(doc, _)| !Arc::ptr_eq(doc, &st.doc)) {
push_undo(st, HistoryEntry { label: "AutoCorrect".into(), doc, selection, vertex_kind });
}
}
}
if spec.journal && outer {
+45
View File
@@ -5301,6 +5301,51 @@ fn autocorrect_options_as_you_type() {
assert_eq!(s.execute("text.get", &json!({"id": title})).unwrap()["text"], "monday notes ");
}
/// #2184: Undo right after AutoCorrect takes back only the correction; the space that set it off
/// stays, and typing on does not correct the word again. A second Undo removes the space.
#[test]
fn undo_takes_back_only_the_autocorrect() {
let mut s = session();
let title = s.doc().unwrap().current_slide().unwrap().shapes[0].id.0;
let text = |s: &Session| {
s.doc().unwrap().current_slide().unwrap().shapes.iter().find(|sh| sh.id.0 == title).unwrap().text.as_ref().unwrap().paragraphs[0].text()
};
s.execute("text.edit", &json!({"id": title})).unwrap();
for c in "hello ".chars() {
s.execute("text.insert", &json!({"text": c.to_string()})).unwrap();
}
assert_eq!(text(&s), "Hello ");
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(text(&s), "hello ", "the correction is undone, the space stays");
assert_eq!(s.doc().unwrap().selection.text.as_ref().map(|t| t.caret), Some((0, 6)), "the caret stays after the space");
// Redo brings the correction back.
s.execute("edit.redo", &json!({})).unwrap();
assert_eq!(text(&s), "Hello ");
s.execute("edit.undo", &json!({})).unwrap();
for c in "world ".chars() {
s.execute("text.insert", &json!({"text": c.to_string()})).unwrap();
}
assert_eq!(text(&s), "hello world ", "typing on leaves the word as it is");
for _ in 0.."world ".len() {
s.execute("edit.undo", &json!({})).unwrap();
}
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(text(&s), "hello", "the next Undo removes the space");
// Typing that AutoCorrect leaves alone is still one step per character.
s.execute("edit.undo", &json!({})).unwrap();
assert_eq!(text(&s), "hell");
// A typed comma sets AutoCorrect off too; the space after it leaves the undone word alone.
s.execute("text.selectAll", &json!({})).ok();
s.execute("text.delete", &json!({"dir": "backward"})).ok();
for c in "hello,".chars() {
s.execute("text.insert", &json!({"text": c.to_string()})).unwrap();
}
assert_eq!(text(&s), "Hello,");
s.execute("edit.undo", &json!({})).unwrap();
s.execute("text.insert", &json!({"text": " "})).unwrap();
assert_eq!(text(&s), "hello, ", "the space after the comma leaves the undone word alone");
}
#[test]
fn autocorrect_superscripts_ordinals_as_you_type() {
let mut s = session();