diff --git a/crates/engine/src/cmd/spelling.rs b/crates/engine/src/cmd/spelling.rs index 03f52d2e19..cd8b4890f2 100644 --- a/crates/engine/src/cmd/spelling.rs +++ b/crates/engine/src/cmd/spelling.rs @@ -48,7 +48,7 @@ pub fn specs() -> Vec { "Change All", [], None, - "{to: string, words: [{id, start: byte, end: byte}…] (words of type objects, as text.spelling.check lists them; none overlapping)} replaces each word with to, all in one undo step, each object laid out once (Check Spelling's Change All); an error, with nothing changed, when a word's bytes are not in its object's text → {changed: count}", + "{to: string, words: [{id, start: byte, end: byte}…] (words of type objects, as text.spelling.check lists them; none overlapping)} replaces each word with to, all in one undo step, each object laid out once (Check Spelling's Change All); a word that already reads as to is left alone, and with no other word there is no edit and no undo step; an error, with nothing changed, when a word's bytes are not in its object's text → {changed: count of words replaced}", has_doc, change_words ), @@ -208,7 +208,10 @@ fn change_words(s: &mut Session, p: &Value) -> Result { } last_end = b; } + // A word that already reads as `to` is left alone, so a list of only those makes no edit. + ranges.retain(|&(a, b)| text.get(a..b) != Some(to.as_str())); } + by_id.retain(|_, ranges| !ranges.is_empty()); let changed: usize = by_id.values().map(Vec::len).sum(); if changed == 0 { return Ok(json!({ "changed": 0 })); diff --git a/crates/engine/src/tests/spelling.rs b/crates/engine/src/tests/spelling.rs index 6142939a33..107c8b0418 100644 --- a/crates/engine/src/tests/spelling.rs +++ b/crates/engine/src/tests/spelling.rs @@ -452,6 +452,27 @@ fn change_all_replaces_every_word_listed_in_one_undo_step() { assert_eq!(run(&mut s, "text.spelling.change", json!({"to": "x", "words": []}))["changed"], 0); } +/// A listed word that already reads as `to` is left alone: with no other word, no edit and no +/// undo step, so the redo history stays. +#[test] +fn change_all_skips_words_that_already_read_as_the_replacement() { + let mut s = session(); + let a = text(&mut s, 50.0, "fixed qzxvw"); + run(&mut s, "text.editRange", json!({"id": a.0, "start": 6, "end": 11, "insert": "other"})); + run(&mut s, "edit.undo", json!({})); + let (undo, redo) = { + let h = &s.doc().unwrap().history; + (h.undo.len(), h.redo.len()) + }; + let r = run(&mut s, "text.spelling.change", json!({"to": "fixed", "words": [{"id": a.0, "start": 0, "end": 5}]})); + assert_eq!(r["changed"], 0); + let h = &s.doc().unwrap().history; + assert_eq!((h.undo.len(), h.redo.len()), (undo, redo), "no undo step, and redo kept"); + let words = json!([{"id": a.0, "start": 0, "end": 5}, {"id": a.0, "start": 6, "end": 11}]); + assert_eq!(run(&mut s, "text.spelling.change", json!({"to": "fixed", "words": words}))["changed"], 1); + assert_eq!(plain(&s, a), "fixed fixed"); +} + /// Each change to the custom dictionary moves its generation on, whether it is saved or not, so /// what Auto Spell Check found before is checked again. #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 252e90fbfd..ed7ecd0413 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -9305,7 +9305,8 @@ system Hunspell dictionary, and last the public-domain Moby word list built into ones `text.editRange` takes, so `text.editRange {id, start, end, insert}` changes a word as one undo step, and `text.spelling.change {to, words: [{id, start, end}…]}` changes every listed word to `to` as one undo step (Check Spelling's Change All; an error, with nothing changed, when a word's bytes are not in its object's text or two words -overlap). At most +overlap). A listed word that already reads as `to` is left alone; when every one does, the command makes no edit and +no undo step, and `changed` is 0. At most `max` words are listed (1 to 10,000, default 10,000); `more` is `true` when there were more. To get the rest, check again with `from: {id, byte}` set to the last word's `id` and `end`: that check lists the words of that type object that start at `byte` or later, then those of the objects after it, so a type object with more unknown words than one