From 39a2e4da07e587bf626aa913e944fcee6b7de6e3 Mon Sep 17 00:00:00 2001 From: cdwilson127 Date: Wed, 11 Nov 2026 16:21:26 +1000 Subject: [PATCH] stylesmanager set replies with only what changed (#3011) The reply carried the table summary (kind, style count, inDrawing, builtin) next to the edit. #3011 asks for only the changed style, so `set` now replies with the table's name and what the call changed: the edited `style`, the `removedStyle` name and the `description`, each only when given. The test checks that nothing else is in the reply. --- crates/engine/src/cmd/layout/plotstyle.rs | 22 +++++++++++----------- crates/engine/tests/it/plot_styles.rs | 18 ++++++++++++------ docs/plot-styles.md | 3 ++- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/cmd/layout/plotstyle.rs b/crates/engine/src/cmd/layout/plotstyle.rs index 8353e68c7..260f7e087 100644 --- a/crates/engine/src/cmd/layout/plotstyle.rs +++ b/crates/engine/src/cmd/layout/plotstyle.rs @@ -285,22 +285,22 @@ fn run_stylesmanager(s: &mut Session, p: &Value) -> Result { } } let t = t.sanitized(); - // Just what changed, not the whole table (a colour-dependent one has 255 styles): the - // table summary as `new` and `load` reply, the edited style, the removed style's name. - let edited = changed.and_then(|n| t.styles.iter().find(|x| x.name.eq_ignore_ascii_case(&n)).and_then(|x| serde_json::to_value(x).ok())); - let removed = str_param(p, "removeStyle").map(str::trim); - let name = t.name.clone(); - store(s.doc_mut()?, t); - let d = s.doc()?; - let mut out = table_summary(d, &find_table(cmd, d, &name)?); + // Only what changed, not the table (a colour-dependent one has 255 styles): the table's + // name, the description if given, the edited style and the removed style's name. + let mut out = json!({ "name": t.name }); if let Some(o) = out.as_object_mut() { - if let Some(e) = edited { + if str_param(p, "description").is_some() { + o.insert("description".into(), json!(t.description)); + } + let edited = changed.and_then(|n| t.styles.iter().find(|x| x.name.eq_ignore_ascii_case(&n))); + if let Some(e) = edited.and_then(|x| serde_json::to_value(x).ok()) { o.insert("style".into(), e); } - if let Some(r) = removed { - o.insert("removedStyle".into(), json!(r)); + if let Some(r) = str_param(p, "removeStyle") { + o.insert("removedStyle".into(), json!(r.trim())); } } + store(s.doc_mut()?, t); Ok(out) } "delete" => { diff --git a/crates/engine/tests/it/plot_styles.rs b/crates/engine/tests/it/plot_styles.rs index 6b9e27cbc..dd9415796 100644 --- a/crates/engine/tests/it/plot_styles.rs +++ b/crates/engine/tests/it/plot_styles.rs @@ -62,8 +62,8 @@ fn stylesmanager_creates_edits_saves_and_loads_tables() { &json!({"action": "set", "name": "office.stb", "style": {"name": "Pen 5", "color": "#00ff00", "screening": 40, "lineweight": 0.35, "linetype": "HIDDEN", "end": "round"}}), ) .unwrap(); - // `set` replies with the table summary and the edited style, not the whole table. - assert_eq!(r["styles"], 2); + // `set` replies with the table's name and the edited style, not the table. + assert_eq!(r["name"], "office.stb"); let pen = r["style"].clone(); assert_eq!(pen["name"], "Pen 5"); assert_eq!( @@ -141,8 +141,11 @@ fn stylesmanager_set_replies_with_the_change_not_the_whole_table() { let mut s = Session::new(); let set = json!({"action": "set", "name": "monochrome.ctb", "style": {"name": "Color_13", "lineweight": 0.3}}); let r = s.execute("stylesmanager", &set).unwrap(); - assert_eq!((r["name"].as_str(), r["styles"].as_u64()), (Some("monochrome.ctb"), Some(255)), "{r}"); + assert_eq!(r["name"], "monochrome.ctb", "{r}"); assert_eq!((r["style"]["name"].as_str(), r["style"]["lineweight"].as_u64()), (Some("Color_13"), Some(30)), "{r}"); + // Only the change: no table summary, no other style. + let keys: Vec<&str> = r.as_object().unwrap().keys().map(String::as_str).collect(); + assert_eq!(keys, ["name", "style"], "{r}"); assert!(r.to_string().len() < 1000, "the reply is short, not the 255-style table: {} bytes", r.to_string().len()); // The edit is in the drawing. let got = s.execute("stylesmanager", &json!({"action": "get", "name": "monochrome.ctb"})).unwrap(); @@ -151,7 +154,10 @@ fn stylesmanager_set_replies_with_the_change_not_the_whole_table() { // Removing a style of a named table names what went. s.execute("stylesmanager", &json!({"action": "new", "name": "office", "kind": "named"})).unwrap(); s.execute("stylesmanager", &json!({"action": "set", "name": "office.stb", "style": {"name": "Pen 5"}})).unwrap(); - let r = s.execute("stylesmanager", &json!({"action": "set", "name": "office.stb", "removeStyle": "Pen 5"})).unwrap(); - assert_eq!((r["removedStyle"].as_str(), r["styles"].as_u64()), (Some("Pen 5"), Some(1)), "{r}"); - assert!(r.get("style").is_none(), "{r}"); + let r = + s.execute("stylesmanager", &json!({"action": "set", "name": "office.stb", "removeStyle": "Pen 5", "description": "Office pens"})).unwrap(); + assert_eq!((r["removedStyle"].as_str(), r["description"].as_str()), (Some("Pen 5"), Some("Office pens")), "{r}"); + assert!(r.get("style").is_none() && r.get("styles").is_none(), "{r}"); + let got = s.execute("stylesmanager", &json!({"action": "get", "name": "office.stb"})).unwrap(); + assert_eq!(got["styles"].as_array().map(Vec::len), Some(1), "{got}"); } diff --git a/docs/plot-styles.md b/docs/plot-styles.md index 55fc300ab..fab29c99a 100644 --- a/docs/plot-styles.md +++ b/docs/plot-styles.md @@ -81,7 +81,8 @@ prints as `Normal`. A colour-dependent table ignores the property. style and the named styles. - `layer.set {name, plotStyle}` sets a layer's plot style. - `stylesmanager {action, …}` manages the tables: `list`, `get`, `new` (`kind`, `from`), `set` - (`style: {name, …fields}`, `removeStyle`, `description`), `delete`, `load` (`path` or `text`) + (`style: {name, …fields}`, `removeStyle`, `description`; it replies with the table's name and + only what changed), `delete`, `load` (`path` or `text`) and `save` (`path`, or returns `text`). Typed or chosen from **File ▸ Plot Styles...**, it opens the Plot Style Manager dialog.