diff --git a/crates/ui-egui/src/dialogs/check_spelling.rs b/crates/ui-egui/src/dialogs/check_spelling.rs index efba47da86..cd36ebdc46 100644 --- a/crates/ui-egui/src/dialogs/check_spelling.rs +++ b/crates/ui-egui/src/dialogs/check_spelling.rs @@ -1,23 +1,19 @@ //! Edit › Spelling › Check Spelling…: walk through unknown words in the selection or the whole -//! document, displaying the unrecognized word in its sentence context, with suggestions, and -//! Ignore, Ignore All, Change, Change All, Add to Dictionary, and Done / Cancel. +//! document. The framework's `CheckSpellingDialog` shows the word in its sentence with suggestions +//! and files the buttons as `SpellingAction`s; this file is the walk behind it (`SpellingWalk`) +//! and the pump that runs each action against the document. //! -//! Fields: -//! - `not_in_dictionary`: the current unrecognized word -//! - `context`: sentence context containing the unrecognized word -//! - `context_before`, `context_word`, `context_after`: parts of the sentence context -//! - `suggestions`: alternative words suggested by the dictionary -//! - `selected_suggestion`: the suggestion currently selected -//! - `change_to`: the replacement word (from suggestions or typed) -//! - `node_id`, `start`, `end`, `lang`: location of the current misspelling -//! - `ids`: object IDs checked (None: entire document) -//! - `skipped`: list of `[node_id, start, word]` skipped by Ignore, which Change All leaves alone +//! The walk's state: +//! - `doc`: uid of the document it started on; another active document ends it +//! - `ids`: object IDs checked (None: entire document; Some(empty): nothing) +//! - `layer`: the one object checked while the dialog's Check All Layers is off +//! - `skipped`: `(node_id, start, word)` skipped by Ignore, which Change All leaves alone //! - `queue`: the words the last check listed after the one shown, last first; `queue_more` -//! whether that check had more, and `queue_from` (`[node_id, byte]`) where the next check goes +//! whether that check had more, and `queue_from` (`(node_id, byte)`) where the next check goes //! on from //! - `revision`: `[document revision, custom dictionary generation]` as of the word shown; when //! either has moved, the next step checks the scope again from its start -//! - `done`: whether spell checking has finished (or no unrecognized words found) +//! - `shown`: the word shown (None: the spell check has finished or found nothing) //! //! Ignore and Ignore All take the next word from `queue`, and a new check (`text.spelling.check`'s //! `from`) goes on from `queue_from` once it is empty, through the rest of that type object and @@ -27,103 +23,126 @@ use std::collections::{BTreeMap, BTreeSet}; use std::ops::Range; +use craft::text::ui::spelling::{CheckSpellingDialog, SpellingAction, SpellingItem}; use serde_json::{Value, json}; -use super::{DialogAction, DialogResult, DialogSpec}; -use crate::state::Dialog; -use crate::theme::Tokens; -use crate::{VectorcraftApp, widgets}; - -/// The dialog kind of Check Spelling. -pub const KIND: &str = "checkSpelling"; +use crate::VectorcraftApp; /// The command its menu item runs. pub const COMMAND: &str = "ui.checkSpelling"; -pub(super) const SPEC: DialogSpec = DialogSpec { - heading: |_| tl!("Check Spelling").into(), - body_with_actions: Some(body), - confirm, - ok: None, - min_width: 480.0, - max_width: Some(560.0), - ..DialogSpec::FORM -}; +/// An occurrence Ignore skipped: (id, start, word). The word is kept with its bytes, so after +/// another command moves the text, a different word that now starts there is not skipped. +type Skip = (u64, u64, String); -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum Action { - Ignore, - IgnoreAll, - Change, - ChangeAll, - AddToDictionary, - Done, - Cancel, +/// One unknown word: (id, start, end, word, lang). +type Word = (u64, usize, usize, String, String); + +/// Where Check Spelling stands: what it checks, what it skipped and the word shown. +#[derive(Default)] +pub(crate) struct SpellingWalk { + /// Uid of the document the walk started on. + doc: u64, + /// The selected objects; None is the whole document, which Some(empty) is not. + ids: Option>, + /// The one object checked while Check All Layers is off. + layer: Option, + /// The words the last check listed after the one shown, last first. + queue: Vec, + /// Where the next check goes on from when `queue` runs out: (id, byte). + queue_from: Option<(u64, usize)>, + /// Whether the check that filled `queue` had more words. + queue_more: bool, + /// The occurrences Ignore skipped. + skipped: BTreeSet, + /// The document revision and dictionary generation as of the word shown. + revision: Option<[u64; 2]>, + /// The word shown; None when the check is complete. + shown: Option, } -/// Open Check Spelling on the active document's type. -pub fn open(app: &mut VectorcraftApp) -> Result { +/// Starts the walk on the active document's type and finds the first word. +fn start(app: &mut VectorcraftApp) -> Result { if app.session.active().is_none() { return Err("no document".into()); } - let (doc_uid, ids) = { + let (doc, ids) = { let st = app.session.doc().map_err(|e| e.to_string())?; // The selection (groups too: the check goes through the type inside them), or, with // nothing selected, every visible, unlocked type object (the check's default). let selected: Vec = st.selection.objects.iter().map(|n| n.0).collect(); (st.uid, (!selected.is_empty()).then_some(selected)) }; - let mut d = Dialog::new( - KIND, - json!({ - "doc_uid": doc_uid, - "ids": ids, - "skipped": [], - "queue": [], - "queue_more": false, - "queue_from": null, - "not_in_dictionary": "", - "context": "", - "context_before": "", - "context_word": "", - "context_after": "", - "change_to": "", - "suggestions": [], - "selected_suggestion": null, - "node_id": null, - "start": null, - "end": null, - "lang": null, - "done": false, - }), - ); - advance(app, &mut d, None, 1)?; - app.ui.dialog = Some(d); + let mut w = SpellingWalk { doc, ids, ..Default::default() }; + advance(app, &mut w, None, 1)?; + Ok(w) +} + +/// Open Check Spelling on the active document's type. +pub fn open(app: &mut VectorcraftApp) -> Result { + let w = start(app)?; + let items = items_of(app, &w)?; + let dialogs = app.dialogs().ok_or("no window")?; + dialogs.with(|host| match host.get_mut::() { + Some(d) => d.restart(items), + None => { + host.open(CheckSpellingDialog::new(items).all_layers(true)); + } + }); + app.spelling = Some(w); Ok(Value::Null) } -/// An occurrence Ignore skipped: (id, start, word). The word is kept with its bytes, so after -/// another command moves the text, a different word that now starts there is not skipped. -type Skip = (u64, u64, String); - -fn get_skipped(d: &Dialog) -> BTreeSet { - d.fields - .get("skipped") - .and_then(Value::as_array) - .map(|arr| { - arr.iter() - .filter_map(|s| match s.as_array().map(Vec::as_slice) { - Some([id, start, word]) => Some((id.as_u64()?, start.as_u64()?, word.as_str()?.to_string())), - _ => None, - }) - .collect() - }) - .unwrap_or_default() -} - -fn set_skipped(d: &mut Dialog, skipped: &BTreeSet) { - let arr: Vec = skipped.iter().map(|(id, start, word)| json!([id, start, word])).collect(); - d.fields.insert("skipped".into(), json!(arr)); +/// Runs the action the dialog filed, then hands it the word the walk shows now. Called every frame +/// by the dialog host; the host's lock is not held while the document is edited. +pub(crate) fn pump(app: &mut VectorcraftApp, ctx: &egui::Context) { + let Some(mut w) = app.spelling.take() else { return }; + // PLAN: assumed `app.dialogs() -> Option` is an owned handle, not a borrow of `app` + let Some(dialogs) = app.dialogs() else { + app.spelling = Some(w); + return; + }; + // Done, Escape and a close from outside all end the walk. + if !dialogs.with(|host| host.is_open::()) { + dialogs.with(|host| { + let _ = host.take::(); + }); + return; + } + if check_doc_uid(app, &w).is_err() { + dialogs.with(|host| host.cancel::(ctx)); + return; + } + let filed = dialogs.with(|host| host.get_mut::().map(|d| (d.take_action(), d.all_layers_checked()))); + let Some((Some(action), all_layers)) = filed else { + app.spelling = Some(w); + return; + }; + let res = match action { + SpellingAction::Ignore(_) => ignore(app, &mut w), + SpellingAction::Relist if all_layers != w.layer.is_none() => { + w.layer = if all_layers { None } else { w.shown.as_ref().map(|s| s.0) }; + advance(app, &mut w, None, 1) + } + SpellingAction::Relist => ignore_all(app, &mut w), + SpellingAction::Change { to, .. } => change(app, &mut w, &to), + SpellingAction::ChangeAll { to, .. } => change_all(app, &mut w, &to), + SpellingAction::Add(_) => add_to_dictionary(app, &mut w), + _ => Ok(()), + }; + if let Err(e) = res { + app.status(&e); + } + let items = items_of(app, &w).unwrap_or_else(|e| { + app.status(&e); + Vec::new() + }); + dialogs.with(|host| { + if let Some(d) = host.get_mut::() { + d.restart(items); + } + }); + app.spelling = Some(w); } /// Whether Ignore skipped this occurrence of a word. @@ -133,12 +152,11 @@ fn is_skipped(skipped: &BTreeSet, w: &Word) -> bool { /// Moves the Ignore positions in `id` that come after edits there by what each edit changed in /// length: `edits` are the (start, end) bytes each replaced with `new_len` bytes. -fn shift_skipped(d: &mut Dialog, id: u64, edits: &[(usize, usize)], new_len: usize) { - let skipped = get_skipped(d) +fn shift_skipped(w: &mut SpellingWalk, id: u64, edits: &[(usize, usize)], new_len: usize) { + w.skipped = std::mem::take(&mut w.skipped) .into_iter() .map(|(sid, start, word)| if sid == id { (sid, shifted(start as usize, edits, new_len) as u64, word) } else { (sid, start, word) }) .collect(); - set_skipped(d, &skipped); } /// Where byte `at` of a text ends up after `edits` ((start, end) bytes, none of them overlapping @@ -234,25 +252,6 @@ pub fn extract_sentence_context(text: &str, range: Range) -> (String, Str (before, word, after) } -fn set_done(d: &mut Dialog) { - d.fields.insert("done".into(), json!(true)); - d.fields.insert("not_in_dictionary".into(), json!("")); - d.fields.insert("context".into(), json!(tl!("The spell check is complete."))); - d.fields.insert("context_before".into(), json!("")); - d.fields.insert("context_word".into(), json!("")); - d.fields.insert("context_after".into(), json!("")); - d.fields.insert("suggestions".into(), json!([])); - d.fields.insert("selected_suggestion".into(), Value::Null); - d.fields.insert("change_to".into(), json!("")); - d.fields.insert("node_id".into(), Value::Null); - d.fields.insert("start".into(), Value::Null); - d.fields.insert("end".into(), Value::Null); - d.fields.insert("lang".into(), Value::Null); -} - -/// One unknown word: (id, start, end, word, lang). -type Word = (u64, usize, usize, String, String); - /// One word of a `text.spelling.check` result. fn word_at(w: &Value) -> Option { let id = w.get("id")?.as_u64()?; @@ -263,32 +262,16 @@ fn word_at(w: &Value) -> Option { Some((id, start, end, word, lang)) } -/// One word of `queue`, kept as `[id, start, end, word, lang]`. -fn queued_word(w: &Value) -> Option { - match w.as_array().map(Vec::as_slice) { - Some([id, start, end, word, lang]) => Some(( - id.as_u64()?, - usize::try_from(start.as_u64()?).ok()?, - usize::try_from(end.as_u64()?).ok()?, - word.as_str()?.to_string(), - lang.as_str()?.to_string(), - )), - _ => None, - } -} - -/// The check's params for the dialog's scope, going on from `from` ((id, byte)) when given. A +/// The check's params for the walk's scope, going on from `from` ((id, byte)) when given. A /// selected object that another command deleted is left out, so the rest of the selection is still /// checked; with every one deleted, the list is empty and the check finds nothing (not the whole /// document, which `ids` left out would check). -fn check_params(app: &VectorcraftApp, d: &Dialog, from: Option<(u64, usize)>, max: usize) -> Value { - let ids = match d.fields.get("ids") { - Some(Value::Array(ids)) => { - let exists = - |id: &Value| id.as_u64().is_some_and(|id| app.session.doc().is_ok_and(|st| st.doc.node(vectorcraft_doc::NodeId(id)).is_some())); - Value::Array(ids.iter().filter(|id| exists(id)).cloned().collect()) - } - _ => Value::Null, +fn check_params(app: &VectorcraftApp, w: &SpellingWalk, from: Option<(u64, usize)>, max: usize) -> Value { + let exists = |id: &u64| app.session.doc().is_ok_and(|st| st.doc.node(vectorcraft_doc::NodeId(*id)).is_some()); + let scope = w.layer.map(|layer| vec![layer]).or_else(|| w.ids.clone()); + let ids = match scope { + Some(ids) => Value::Array(ids.iter().filter(|id| exists(id)).map(|&id| json!(id)).collect()), + None => Value::Null, }; match from { Some((id, byte)) => json!({ "ids": ids, "from": { "id": id, "byte": byte }, "max": max }), @@ -300,10 +283,10 @@ fn check_params(app: &VectorcraftApp, d: &Dialog, from: Option<(u64, usize)>, ma /// words, and shows the first one Ignore did not skip, keeping the rest in `queue`; completes the /// spell check when there is none. Past a check whose words were all skipped it lists [`page`] /// words at a time. -pub fn advance(app: &mut VectorcraftApp, d: &mut Dialog, mut from: Option<(u64, usize)>, mut max: usize) -> Result<(), String> { - let skipped = get_skipped(d); +fn advance(app: &mut VectorcraftApp, w: &mut SpellingWalk, mut from: Option<(u64, usize)>, mut max: usize) -> Result<(), String> { + let skipped = w.skipped.clone(); loop { - let params = check_params(app, d, from, max); + let params = check_params(app, w, from, max); let check_res = match app.run("text.spelling.check", params) { Ok(r) => r, // The object `from` names left the scope (another command hid, locked or deleted @@ -318,14 +301,13 @@ pub fn advance(app: &mut VectorcraftApp, d: &mut Dialog, mut from: Option<(u64, let words: Vec = check_res.get("words").and_then(Value::as_array).map(|a| a.iter().filter_map(word_at).collect()).unwrap_or_default(); let more = check_res.get("more").and_then(Value::as_bool).unwrap_or(false); let next_from = words.last().map(|w| (w.0, w.2)); - let mut rest = words.into_iter().filter(|w| !is_skipped(&skipped, w)); - if let Some(w) = rest.next() { - let mut queue: Vec = rest.map(|(id, start, end, word, lang)| json!([id, start, end, word, lang])).collect(); - queue.reverse(); - d.fields.insert("queue".into(), Value::Array(queue)); - d.fields.insert("queue_more".into(), json!(more)); - d.fields.insert("queue_from".into(), json!(next_from.map(|(id, byte)| [id, byte as u64]))); - return show(app, d, w); + let mut rest = words.into_iter().filter(|word| !is_skipped(&skipped, word)); + if let Some(word) = rest.next() { + w.queue = rest.collect(); + w.queue.reverse(); + w.queue_more = more; + w.queue_from = next_from; + return show(app, w, word); } match next_from { Some(f) if more => { @@ -333,7 +315,7 @@ pub fn advance(app: &mut VectorcraftApp, d: &mut Dialog, mut from: Option<(u64, max = page(); } _ => { - set_done(d); + w.shown = None; return Ok(()); } } @@ -344,25 +326,17 @@ pub fn advance(app: &mut VectorcraftApp, d: &mut Dialog, mut from: Option<(u64, /// with a new check from `queue_from` when the queue runs out. The queue holds what a check listed /// at the document revision of the word shown, so callers use it only while [`resume_at`] gives /// Some. -fn step(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - let skipped = get_skipped(d); - loop { - let next = d.fields.get_mut("queue").and_then(Value::as_array_mut).and_then(Vec::pop); - let Some(next) = next else { break }; - let Some(w) = queued_word(&next) else { continue }; - if is_skipped(&skipped, &w) || ignored_all(app, &w.3) { +fn step(app: &mut VectorcraftApp, w: &mut SpellingWalk) -> Result<(), String> { + while let Some(word) = w.queue.pop() { + if is_skipped(&w.skipped, &word) || ignored_all(app, &word.3) { continue; } - return show(app, d, w); + return show(app, w, word); } - let from = d.fields.get("queue_from").and_then(Value::as_array).and_then(|a| match a.as_slice() { - [id, byte] => Some((id.as_u64()?, usize::try_from(byte.as_u64()?).ok()?)), - _ => None, - }); - match from { - Some(from) if d.bool("queue_more") => advance(app, d, Some(from), page()), + match w.queue_from { + Some(from) if w.queue_more => advance(app, w, Some(from), page()), _ => { - set_done(d); + w.shown = None; Ok(()) } } @@ -374,49 +348,43 @@ fn ignored_all(app: &VectorcraftApp, word: &str) -> bool { app.session.doc().is_ok_and(|st| st.spelling_ignored.contains(&w)) } -/// Shows `w` with its sentence and suggestions. -fn show(app: &mut VectorcraftApp, d: &mut Dialog, w: Word) -> Result<(), String> { - let (id, start, end, word, lang) = w; - let (before, target, after) = { +/// Makes `word` the word shown. +fn show(app: &VectorcraftApp, w: &mut SpellingWalk, word: Word) -> Result<(), String> { + w.shown = Some(word); + remember_revision(app, w); + Ok(()) +} + +/// The dialog's item for `word`: its sentence and suggestions. Suggestions are asked once per +/// word shown. +fn item(app: &mut VectorcraftApp, word: &Word) -> Result { + let (id, start, end, text, lang) = word; + let (before, _, after) = { let st = app.session.doc().map_err(|e| e.to_string())?; - if let Some(vectorcraft_doc::NodeKind::Text(t)) = st.doc.node(vectorcraft_doc::NodeId(id)).map(|n| &n.kind) { - extract_sentence_context(&t.plain_text(), start..end) + if let Some(vectorcraft_doc::NodeKind::Text(t)) = st.doc.node(vectorcraft_doc::NodeId(*id)).map(|n| &n.kind) { + extract_sentence_context(&t.plain_text(), *start..*end) } else { - (String::new(), word.clone(), String::new()) + (String::new(), String::new(), String::new()) } }; - - let sugg_res = app.run("text.spelling.suggest", json!({ "word": word, "lang": lang, "max": 10 })).map_err(|e| e.to_string())?; + let sugg_res = app.run("text.spelling.suggest", json!({ "word": text, "lang": lang, "max": 10 })).map_err(|e| e.to_string())?; let suggestions: Vec = sugg_res .get("suggestions") .and_then(Value::as_array) .map(|a| a.iter().filter_map(Value::as_str).map(String::from).collect()) .unwrap_or_default(); + Ok(SpellingItem::new(*id, *start..*end, text.clone(), suggestions).context(before, after)) +} - let change_to = suggestions.first().cloned().unwrap_or_else(|| word.clone()); - let full_context = format!("{before}{target}{after}"); - - d.fields.insert("not_in_dictionary".into(), json!(word)); - d.fields.insert("context".into(), json!(full_context)); - d.fields.insert("context_before".into(), json!(before)); - d.fields.insert("context_word".into(), json!(target)); - d.fields.insert("context_after".into(), json!(after)); - d.fields.insert("suggestions".into(), json!(suggestions)); - d.fields.insert("selected_suggestion".into(), json!(suggestions.first().cloned())); - d.fields.insert("change_to".into(), json!(change_to)); - d.fields.insert("node_id".into(), json!(id)); - d.fields.insert("start".into(), json!(start)); - d.fields.insert("end".into(), json!(end)); - d.fields.insert("lang".into(), json!(lang)); - d.fields.insert("done".into(), json!(false)); - remember_revision(app, d); - Ok(()) +/// The dialog's items for the walk: the word shown, or none when the check is complete. +fn items_of(app: &mut VectorcraftApp, w: &SpellingWalk) -> Result, String> { + w.shown.as_ref().map(|word| item(app, word)).transpose().map(|i| i.into_iter().collect()) } /// Notes the document's revision and the custom dictionary's generation as of the word shown, /// which [`resume_at`] compares. -fn remember_revision(app: &VectorcraftApp, d: &mut Dialog) { - d.fields.insert("revision".into(), json!(revision(app))); +fn remember_revision(app: &VectorcraftApp, w: &mut SpellingWalk) { + w.revision = revision(app); } /// The document's revision and the custom dictionary's generation: when either moves, the words @@ -426,125 +394,102 @@ fn revision(app: &VectorcraftApp) -> Option<[u64; 2]> { Some([doc, vectorcraft_engine::cmd::spelling::dictionary_generation()]) } -/// The word shown: (id, start, end). -fn current(d: &Dialog) -> Option<(u64, usize, usize)> { - let id = d.fields.get("node_id").and_then(Value::as_u64)?; - let start = usize::try_from(d.fields.get("start").and_then(Value::as_u64)?).ok()?; - let end = usize::try_from(d.fields.get("end").and_then(Value::as_u64)?).ok()?; - Some((id, start, end)) -} - /// Where the next check goes on from: the start of the word shown. When another command changed /// the document or the custom dictionary since the word was shown, the text before the word may /// hold new unknown words, and `queue` may be out of date, so the check starts the scope again /// (None); [`advance`] still skips the occurrences Ignore skipped. -fn resume_at(app: &VectorcraftApp, d: &Dialog) -> Option<(u64, usize)> { +fn resume_at(app: &VectorcraftApp, w: &SpellingWalk) -> Option<(u64, usize)> { let now = revision(app)?; - if d.fields.get("revision") != Some(&json!(now)) { + if w.revision != Some(now) { return None; } - let (id, start, _, true) = shown_word(app, d)? else { return None }; + let (id, start, _, true) = shown_word(app, w)? else { return None }; Some((id, start)) } /// The word shown, (id, start, end), and whether its object still holds it at those bytes; None /// when there is none or its object is no longer type. -fn shown_word(app: &VectorcraftApp, d: &Dialog) -> Option<(u64, usize, usize, bool)> { - let (id, start, end) = current(d)?; +fn shown_word(app: &VectorcraftApp, w: &SpellingWalk) -> Option<(u64, usize, usize, bool)> { + let (id, start, end, word, _) = w.shown.as_ref()?; let st = app.session.doc().ok()?; - let Some(vectorcraft_doc::NodeKind::Text(t)) = st.doc.node(vectorcraft_doc::NodeId(id)).map(|n| &n.kind) else { return None }; - let word = d.str("not_in_dictionary"); - let still_there = !word.is_empty() && t.plain_text().get(start..end).is_some_and(|w| w == word); - Some((id, start, end, still_there)) + let Some(vectorcraft_doc::NodeKind::Text(t)) = st.doc.node(vectorcraft_doc::NodeId(*id)).map(|n| &n.kind) else { return None }; + let still_there = !word.is_empty() && t.plain_text().get(*start..*end).is_some_and(|text| text == word); + Some((*id, *start, *end, still_there)) } -fn check_doc_uid(app: &VectorcraftApp, d: &Dialog) -> Result<(), String> { - let doc_uid = d.fields.get("doc_uid").and_then(Value::as_u64); - let active_uid = app.session.active().map(|st| st.uid); - if doc_uid.is_some() && doc_uid != active_uid { +/// Fails when the active document is not the one the walk started on. +fn check_doc_uid(app: &VectorcraftApp, w: &SpellingWalk) -> Result<(), String> { + if app.session.active().map(|st| st.uid) != Some(w.doc) { return Err("the document was switched".into()); } Ok(()) } -fn ignore(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - check_doc_uid(app, d)?; - if d.bool("done") { - return Ok(()); +/// The word shown, when the walk is still going and on the walk's document. +fn shown_text(app: &VectorcraftApp, w: &SpellingWalk) -> Result, String> { + check_doc_uid(app, w)?; + Ok(w.shown.as_ref().map(|s| s.3.clone())) +} + +fn ignore(app: &mut VectorcraftApp, w: &mut SpellingWalk) -> Result<(), String> { + let Some(word) = shown_text(app, w)? else { return Ok(()) }; + let unchanged = resume_at(app, w).is_some(); + if let Some((id, start, _, true)) = shown_word(app, w) { + w.skipped.insert((id, start as u64, word)); } - let unchanged = resume_at(app, d).is_some(); - if let Some((id, start, _, true)) = shown_word(app, d) { - let mut skipped = get_skipped(d); - skipped.insert((id, start as u64, d.str("not_in_dictionary"))); - set_skipped(d, &skipped); - } - go_on(app, d, unchanged) + go_on(app, w, unchanged) } /// On past the word shown: the next word of the queue while nothing changed since it was shown, /// otherwise the scope checked again from its start. -fn go_on(app: &mut VectorcraftApp, d: &mut Dialog, unchanged: bool) -> Result<(), String> { - if unchanged { step(app, d) } else { advance(app, d, None, page()) } +fn go_on(app: &mut VectorcraftApp, w: &mut SpellingWalk, unchanged: bool) -> Result<(), String> { + if unchanged { step(app, w) } else { advance(app, w, None, page()) } } -fn ignore_all(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - check_doc_uid(app, d)?; - if d.bool("done") { - return Ok(()); - } - let unchanged = resume_at(app, d).is_some(); - let word = d.str("not_in_dictionary"); +fn ignore_all(app: &mut VectorcraftApp, w: &mut SpellingWalk) -> Result<(), String> { + let Some(word) = shown_text(app, w)? else { return Ok(()) }; + let unchanged = resume_at(app, w).is_some(); if !word.is_empty() { app.run("text.spelling.ignoreAll", json!({ "word": word })).map_err(|e| e.to_string())?; } // The queue's other occurrences of the word are passed over too. - go_on(app, d, unchanged) + go_on(app, w, unchanged) } -fn change(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - check_doc_uid(app, d)?; - if d.bool("done") { - return Ok(()); - } - let from = resume_at(app, d); - let change_to = d.str("change_to"); +fn change(app: &mut VectorcraftApp, w: &mut SpellingWalk, to: &str) -> Result<(), String> { + let Some(word) = shown_text(app, w)? else { return Ok(()) }; + let from = resume_at(app, w); // Changed only while the word is still at the bytes shown. - let Some((id, start, end, true)) = shown_word(app, d) else { return advance(app, d, None, page()) }; - if change_to.trim().is_empty() { - return recheck(app, d, from); + let Some((id, start, end, true)) = shown_word(app, w) else { return advance(app, w, None, page()) }; + if to.trim().is_empty() { + return recheck(app, w, from); } - if change_to == d.str("not_in_dictionary") { + if to == word { // Nothing to change, so no undo step: on past the word. - return go_on(app, d, from.is_some()); + return go_on(app, w, from.is_some()); } - app.run("text.editRange", json!({ "id": id, "start": start, "end": end, "insert": change_to })).map_err(|e| e.to_string())?; - shift_skipped(d, id, &[(start, end)], change_to.len()); + app.run("text.editRange", json!({ "id": id, "start": start, "end": end, "insert": to })).map_err(|e| e.to_string())?; + shift_skipped(w, id, &[(start, end)], to.len()); // On from the replacement, which is shown when the dictionaries don't know it either (or // from the start of the scope, when the document changed under the dialog). - recheck(app, d, from) + recheck(app, w, from) } /// A new check after a change: on from `from` until the next unknown word, or the scope from its /// start when `from` is None. -fn recheck(app: &mut VectorcraftApp, d: &mut Dialog, from: Option<(u64, usize)>) -> Result<(), String> { +fn recheck(app: &mut VectorcraftApp, w: &mut SpellingWalk, from: Option<(u64, usize)>) -> Result<(), String> { match from { - Some(from) => advance(app, d, Some(from), 1), - None => advance(app, d, None, page()), + Some(from) => advance(app, w, Some(from), 1), + None => advance(app, w, None, page()), } } -fn change_all(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - check_doc_uid(app, d)?; - if d.bool("done") { - return Ok(()); +fn change_all(app: &mut VectorcraftApp, w: &mut SpellingWalk, to: &str) -> Result<(), String> { + let Some(word) = shown_text(app, w)? else { return Ok(()) }; + let here = resume_at(app, w); + if word.is_empty() || to.trim().is_empty() { + return recheck(app, w, here); } - let word = d.str("not_in_dictionary"); - let change_to = d.str("change_to"); - let here = resume_at(app, d); - if word.is_empty() || change_to.trim().is_empty() { - return recheck(app, d, here); - } - let skipped = get_skipped(d); // Every occurrence in the scope, found before anything changes, so the bytes each check gives // stay true: one check after another, each going on from the end of the last word listed, @@ -552,14 +497,14 @@ fn change_all(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { let mut by_node: BTreeMap> = BTreeMap::new(); let mut from = None; loop { - let params = check_params(app, d, from, page()); + let params = check_params(app, w, from, page()); let check_res = app.run("text.spelling.check", params).map_err(|e| e.to_string())?; let words: Vec = check_res.get("words").and_then(Value::as_array).map(|a| a.iter().filter_map(word_at).collect()).unwrap_or_default(); let more = check_res.get("more").and_then(Value::as_bool).unwrap_or(false); - for w in &words { + for found in &words { // An occurrence that already reads as the replacement is left alone: no edit, no undo step. - if w.3.eq_ignore_ascii_case(&word) && w.3 != change_to && !is_skipped(&skipped, w) { - by_node.entry(w.0).or_default().push((w.1, w.2)); + if found.3.eq_ignore_ascii_case(&word) && found.3 != to && !is_skipped(&w.skipped, found) { + by_node.entry(found.0).or_default().push((found.1, found.2)); } } match words.last() { @@ -570,211 +515,35 @@ fn change_all(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { if by_node.is_empty() { // Nothing to change: on past the word shown, as Change does. - return go_on(app, d, here.is_some()); + return go_on(app, w, here.is_some()); } // Every edit in one command: one undo step, each object laid out once. let words: Vec = by_node.iter().flat_map(|(&id, ranges)| ranges.iter().map(move |&(start, end)| json!({ "id": id, "start": start, "end": end }))).collect(); - app.run("text.spelling.change", json!({ "to": change_to, "words": words })).map_err(|e| e.to_string())?; + app.run("text.spelling.change", json!({ "to": to, "words": words })).map_err(|e| e.to_string())?; for (&id, ranges) in &by_node { - shift_skipped(d, id, ranges, change_to.len()); + shift_skipped(w, id, ranges, to.len()); } // On from the shown word's replacement, where it moved to. - let from = here.map(|(id, start)| (id, by_node.get(&id).map_or(start, |ranges| shifted(start, ranges, change_to.len())))); - recheck(app, d, from) + let from = here.map(|(id, start)| (id, by_node.get(&id).map_or(start, |ranges| shifted(start, ranges, to.len())))); + recheck(app, w, from) } -fn add_to_dictionary(app: &mut VectorcraftApp, d: &mut Dialog) -> Result<(), String> { - check_doc_uid(app, d)?; - if d.bool("done") { - return Ok(()); - } +fn add_to_dictionary(app: &mut VectorcraftApp, w: &mut SpellingWalk) -> Result<(), String> { + let Some(word) = shown_text(app, w)? else { return Ok(()) }; // The check takes the word as right from now on, so it goes on from where the word starts. - let from = resume_at(app, d); - let word = d.str("not_in_dictionary"); + let from = resume_at(app, w); if !word.is_empty() { app.run("text.spelling.addWord", json!({ "word": word })).map_err(|e| e.to_string())?; } - recheck(app, d, from) -} - -fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> DialogAction { - let doc_uid = d.fields.get("doc_uid").and_then(Value::as_u64); - let active_uid = app.session.active().map(|st| st.uid); - if doc_uid.is_some() && doc_uid != active_uid { - return DialogAction::Cancel; - } - - if let Some(act) = d.fields.remove("action").and_then(|v| v.as_str().map(String::from)) { - let res = match act.as_str() { - "ignore" => ignore(app, d), - "ignoreAll" => ignore_all(app, d), - "change" => change(app, d), - "changeAll" => change_all(app, d), - "addToDictionary" => add_to_dictionary(app, d), - "done" | "cancel" => return DialogAction::Cancel, - _ => Ok(()), - }; - if let Err(e) = res { - app.status(&e); - d.fields.insert("error".into(), json!(e)); - } else { - d.fields.remove("error"); - } - } - - let mut action_to_run = None; - let done = d.bool("done"); - let before = d.str("context_before"); - let target = d.str("context_word"); - let after = d.str("context_after"); - let mut change_to = d.str("change_to"); - let suggestions: Vec = d - .fields - .get("suggestions") - .and_then(Value::as_array) - .map(|a| a.iter().filter_map(Value::as_str).map(String::from).collect()) - .unwrap_or_default(); - - let can_change = !done && !change_to.trim().is_empty(); - - if crate::shortcuts::pressed_anew(ui.ctx(), egui::Key::Enter) - || ui.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { key: egui::Key::Enter, pressed: true, repeat: false, .. }))) - { - if can_change { - action_to_run = Some(Action::Change); - } else if done { - return DialogAction::Cancel; - } - } - if ui.input(|i| i.key_pressed(egui::Key::Escape)) { - return DialogAction::Cancel; - } - - let action_col_width = 110.0; - let col_spacing = 16.0; - let text_col_width = (ui.available_width() - action_col_width - col_spacing).clamp(200.0, 320.0); - - ui.horizontal_top(|ui| { - ui.vertical(|ui| { - ui.set_width(text_col_width); - if done { - ui.add_space(20.0); - widgets::dim_label(ui, tl!("The spell check is complete.")); - ui.add_space(80.0); - } else { - widgets::dim_label(ui, tl!("Not in Dictionary:")); - ui.group(|ui| { - ui.set_width(text_col_width); - ui.horizontal_wrapped(|ui| { - ui.spacing_mut().item_spacing.x = 0.0; - if !before.is_empty() { - ui.label(&before); - } - let t = Tokens::get(ui.ctx()); - ui.add(egui::Label::new(egui::RichText::new(&target).color(t.accent).strong().underline())); - if !after.is_empty() { - ui.label(&after); - } - }); - }); - - ui.add_space(8.0); - widgets::dim_label(ui, tl!("Change to:")); - let field_id = ui.scope_id().with(("dlg-text", "change_to")); - widgets::take_dialog_focus(ui, field_id, &change_to); - if ui.add(egui::TextEdit::singleline(&mut change_to).id(field_id).desired_width(text_col_width)).changed() { - d.fields.insert("change_to".into(), json!(change_to)); - } - - ui.add_space(8.0); - widgets::dim_label(ui, tl!("Suggestions:")); - widgets::list_box(ui, |ui| { - ui.set_width(text_col_width); - let row = ui.spacing().interact_size.y; - egui::ScrollArea::vertical().id_salt("check-spelling-suggestions").min_scrolled_height(140.0).max_height(140.0).show_rows( - ui, - row, - suggestions.len(), - |ui, range| { - ui.set_width(ui.available_width()); - for s in suggestions.get(range).unwrap_or_default() { - let is_selected = change_to == *s; - if ui.selectable_label(is_selected, s.as_str()).clicked() { - d.fields.insert("change_to".into(), json!(s)); - d.fields.insert("selected_suggestion".into(), json!(s)); - } - } - }, - ); - }); - } - }); - - ui.add_space(col_spacing); - - ui.vertical(|ui| { - let w = action_col_width; - if ui.add_enabled_ui(!done, |ui| widgets::flat_button_fit(ui, tl!("Ignore"), w).clicked()).inner { - action_to_run = Some(Action::Ignore); - } - ui.add_space(6.0); - if ui.add_enabled_ui(!done, |ui| widgets::flat_button_fit(ui, tl!("Ignore All"), w).clicked()).inner { - action_to_run = Some(Action::IgnoreAll); - } - ui.add_space(6.0); - if ui.add_enabled_ui(can_change, |ui| widgets::flat_button_fit(ui, tl!("Change"), w).clicked()).inner { - action_to_run = Some(Action::Change); - } - ui.add_space(6.0); - if ui.add_enabled_ui(can_change, |ui| widgets::flat_button_fit(ui, tl!("Change All"), w).clicked()).inner { - action_to_run = Some(Action::ChangeAll); - } - ui.add_space(6.0); - if ui.add_enabled_ui(!done, |ui| widgets::flat_button_fit(ui, tl!("Add to Dictionary"), w).clicked()).inner { - action_to_run = Some(Action::AddToDictionary); - } - ui.add_space(16.0); - if widgets::flat_button_fit(ui, tl!("Done"), w).clicked() { - action_to_run = Some(Action::Done); - } - ui.add_space(6.0); - if widgets::flat_button_fit(ui, tl!("Cancel"), w).clicked() { - action_to_run = Some(Action::Cancel); - } - }); - }); - - if let Some(act) = action_to_run { - let res = match act { - Action::Ignore => ignore(app, d), - Action::IgnoreAll => ignore_all(app, d), - Action::Change => change(app, d), - Action::ChangeAll => change_all(app, d), - Action::AddToDictionary => add_to_dictionary(app, d), - Action::Done | Action::Cancel => return DialogAction::Cancel, - }; - if let Err(e) = res { - app.status(&e); - d.fields.insert("error".into(), json!(e)); - } else { - d.fields.remove("error"); - } - } - - DialogAction::None -} - -fn confirm(app: &mut VectorcraftApp, _: &Dialog) -> DialogResult { - app.ui.dialog = None; - Ok(Value::Null) + recheck(app, w, from) } #[cfg(test)] mod tests { - use egui::{Event, PointerButton, Pos2, Rect, vec2}; use serde_json::json; + use super::SpellingWalk; use crate::VectorcraftApp; const WORD_A: &str = "zqvspella"; @@ -784,10 +553,17 @@ mod tests { const WORD_D: &str = "zqvspelld"; thread_local! { - /// The most words one Change All check lists in this test thread ([`super::change_all_page`]). + /// The most words one Change All check lists in this test thread ([`super::page`]). pub(super) static PAGE: std::cell::Cell = const { std::cell::Cell::new(vectorcraft_engine::cmd::spelling::MAX_WORDS) }; } + type Step = fn(&mut VectorcraftApp, &mut SpellingWalk) -> Result<(), String>; + + /// Ignore, Ignore All, Change and Change All (both to "fixed"), and Add to Dictionary. + fn steps() -> [Step; 5] { + [super::ignore, super::ignore_all, |a, w| super::change(a, w, "fixed"), |a, w| super::change_all(a, w, "fixed"), super::add_to_dictionary] + } + fn plain(app: &VectorcraftApp, id: u64) -> String { match &app.session.doc().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind { vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), @@ -795,16 +571,26 @@ mod tests { } } - /// The word the dialog shows: (id, start, word). - fn shown(app: &VectorcraftApp) -> (u64, u64, String) { - let d = app.ui.dialog.as_ref().unwrap(); - (d.fields["node_id"].as_u64().unwrap(), d.fields["start"].as_u64().unwrap(), d.str("not_in_dictionary")) + /// Starts the walk on the app, as `open` does without a window. + fn begin(app: &mut VectorcraftApp) { + app.spelling = Some(super::start(app).unwrap()); } - fn act(app: &mut VectorcraftApp, f: fn(&mut VectorcraftApp, &mut crate::state::Dialog) -> Result<(), String>) { - let mut d = app.ui.dialog.take().unwrap(); - f(app, &mut d).unwrap(); - app.ui.dialog = Some(d); + /// The word shown: (id, start, word). + fn shown(app: &VectorcraftApp) -> (u64, u64, String) { + let (id, start, _, word, _) = app.spelling.as_ref().unwrap().shown.clone().unwrap(); + (id, start as u64, word) + } + + /// Whether the walk has no word to show. + fn done(app: &VectorcraftApp) -> bool { + app.spelling.as_ref().unwrap().shown.is_none() + } + + fn act(app: &mut VectorcraftApp, f: impl FnOnce(&mut VectorcraftApp, &mut SpellingWalk) -> Result<(), String>) { + let mut w = app.spelling.take().unwrap(); + f(app, &mut w).unwrap(); + app.spelling = Some(w); } #[test] @@ -813,17 +599,17 @@ mod tests { let id = text(&mut app, 50.0, &format!("{WORD_B} one {WORD_B} two {WORD_B}")); let other = text(&mut app, 100.0, &format!("then {WORD_A}")); app.run("select.none", json!({})).unwrap(); - super::open(&mut app).unwrap(); + begin(&mut app); // Each step asks for one word on from the last, so every occurrence in the object is shown // in turn before the next object's. let mut seen = vec![shown(&app)]; for _ in 0..4 { act(&mut app, super::ignore); - if !app.ui.dialog.as_ref().unwrap().bool("done") { + if !done(&app) { seen.push(shown(&app)); } } - assert!(app.ui.dialog.as_ref().unwrap().bool("done")); + assert!(done(&app)); let b = WORD_B.len() as u64; let want = vec![(id, 0, WORD_B.to_string()), (id, b + 5, WORD_B.to_string()), (id, 2 * b + 10, WORD_B.to_string()), (other, 5, WORD_A.to_string())]; @@ -841,23 +627,22 @@ mod tests { app.run("select.set", json!({ "ids": [group] })).unwrap(); PAGE.with(|p| p.set(1)); - super::open(&mut app).unwrap(); + begin(&mut app); let mut seen = vec![shown(&app)]; for _ in 0..5 { act(&mut app, super::ignore); - if !app.ui.dialog.as_ref().unwrap().bool("done") { + if !done(&app) { seen.push(shown(&app)); } } - assert!(app.ui.dialog.as_ref().unwrap().bool("done")); + assert!(done(&app)); let ids: Vec<(u64, String)> = seen.into_iter().map(|(id, _, w)| (id, w)).collect(); let want = [(a, WORD_B), (a, WORD_D), (a, WORD_B), (b, WORD_D), (b, WORD_B)].map(|(id, w)| (id, w.to_string())); assert_eq!(ids, want, "the group's descendants, one word at a time; not the object outside it"); // Change All goes through the group one listed word at a time too. - super::open(&mut app).unwrap(); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("fixed")); - act(&mut app, super::change_all); + begin(&mut app); + act(&mut app, |a, w| super::change_all(a, w, "fixed")); assert_eq!(plain(&app, a), format!("fixed {WORD_D} fixed")); assert_eq!(plain(&app, b), format!("{WORD_D} fixed")); assert_eq!(plain(&app, outside), format!("{WORD_B} outside"), "outside the selection"); @@ -869,14 +654,13 @@ mod tests { // The second WORD_A comes after a whole check's worth of other unknown words. PAGE.with(|p| p.set(2)); let id = text(&mut app, 50.0, &format!("{WORD_A} {WORD_B} {WORD_D} {WORD_A} {WORD_B} {WORD_A}")); - super::open(&mut app).unwrap(); + begin(&mut app); // Ignore the first WORD_A, then change all from the second. act(&mut app, super::ignore); act(&mut app, super::ignore); act(&mut app, super::ignore); assert_eq!(shown(&app).2, WORD_A); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("fixed")); - act(&mut app, super::change_all); + act(&mut app, |a, w| super::change_all(a, w, "fixed")); assert_eq!(plain(&app, id), format!("{WORD_A} {WORD_B} {WORD_D} fixed {WORD_B} fixed"), "the ignored one stays"); // On from the replacement: the WORD_B after it. assert_eq!(shown(&app).2, WORD_B); @@ -892,10 +676,9 @@ mod tests { // type object. let others = vec![WORD_B; MAX_WORDS].join(" "); let id = text(&mut app, 50.0, &format!("{WORD_A} {others} {WORD_A}")); - super::open(&mut app).unwrap(); + begin(&mut app); assert_eq!(shown(&app), (id, 0, WORD_A.to_string())); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("fixed")); - act(&mut app, super::change_all); + act(&mut app, |a, w| super::change_all(a, w, "fixed")); assert_eq!(plain(&app, id), format!("fixed {others} fixed")); app.run("edit.undo", json!({})).unwrap(); assert_eq!(plain(&app, id), format!("{WORD_A} {others} {WORD_A}"), "one undo step"); @@ -905,14 +688,13 @@ mod tests { fn text_changed_under_the_dialog_is_checked_again_from_its_start() { // A word of its own: Add to Dictionary puts it in the custom dictionary the process shares. const STALE: &str = "zqvstaleword"; - for f in [super::ignore, super::ignore_all, super::change, super::change_all, super::add_to_dictionary] { + for f in steps() { let mut app = app(); let id = text(&mut app, 50.0, &format!("hello {STALE}")); - super::open(&mut app).unwrap(); + begin(&mut app); assert_eq!(shown(&app), (id, 6, STALE.to_string())); // Another command replaces the text with a shorter one: the shown bytes are gone. app.run("text.editRange", json!({ "id": id, "start": 0, "end": 6 + STALE.len(), "insert": WORD_D })).unwrap(); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("fixed")); act(&mut app, f); forget(&mut app, &[STALE]); assert_eq!(plain(&app, id), WORD_D, "nothing is changed at stale bytes"); @@ -924,7 +706,7 @@ mod tests { fn a_restarted_check_still_skips_the_ignored_occurrences() { let mut app = app(); let id = text(&mut app, 50.0, &format!("{WORD_A} {WORD_B} {WORD_A}")); - super::open(&mut app).unwrap(); + begin(&mut app); act(&mut app, super::ignore); assert_eq!(shown(&app), (id, 10, WORD_B.to_string())); // Another command changes the word shown, so the next step checks the object from its start. @@ -939,24 +721,18 @@ mod tests { // FIRST has as many bytes as "different", so the second word stays at the bytes shown. const FIRST: &str = "zqvbckone"; const SECOND: &str = "zqvbacktwo"; - for f in [super::ignore, super::ignore_all, super::change, super::change_all, super::add_to_dictionary] { + for f in steps() { let mut app = app(); let id = text(&mut app, 50.0, &format!("{FIRST} {SECOND}")); - super::open(&mut app).unwrap(); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("different")); - act(&mut app, super::change); - // Drawing the dialog leaves the document's revision alone, so the next step goes on - // from the word shown rather than starting again. - let ctx = context(); - frame(&mut app, &ctx, vec![]); - frame(&mut app, &ctx, vec![]); - let d = app.ui.dialog.as_ref().unwrap(); - assert_eq!(d.fields["revision"][0], json!(app.session.doc().unwrap().revision)); + begin(&mut app); + act(&mut app, |a, w| super::change(a, w, "different")); + // The walk remembers the revision as of the word shown, so the next step goes on from + // it rather than starting again. + assert_eq!(app.spelling.as_ref().unwrap().revision.unwrap()[0], app.session.doc().unwrap().revision); assert_eq!(shown(&app), (id, 10, SECOND.to_string())); // Another command (Undo) brings the first word back before the one shown. app.run("edit.undo", json!({})).unwrap(); assert_eq!(plain(&app, id), format!("{FIRST} {SECOND}")); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("fixed")); act(&mut app, f); forget(&mut app, &[SECOND]); assert_eq!(shown(&app), (id, 0, FIRST.to_string()), "the check starts again and finds it"); @@ -970,7 +746,7 @@ mod tests { const SECOND: &str = "zqvdicttwo"; let mut app = app(); let id = text(&mut app, 50.0, &format!("{FIRST} {SECOND}")); - super::open(&mut app).unwrap(); + begin(&mut app); act(&mut app, super::add_to_dictionary); assert_eq!(shown(&app), (id, 11, SECOND.to_string())); // Another command takes the first word out of the dictionary again. @@ -985,7 +761,7 @@ mod tests { let first = text(&mut app, 50.0, &format!("{WORD_A} here")); let second = text(&mut app, 100.0, &format!("{WORD_B} there")); app.run("select.none", json!({})).unwrap(); - super::open(&mut app).unwrap(); + begin(&mut app); assert_eq!(shown(&app).0, first); app.run("object.lock", json!({ "ids": [first] })).unwrap(); act(&mut app, super::ignore); @@ -999,28 +775,24 @@ mod tests { let second = text(&mut app, 100.0, &format!("{WORD_B} there")); let outside = text(&mut app, 150.0, &format!("{WORD_C} elsewhere")); app.run("select.set", json!({ "ids": [first, second] })).unwrap(); - super::open(&mut app).unwrap(); + begin(&mut app); assert_eq!(shown(&app).0, first); app.run("edit.clear", json!({ "ids": [first] })).unwrap(); act(&mut app, super::ignore); assert_eq!(shown(&app), (second, 0, WORD_B.to_string()), "on to the selected type that is left"); app.run("edit.clear", json!({ "ids": [second] })).unwrap(); act(&mut app, super::ignore); - let d = app.ui.dialog.as_ref().unwrap(); - assert!(d.bool("done"), "nothing left in the selection: not the rest of the document ({outside})"); + assert!(done(&app), "nothing left in the selection: not the rest of the document ({outside})"); } /// Marks the first `n` occurrences of WORD_B in `id` (each WORD_B and a space) as skipped by /// Ignore, with the last of them shown. fn ignored_b_words(app: &mut VectorcraftApp, id: u64, n: usize) { let step = WORD_B.len() + 1; - let d = app.ui.dialog.as_mut().unwrap(); - let skipped: Vec<_> = (0..n).map(|i| json!([id, i * step, WORD_B])).collect(); - d.fields.insert("skipped".into(), json!(skipped)); + let w = app.spelling.as_mut().unwrap(); + w.skipped = (0..n).map(|i| (id, (i * step) as u64, WORD_B.to_string())).collect(); let last = (n - 1) * step; - for (k, v) in [("node_id", json!(id)), ("start", json!(last)), ("end", json!(last + WORD_B.len())), ("not_in_dictionary", json!(WORD_B))] { - d.fields.insert(k.into(), v); - } + w.shown = Some((id, last, last + WORD_B.len(), WORD_B.to_string(), "en".to_string())); } #[test] @@ -1028,7 +800,7 @@ mod tests { use vectorcraft_engine::cmd::spelling::MAX_WORDS; let mut app = app(); let id = text(&mut app, 50.0, &vec![WORD_B; MAX_WORDS + 1].join(" ")); - super::open(&mut app).unwrap(); + begin(&mut app); ignored_b_words(&mut app, id, MAX_WORDS); // Another command changes the document, so the next step checks the scope from its start, // where every word one check lists was skipped. @@ -1036,7 +808,7 @@ mod tests { act(&mut app, super::ignore); assert_eq!(shown(&app), (id, (MAX_WORDS * (WORD_B.len() + 1)) as u64, WORD_B.to_string()), "the last occurrence"); act(&mut app, super::ignore); - assert!(app.ui.dialog.as_ref().unwrap().bool("done")); + assert!(done(&app)); } #[test] @@ -1049,21 +821,20 @@ mod tests { app.run("select.set", json!({ "ids": [a, b] })).unwrap(); let group = app.run("object.group", json!({})).unwrap()["id"].as_u64().unwrap(); app.run("select.set", json!({ "ids": [group] })).unwrap(); - super::open(&mut app).unwrap(); + begin(&mut app); ignored_b_words(&mut app, a, MAX_WORDS); // An edit outside the group moves the document's revision: the group is checked again. app.run("text.editRange", json!({ "id": outside, "start": 0, "end": 0, "insert": "the " })).unwrap(); act(&mut app, super::ignore); assert_eq!(shown(&app), (b, 0, WORD_A.to_string()), "the next object in the group"); act(&mut app, super::ignore); - assert!(app.ui.dialog.as_ref().unwrap().bool("done"), "not the object outside the group"); + assert!(done(&app), "not the object outside the group"); // Change All: a target, then the most words one check lists, then a target in the next object. app.run("text.editRange", json!({ "id": a, "start": 0, "end": 0, "insert": format!("{WORD_A} ") })).unwrap(); - super::open(&mut app).unwrap(); + begin(&mut app); assert_eq!(shown(&app), (a, 0, WORD_A.to_string())); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("fixed")); - act(&mut app, super::change_all); + act(&mut app, |a, w| super::change_all(a, w, "fixed")); assert!(plain(&app, a).starts_with(&format!("fixed {WORD_B} "))); assert_eq!(plain(&app, b), "fixed end"); } @@ -1073,43 +844,43 @@ mod tests { PAGE.with(|p| p.set(2)); let mut app = app(); let id = text(&mut app, 50.0, &format!("{WORD_A} {WORD_B} {WORD_A} {WORD_D} {WORD_A}")); - super::open(&mut app).unwrap(); + begin(&mut app); assert_eq!(shown(&app), (id, 0, WORD_A.to_string())); act(&mut app, super::ignore_all); let mut seen = vec![shown(&app)]; act(&mut app, super::ignore); seen.push(shown(&app)); act(&mut app, super::ignore); - assert!(app.ui.dialog.as_ref().unwrap().bool("done")); + assert!(done(&app)); assert_eq!(seen, vec![(id, 10, WORD_B.to_string()), (id, 30, WORD_D.to_string())], "no WORD_A after Ignore All"); // Words of their own: the custom dictionary is shared by the whole process. const ADDED: &str = "zqvaddone"; const OTHER: &str = "zqvaddtwo"; PAGE.with(|p| p.set(1)); - let mut app = super::tests::app(); + let mut app = app(); let id = text(&mut app, 50.0, &format!("{ADDED} {OTHER} {ADDED} {OTHER}")); - super::open(&mut app).unwrap(); + begin(&mut app); act(&mut app, super::add_to_dictionary); let mut seen = vec![shown(&app)]; act(&mut app, super::ignore); seen.push(shown(&app)); act(&mut app, super::ignore); - let done = app.ui.dialog.as_ref().unwrap().bool("done"); + let finished = done(&app); forget(&mut app, &[ADDED]); - assert!(done); + assert!(finished); assert_eq!(seen, vec![(id, 10, OTHER.to_string()), (id, 30, OTHER.to_string())]); } #[test] fn change_to_the_same_word_changes_nothing_and_goes_on() { - for f in [super::change, super::change_all] { + type To = fn(&mut VectorcraftApp, &mut SpellingWalk, &str) -> Result<(), String>; + for f in [super::change as To, super::change_all] { let mut app = app(); let id = text(&mut app, 50.0, &format!("{WORD_D} and {WORD_D} {WORD_A}")); - super::open(&mut app).unwrap(); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!(WORD_D)); + begin(&mut app); let depth = app.session.doc().unwrap().history.undo.len(); - act(&mut app, f); + act(&mut app, |a, w| f(a, w, WORD_D)); assert_eq!(app.session.doc().unwrap().history.undo.len(), depth, "no undo step"); assert_eq!(plain(&app, id), format!("{WORD_D} and {WORD_D} {WORD_A}")); let d = WORD_D.len() as u64; @@ -1121,7 +892,7 @@ mod tests { fn a_word_moved_by_another_command_is_not_taken_for_an_ignored_one() { let mut app = app(); let id = text(&mut app, 50.0, &format!("{WORD_A} {WORD_B} {WORD_A}")); - super::open(&mut app).unwrap(); + begin(&mut app); act(&mut app, super::ignore); assert_eq!(shown(&app), (id, 10, WORD_B.to_string())); // Another command puts a different unknown word where the ignored one was. @@ -1136,13 +907,12 @@ mod tests { use vectorcraft_engine::cmd::spelling::MAX_WORDS; let mut app = app(); let id = text(&mut app, 50.0, &vec![WORD_A; MAX_WORDS + 1].join(" ")); - super::open(&mut app).unwrap(); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("fixed")); + begin(&mut app); let depth = app.session.doc().unwrap().history.undo.len(); - act(&mut app, super::change_all); + act(&mut app, |a, w| super::change_all(a, w, "fixed")); assert_eq!(plain(&app, id), vec!["fixed"; MAX_WORDS + 1].join(" ")); assert_eq!(app.session.doc().unwrap().history.undo.len(), depth + 1, "one undo step"); - assert!(app.ui.dialog.as_ref().unwrap().bool("done")); + assert!(done(&app)); } #[test] @@ -1153,6 +923,35 @@ mod tests { assert_eq!(super::shifted(9, &[(0, 2), (3, 5)], 1), 7); } + #[test] + fn a_check_with_all_layers_off_stays_in_the_shown_object() { + let mut app = app(); + let a = text(&mut app, 50.0, &format!("{WORD_A} x")); + let b = text(&mut app, 100.0, &format!("{WORD_A} y")); + app.run("select.none", json!({})).unwrap(); + begin(&mut app); + assert_eq!(shown(&app).0, a); + + // Off: Ignore finds nothing in the other object. + app.spelling.as_mut().unwrap().layer = Some(a); + act(&mut app, super::ignore); + assert!(done(&app), "the other object is out of scope"); + + // Off: Change All reaches only the shown object. + begin(&mut app); + app.spelling.as_mut().unwrap().layer = Some(a); + act(&mut app, |app, w| super::change_all(app, w, "fixed")); + assert_eq!(plain(&app, a), "fixed x"); + assert_eq!(plain(&app, b), format!("{WORD_A} y")); + + // On again: the other object is reached. + app.spelling.as_mut().unwrap().layer = None; + act(&mut app, |app, w| super::advance(app, w, None, 1)); + assert_eq!(shown(&app), (b, 0, WORD_A.to_string())); + act(&mut app, |app, w| super::change_all(app, w, "fixed")); + assert_eq!(plain(&app, b), "fixed y"); + } + fn app() -> VectorcraftApp { let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), crate::Services::default()); app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); @@ -1169,48 +968,6 @@ mod tests { } } - fn context() -> egui::Context { - let ctx = egui::Context::default(); - crate::theme::install_fonts(&ctx); - ctx - } - - fn frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec) -> Vec<(String, Rect)> { - let time = ctx.input(|i| i.time) + 0.25; - let screen_rect = Some(Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0))); - let raw = egui::RawInput { screen_rect, events, time: Some(time), ..Default::default() }; - let mut out = ctx.run_ui(raw, |ui| crate::dialogs::show(app, ui.ctx())); - out.textures_delta.clear(); - fn walk(s: &egui::Shape, v: &mut Vec<(String, Rect)>) { - match s { - egui::Shape::Text(t) => v.push((t.galley.text().to_string(), Rect::from_min_size(t.pos, t.galley.size()))), - egui::Shape::Vec(s) => s.iter().for_each(|s| walk(s, v)), - _ => {} - } - } - let mut v = vec![]; - out.shapes.iter().for_each(|c| walk(&c.shape, &mut v)); - v - } - - fn click(app: &mut VectorcraftApp, ctx: &egui::Context, label: &str) -> Vec<(String, Rect)> { - frame(app, ctx, vec![]); - let texts = frame(app, ctx, vec![]); - let at = texts.iter().find(|(t, _)| t == label).map(|(_, r)| r.center()).unwrap_or_else(|| panic!("no `{label}` in {texts:?}")); - let press = |pressed| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Default::default() }; - frame(app, ctx, vec![Event::PointerMoved(at)]); - frame(app, ctx, vec![press(true)]); - frame(app, ctx, vec![press(false)]); - frame(app, ctx, vec![]) - } - - fn key_press(app: &mut VectorcraftApp, ctx: &egui::Context, key: egui::Key) { - let down = Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }; - frame(app, ctx, vec![down]); - let up = Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers: egui::Modifiers::NONE }; - frame(app, ctx, vec![up]); - } - #[test] fn test_extract_sentence_context() { let text = "First sentence. Here is teh sample typo! Third sentence."; @@ -1253,66 +1010,20 @@ mod tests { assert!(w_mid.is_empty() && b_mid.is_empty() && a_mid.is_empty()); } - #[test] - fn menu_opens_check_spelling_dialog() { - let mut app = app(); - text(&mut app, 50.0, &format!("Hello {WORD_A} world")); - - let e = crate::menus::menu_entries(&app).into_iter().find(|e| e.label == "Check Spelling…").expect("in Edit menu"); - assert_eq!(e.command.as_deref(), Some(super::COMMAND)); - assert!(e.enabled); - - crate::menus::invoke(&mut app, super::COMMAND, json!({})); - let d = app.ui.dialog.as_ref().expect("Check Spelling dialog opens"); - assert_eq!(d.kind, super::KIND); - assert_eq!(d.str("not_in_dictionary"), WORD_A); - assert!(d.str("context").contains(WORD_A)); - assert!(!d.bool("done")); - } - #[test] fn check_spelling_word_replacement_and_undo_redo() { let mut app = app(); let id = text(&mut app, 50.0, &format!("Prefix {WORD_A} suffix")); - super::open(&mut app).unwrap(); - let ctx = context(); + begin(&mut app); + act(&mut app, |a, w| super::change(a, w, "replacement")); + assert_eq!(plain(&app, id), "Prefix replacement suffix"); - // Change replacement word - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("replacement")); - click(&mut app, &ctx, "Change"); - - // Verify word replacement in the document - let plain = { - let st = app.session.doc().unwrap(); - match &st.doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - } - }; - assert_eq!(plain, "Prefix replacement suffix"); - - // Verify one-step undo brings the original word back + // One undo step brings the original word back, and redo re-applies the replacement. app.run("edit.undo", json!({})).unwrap(); - let plain_undone = { - let st = app.session.doc().unwrap(); - match &st.doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - } - }; - assert_eq!(plain_undone, format!("Prefix {WORD_A} suffix")); - - // Verify redo re-applies the replacement + assert_eq!(plain(&app, id), format!("Prefix {WORD_A} suffix")); app.run("edit.redo", json!({})).unwrap(); - let plain_redone = { - let st = app.session.doc().unwrap(); - match &st.doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - } - }; - assert_eq!(plain_redone, "Prefix replacement suffix"); + assert_eq!(plain(&app, id), "Prefix replacement suffix"); } #[test] @@ -1322,47 +1033,15 @@ mod tests { let b = text(&mut app, 100.0, &format!("Second {WORD_B} line")); app.run("select.none", json!({})).unwrap(); - super::open(&mut app).unwrap(); - let ctx = context(); + begin(&mut app); + act(&mut app, |app, w| super::change_all(app, w, "replaced")); + assert_eq!(plain(&app, a), "First replaced here and replaced again"); + assert_eq!(plain(&app, b), "Second replaced line"); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("replaced")); - click(&mut app, &ctx, "Change All"); - - let plain_a = { - let st = app.session.doc().unwrap(); - match &st.doc.node(vectorcraft_doc::NodeId(a)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - } - }; - let plain_b = { - let st = app.session.doc().unwrap(); - match &st.doc.node(vectorcraft_doc::NodeId(b)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - } - }; - assert_eq!(plain_a, "First replaced here and replaced again"); - assert_eq!(plain_b, "Second replaced line"); - - // Single undo reverts all changes made by Change All + // A single undo reverts everything Change All did. app.run("edit.undo", json!({})).unwrap(); - let plain_a_undone = { - let st = app.session.doc().unwrap(); - match &st.doc.node(vectorcraft_doc::NodeId(a)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - } - }; - let plain_b_undone = { - let st = app.session.doc().unwrap(); - match &st.doc.node(vectorcraft_doc::NodeId(b)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - } - }; - assert_eq!(plain_a_undone, format!("First {WORD_B} here and {WORD_B} again")); - assert_eq!(plain_b_undone, format!("Second {WORD_B} line")); + assert_eq!(plain(&app, a), format!("First {WORD_B} here and {WORD_B} again")); + assert_eq!(plain(&app, b), format!("Second {WORD_B} line")); } #[test] @@ -1371,128 +1050,17 @@ mod tests { forget(&mut app, &[WORD_C]); text(&mut app, 50.0, &format!("Look at {WORD_C} here")); - super::open(&mut app).unwrap(); - let ctx = context(); - assert_eq!(app.ui.dialog.as_ref().unwrap().str("not_in_dictionary"), WORD_C); + begin(&mut app); + assert_eq!(shown(&app).2, WORD_C); + act(&mut app, super::add_to_dictionary); - click(&mut app, &ctx, "Add to Dictionary"); - - // Verify WORD_C was added to user dictionary let dict = app.run("text.spelling.dictionary", json!({})).unwrap(); let words = dict["words"].as_array().unwrap(); assert!(words.contains(&json!(WORD_C))); - - // Dialog should now have completed because no other unknown words exist - assert!(app.ui.dialog.as_ref().unwrap().bool("done")); - + // No other unknown word exists, so the walk is over. + let finished = done(&app); forget(&mut app, &[WORD_C]); - } - - #[test] - fn check_spelling_ignore_and_ignore_all() { - let mut app = app(); - text(&mut app, 50.0, &format!("{WORD_A} then {WORD_B}")); - - super::open(&mut app).unwrap(); - let ctx = context(); - - // First word is WORD_A - assert_eq!(app.ui.dialog.as_ref().unwrap().str("not_in_dictionary"), WORD_A); - - // Clicking Ignore moves to WORD_B without modifying the document or ignore list - click(&mut app, &ctx, "Ignore"); - assert_eq!(app.ui.dialog.as_ref().unwrap().str("not_in_dictionary"), WORD_B); - assert!(!app.session.doc().unwrap().spelling_ignored.contains(WORD_A)); - - // Clicking Ignore All adds WORD_B to the document's spelling_ignored list - click(&mut app, &ctx, "Ignore All"); - assert!(app.session.doc().unwrap().spelling_ignored.contains(WORD_B)); - assert!(app.ui.dialog.as_ref().unwrap().bool("done")); - } - - #[test] - fn check_spelling_keyboard_navigation() { - let mut app = app(); - let id = text(&mut app, 50.0, &format!("Item {WORD_A} text")); - - super::open(&mut app).unwrap(); - let ctx = context(); - - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("fixed")); - - // Enter triggers Change - key_press(&mut app, &ctx, egui::Key::Enter); - - let plain = { - let st = app.session.doc().unwrap(); - match &st.doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - } - }; - assert_eq!(plain, "Item fixed text"); - assert!(app.ui.dialog.as_ref().unwrap().bool("done")); - - // When done, Enter closes the dialog - key_press(&mut app, &ctx, egui::Key::Enter); - assert!(app.ui.dialog.is_none(), "Enter on done closes dialog"); - - // Enter when change_to is empty and review is unfinished leaves dialog open - text(&mut app, 100.0, &format!("More {WORD_A}")); - super::open(&mut app).unwrap(); - assert!(!app.ui.dialog.as_ref().unwrap().bool("done")); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!("")); - key_press(&mut app, &ctx, egui::Key::Enter); - assert!(app.ui.dialog.is_some(), "Enter with empty change_to leaves dialog open"); - - // Test Escape closes dialog - key_press(&mut app, &ctx, egui::Key::Escape); - assert!(app.ui.dialog.is_none(), "Escape closes dialog"); - } - - #[test] - fn check_spelling_selected_group_finds_nested_text() { - let mut app = app(); - let t = text(&mut app, 50.0, &format!("Grouped {WORD_A} item")); - app.run("select.set", json!({ "ids": [t] })).unwrap(); - let g = app.run("object.group", json!({})).unwrap(); - let group_id = g["id"].as_u64().unwrap(); - app.run("select.set", json!({ "ids": [group_id] })).unwrap(); - - super::open(&mut app).unwrap(); - let d = app.ui.dialog.as_ref().expect("dialog opens"); - assert_eq!(d.str("not_in_dictionary"), WORD_A); - } - - #[test] - fn check_spelling_when_no_misspellings() { - let mut app = app(); - text(&mut app, 50.0, "All correctly spelled English words here"); - - super::open(&mut app).unwrap(); - let d = app.ui.dialog.as_ref().expect("dialog opens"); - assert!(d.bool("done")); - assert!(d.str("context").contains("The spell check is complete.")); - - let ctx = context(); - click(&mut app, &ctx, "Done"); - assert!(app.ui.dialog.is_none()); - } - - #[test] - fn check_spelling_refused_on_document_switch() { - let mut app = app(); - text(&mut app, 50.0, &format!("Text {WORD_A} here")); - - super::open(&mut app).unwrap(); - assert!(app.ui.dialog.is_some()); - - // Create a new document, switching active document UID - app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); - - let ctx = context(); - frame(&mut app, &ctx, vec![]); - assert!(app.ui.dialog.is_none(), "switching documents closes spelling dialog"); + assert!(finished); } #[test] @@ -1516,6 +1084,20 @@ mod tests { assert!(!a2.contains("Near after")); } + #[test] + fn check_spelling_refused_on_document_switch() { + let mut app = app(); + text(&mut app, 50.0, &format!("Text {WORD_A} here")); + begin(&mut app); + + // PLAN: the pump's close-on-switch needs the window host fixture of the hosted-dialog + // tests; this drives the guard the pump calls (`check_doc_uid`) through the walk's steps. + app.run("file.new", json!({"width": 400, "height": 400})).unwrap(); + let mut w = app.spelling.take().unwrap(); + assert!(super::check_doc_uid(&app, &w).is_err()); + assert!(super::ignore(&mut app, &mut w).is_err(), "no step runs on another document"); + } + #[test] fn check_spelling_ignores_uneditable_text() { let mut app = app(); @@ -1527,14 +1109,13 @@ mod tests { app.run("object.lock", json!({})).unwrap(); app.run("select.set", json!({ "ids": [] })).unwrap(); - super::open(&mut app).unwrap(); - let ctx = context(); - assert_eq!(app.ui.dialog.as_ref().unwrap().str("not_in_dictionary"), WORD_A); - assert_eq!(app.ui.dialog.as_ref().unwrap().fields["node_id"], json!(editable_id)); + begin(&mut app); + assert_eq!(shown(&app).0, editable_id); + assert_eq!(shown(&app).2, WORD_A); // The locked object's word is never shown. - click(&mut app, &ctx, "Ignore"); - assert!(app.ui.dialog.as_ref().unwrap().bool("done"), "locked type {locked_id} is left out"); + act(&mut app, super::ignore); + assert!(done(&app), "locked type {locked_id} is left out"); } #[test] @@ -1542,23 +1123,12 @@ mod tests { let mut app = app(); let t1 = text(&mut app, 50.0, &format!("Prefix {WORD_A} suffix")); - super::open(&mut app).unwrap(); - assert!(app.ui.dialog.is_some()); - + begin(&mut app); // Replace WORD_A with something containing WORD_A - let ctx = context(); let replacement = format!("{WORD_A}abc"); - app.ui.dialog.as_mut().unwrap().fields.insert("change_to".into(), json!(replacement)); - - click(&mut app, &ctx, "Change All"); - // Change All successfully replaced WORD_A; advance moves to the new word - assert_eq!(app.ui.dialog.as_ref().unwrap().str("not_in_dictionary"), format!("{WORD_A}abc")); - - let st = app.session.doc().unwrap(); - let t1_text = match &st.doc.node(vectorcraft_doc::NodeId(t1)).unwrap().kind { - vectorcraft_doc::NodeKind::Text(t) => t.plain_text(), - _ => panic!("not text"), - }; - assert!(t1_text.contains(&format!("{WORD_A}abc"))); + act(&mut app, |a, w| super::change_all(a, w, &replacement)); + // Change All replaced WORD_A; the walk moves to the new word + assert_eq!(shown(&app).2, replacement); + assert!(plain(&app, t1).contains(&replacement)); } }