From 8042d9d3bb1b28055314e50a09e923924ea033bb Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Fri, 6 Nov 2026 16:09:02 +0000 Subject: [PATCH] #3518: whole-document OK sends only what was changed in the Language dialog With "Apply to the whole document" ticked, OK sends noProof only if the user changed it from the dialog's opening value, and lang if changed or if neither changed (never the engine's en-US default). Heading reads "Mark the whole document as:" while ticked. Glossary walker uses doc's PARA_WALK_DEPTH (now pub). Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01FypgCVCCEo7u5wetHaJRU4 --- crates/doc/src/lib.rs | 4 +- crates/doc/src/text_walk.rs | 2 +- crates/engine/src/cmd/tools/actions.rs | 2 +- .../src/dialogs/dialogs_layout/tests.rs | 63 ++++++++++++++++++- .../src/dialogs/dialogs_layout/text.rs | 24 +++++-- 5 files changed, 85 insertions(+), 10 deletions(-) diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index c759f84b04..ee89da0269 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -94,8 +94,8 @@ pub use settings::{ COMPAT_MODE_CURRENT, CoreProps, CustomProp, DEFAULT_GRID, DEFAULT_HYPHENATION_ZONE, LEGACY_COMPAT_MODE, OdfLayout, PagePicture, Settings, Source, THEME_COLORS, Watermark, Zoom, ZoomFit, }; -pub(crate) use text_walk::{PARA_WALK_DEPTH, walk_between}; -pub use text_walk::{count_chars, count_chars_no_spaces, count_words}; +pub(crate) use text_walk::walk_between; +pub use text_walk::{PARA_WALK_DEPTH, count_chars, count_chars_no_spaces, count_words}; /// A document. #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] diff --git a/crates/doc/src/text_walk.rs b/crates/doc/src/text_walk.rs index 3ce3499e69..4c76cc1e63 100644 --- a/crates/doc/src/text_walk.rs +++ b/crates/doc/src/text_walk.rs @@ -48,7 +48,7 @@ pub fn count_chars_no_spaces(s: &str) -> usize { } /// How deep in nested tables [`Document::para_paths`] goes: cells below this are skipped. -pub(crate) const PARA_WALK_DEPTH: usize = 16; +pub const PARA_WALK_DEPTH: usize = 16; /// The note and text box stories an object shows, for `Document::reachable`. pub(crate) fn notes_and_boxes(o: &InlineObject) -> Vec<(u32, &'static [PartKind])> { diff --git a/crates/engine/src/cmd/tools/actions.rs b/crates/engine/src/cmd/tools/actions.rs index 513335e347..d134f2b574 100644 --- a/crates/engine/src/cmd/tools/actions.rs +++ b/crates/engine/src/cmd/tools/actions.rs @@ -99,7 +99,7 @@ fn set_document_props(s: &mut Session, f: &dyn Fn(&mut CharProps)) -> Result<(), /// [`set_document_props`] for a block list outside any story (depth-limited like `para_paths`). fn format_blocks(blocks: &mut wordcraft_doc::Blocks, f: &dyn Fn(&mut CharProps), depth: usize) { - if depth > 32 { + if depth > wordcraft_doc::PARA_WALK_DEPTH { return; } for blk in blocks.iter_mut() { diff --git a/crates/ui-egui/src/dialogs/dialogs_layout/tests.rs b/crates/ui-egui/src/dialogs/dialogs_layout/tests.rs index 4dad2d1c47..9d7354dc08 100644 --- a/crates/ui-egui/src/dialogs/dialogs_layout/tests.rs +++ b/crates/ui-egui/src/dialogs/dialogs_layout/tests.rs @@ -213,6 +213,7 @@ fn language_dialog_default_survives_a_restart() { no_proof: false, detect: false, whole_document: false, + initial: ("fr-FR".into(), false), })))); let mut h = egui_kittest::Harness::builder().with_size(egui::vec2(1440.0, 900.0)).build_ui_state( |ui, app: &mut WordApp| { @@ -241,7 +242,7 @@ fn language_dialog_default_survives_a_restart() { #[test] fn language_dialog_whole_document_checkbox_sends_scope() { use egui_kittest::kittest::Queryable; - let f = LanguageForm { lang: "fr-FR".into(), no_proof: false, detect: false, whole_document: false }; + let f = LanguageForm { lang: "fr-FR".into(), no_proof: false, detect: false, whole_document: false, initial: ("fr-FR".into(), false) }; assert!(f.params().get("scope").is_none()); assert_eq!(LanguageForm { whole_document: true, ..f.clone() }.params()["scope"], "document"); let mut a = app(); @@ -267,6 +268,66 @@ fn language_dialog_whole_document_checkbox_sends_scope() { assert_eq!(hp.runs.first().and_then(|r| r.props.lang.as_deref()), Some("fr-FR")); } +/// "Plain text, then a Do-not-check run", the caret in the plain text. +fn proofing_mix_app() -> WordApp { + let mut a = app(); + a.run("text.insert", json!({"text": "Plain code"})).unwrap(); + a.run("select.text", json!({"text": "code"})).unwrap(); + a.run("review.language", json!({"lang": "de-DE", "noProof": true})).unwrap(); + a.run("select.text", json!({"text": "Plain"})).unwrap(); + a.run("review.language", json!({"lang": "en-US", "noProof": false})).unwrap(); + a.run("select.text", json!({"text": "Plain"})).unwrap(); + a +} + +fn run_proofing(a: &mut WordApp, text: &str) -> (Option, Option) { + a.run("select.text", json!({"text": text})).unwrap(); + let st = a.run("format.state", json!({})).unwrap(); + (st["lang"].as_str().map(String::from), st["noProof"].as_bool()) +} + +/// #3518: whole-document OK sends only what the user changed. +#[test] +fn language_dialog_whole_document_sends_only_changes() { + // Changing only the language leaves the Do-not-check run alone. + let mut a = proofing_mix_app(); + let mut f = LanguageForm::read(&a); + f.whole_document = true; + f.lang = "en-GB".into(); + let p = f.params(); + assert!(p.get("noProof").is_none()); + a.run("review.language", p).unwrap(); + assert_eq!(run_proofing(&mut a, "code"), (Some("en-GB".into()), Some(true))); + assert_eq!(run_proofing(&mut a, "Plain"), (Some("en-GB".into()), Some(false))); + + // Changing only Do-not-check leaves each run's language alone. + let mut a = proofing_mix_app(); + let mut f = LanguageForm::read(&a); + f.whole_document = true; + f.no_proof = true; + let p = f.params(); + assert!(p.get("lang").is_none()); + a.run("review.language", p).unwrap(); + assert_eq!(run_proofing(&mut a, "code"), (Some("de-DE".into()), Some(true))); + assert_eq!(run_proofing(&mut a, "Plain"), (Some("en-US".into()), Some(true))); + + // No edits: the caret's language, no noProof (never the engine's en-US default). + let mut a = proofing_mix_app(); + let mut f = LanguageForm::read(&a); + f.lang = "fr-FR".into(); + f.initial.0 = "fr-FR".into(); + f.whole_document = true; + let p = f.params(); + assert_eq!(p["lang"], "fr-FR"); + assert!(p.get("noProof").is_none()); + a.run("review.language", p).unwrap(); + assert_eq!(run_proofing(&mut a, "code"), (Some("fr-FR".into()), Some(true))); + + // Selection mode is unchanged: both are sent. + f.whole_document = false; + assert_eq!(f.params()["noProof"], false); +} + /// #407: Envelopes starts from the selected address; Add to Document puts an envelope /// section before the letter without asking to save (nothing is replaced). #[test] diff --git a/crates/ui-egui/src/dialogs/dialogs_layout/text.rs b/crates/ui-egui/src/dialogs/dialogs_layout/text.rs index bc6e72c602..53a811eadf 100644 --- a/crates/ui-egui/src/dialogs/dialogs_layout/text.rs +++ b/crates/ui-egui/src/dialogs/dialogs_layout/text.rs @@ -322,6 +322,9 @@ pub struct LanguageForm { pub detect: bool, /// "Apply to the whole document": OK sends `scope: "document"` (every story), not the selection. pub whole_document: bool, + /// The language and Do-not-check state the dialog opened with, to tell what the user changed. + #[serde(skip)] + pub initial: (String, bool), } impl LanguageForm { @@ -330,14 +333,25 @@ impl LanguageForm { let style = s.doc.para_at(&s.sel.focus).and_then(|p| p.props.style.as_deref()); let here = s.doc.styles.resolve_char(style, &s.typing_props()); let lang = here.lang.unwrap_or_else(|| "en-US".into()); - LanguageForm { lang, no_proof: here.no_proof, detect: s.prefs.detect_language, whole_document: false } + LanguageForm { initial: (lang.clone(), here.no_proof), lang, no_proof: here.no_proof, detect: s.prefs.detect_language, whole_document: false } } /// `review.language` parameters for OK. pub fn params(&self) -> Value { - let mut v = json!({"lang": self.lang, "noProof": self.no_proof, "detect": self.detect}); - if let (true, Some(m)) = (self.whole_document, v.as_object_mut()) { - m.insert("scope".into(), json!("document")); + if !self.whole_document { + return json!({"lang": self.lang, "noProof": self.no_proof, "detect": self.detect}); + } + // Whole document: the caret's values must not flatten what differs elsewhere, so send only + // what was changed. With no edits at all the language goes (never the engine's en-US default). + let (lang_changed, proof_changed) = (self.lang != self.initial.0, self.no_proof != self.initial.1); + let mut v = json!({"scope": "document", "detect": self.detect}); + if let Some(m) = v.as_object_mut() { + if lang_changed || !proof_changed { + m.insert("lang".into(), json!(self.lang)); + } + if proof_changed { + m.insert("noProof".into(), json!(self.no_proof)); + } } v } @@ -349,7 +363,7 @@ pub(super) fn language_name(tag: &str) -> String { } pub(super) fn language_ui(app: &mut WordApp, ui: &mut Ui, f: &mut LanguageForm) -> bool { - ui.label(tl!("Mark selected text as:")); + ui.label(if f.whole_document { tl!("Mark the whole document as:") } else { tl!("Mark selected text as:") }); let list = crate::ribbon::language_list(&f.lang); egui::Frame::NONE.stroke(egui::Stroke::new(1.0, crate::theme::Tokens::get(ui.ctx()).border_strong)).inner_margin(4.0).show(ui, |ui| { egui::ScrollArea::vertical().max_height(220.0).min_scrolled_height(220.0).show(ui, |ui| {