Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge #789: Styles: name the attributes in the params docs, and a wrong one in the command's error

aionggee2c450829ce
diff --cc crates/doc/src/attrs.rs
index 8c5ff1938,6dc9b8efd..a6d8ffb55
--- a/crates/doc/src/attrs.rs
+++ b/crates/doc/src/attrs.rs
@@@ -1398,25 -997,23 +1449,42 @@@ impl ListCounter
mod tests {
use super::*;
+ /// #777: an unknown attribute names the nearest one, or lists them all.
+ #[test]
+ fn unknown_attributes_suggest_the_right_name() {
+ let mut c = CharAttrs::default();
+ assert_eq!(c.set_json("font", &serde_json::json!("X")).unwrap_err(), "unknown attribute `font`; did you mean `fontFamily`?");
+ assert_eq!(c.set_json("fontFamly", &serde_json::json!("X")).unwrap_err(), "unknown attribute `fontFamly`; did you mean `fontFamily`?");
+ let all = c.set_json("zzzzzz", &serde_json::json!(1)).unwrap_err();
+ assert!(
+ all.starts_with("unknown attribute `zzzzzz`; the attributes are: ") && all.contains("fontFamily") && all.contains("fontStyle"),
+ "{all}"
+ );
+ assert!(c.set_json("font_family", &serde_json::json!("X")).is_ok() && c.set_json("fontFamily", &serde_json::json!("X")).is_ok());
+ let mut p = ParaAttrs::default();
+ assert_eq!(p.set_json("spaceBefor", &serde_json::json!(3)).unwrap_err(), "unknown attribute `spaceBefor`; did you mean `spaceBefore`?");
+ assert_eq!(camel("first_line_indent"), "firstLineIndent");
+ }
+
+ #[test]
+ fn levels_number_on_their_own_and_restart_after_a_higher_level() {
+ let at =
+ |level: u32| ParaProps { list_type: ListType::Numbers, list_level: level, number_expression: "^1.^#".into(), ..ParaProps::default() };
+ let mut c = ListCounter::default();
+ let mut labels = vec![];
+ for level in [1, 2, 2, 1, 2] {
+ let pp = at(level);
+ c.advance(&pp);
+ labels.push(pp.number_label_in(&c));
+ }
+ assert_eq!(labels, ["1.1", "1.1", "1.2", "2.2", "2.1"]);
+ // Without the restart, level 2 carries on.
+ let mut c = ListCounter::default();
+ let keep = |level: u32| ParaProps { restart_numbers: false, ..at(level) };
+ let n: Vec<u32> = [1, 2, 1, 2].into_iter().map(|l| c.advance(&keep(l))).collect();
+ assert_eq!(n, [1, 1, 2, 2]);
+ }
+
#[test]
fn a_partial_rule_keeps_the_other_fields() {
let current = ParaProps {
diff --cc crates/engine/src/cmd/style.rs
index 0a41c43ce,3c854ad87..0e921ace6
--- a/crates/engine/src/cmd/style.rs
+++ b/crates/engine/src/cmd/style.rs
@@@ -86,7 -86,7 +86,7 @@@ pub fn specs() -> Vec<CommandSpec>
"New Paragraph Style…",
[],
None,
- "{name, basedOn?, nextStyle?, para?: {… startAt?: null = continue numbering; grepStyles?: [{style, pattern}]: a pattern that can't compile is an error}, chars?: {…}, fromSelection?: bool}",
- "{name, basedOn?, nextStyle?, para?: {paragraph attributes, as type.para names them: align, leftIndent, firstLineIndent, spaceBefore, spaceAfter, …}, chars?: {character attributes, as type.char names them: fontFamily, fontStyle, size, leading, tracking, fill, …}, fromSelection?: bool}",
++ "{name, basedOn?, nextStyle?, para?: {paragraph attributes, as type.para names them: align, leftIndent, firstLineIndent, spaceBefore, spaceAfter, …; startAt?: null = continue numbering; grepStyles?: [{style, pattern}]: a pattern that can't compile is an error}, chars?: {character attributes, as type.char names them: fontFamily, fontStyle, size, leading, tracking, fill, …}, fromSelection?: bool}",
has_doc,
create_para
),
@@@ -95,7 -95,7 +95,7 @@@
"Paragraph Style Options…",
[],
None,
- "{name, rename?, basedOn?, nextStyle?, para?: {… ruleAbove?/ruleBelow?: only the named rule fields change; startAt?: null = continue numbering; kinsoku?: set | null (default rules); a grepStyles pattern that can't compile is an error}, chars?: {…}}",
- "{name, rename?, basedOn?, nextStyle?, para?: {paragraph attributes, as type.para names them: align, leftIndent, spaceBefore, …; ruleAbove?/ruleBelow?: only the named rule fields change}, chars?: {character attributes, as type.char names them: fontFamily, fontStyle, size, leading, tracking, fill, …}}",
++ "{name, rename?, basedOn?, nextStyle?, para?: {paragraph attributes, as type.para names them: align, leftIndent, spaceBefore, …; ruleAbove?/ruleBelow?: only the named rule fields change; startAt?: null = continue numbering; kinsoku?: set | null (default rules); a grepStyles pattern that can't compile is an error}, chars?: {character attributes, as type.char names them: fontFamily, fontStyle, size, leading, tracking, fill, …}}",
has_doc,
edit_para
),
@@@ -478,25 -440,10 +484,26 @@@ fn apply_char(s: &mut Session, p: &Valu
})
}
+/// In a style, `startAt: null` is Continue from Previous Number: it is stored as an explicit
+/// "continue" so it also overrides a Start At the style would inherit.
+fn continue_numbering(p: Option<&Value>, para: &mut ParaAttrs) {
+ let explicit =
+ p.and_then(Value::as_object).is_some_and(|o| o.iter().any(|(k, v)| v.is_null() && k.replace('_', "").eq_ignore_ascii_case("startAt")));
+ if explicit {
+ para.start_at = Some(None);
+ }
+}
+
fn create_para(s: &mut Session, p: &Value) -> Result<Value> {
let base = str_param(p, "name").unwrap_or("Paragraph Style 1").to_string();
- let mut para: ParaAttrs = attrs(p.get("para"), |a: &mut ParaAttrs, k, v| a.set_json_over(k, v, &designcraft_doc::ParaProps::default()))?;
- let mut para: ParaAttrs = attrs("style.paragraph.create", "para", p, |a: &mut ParaAttrs, k, v| a.set_json(k, v))?;
++ let mut para: ParaAttrs =
++ attrs("style.paragraph.create", "para", p, |a: &mut ParaAttrs, k, v| a.set_json_over(k, v, &designcraft_doc::ParaProps::default()))?;
+ continue_numbering(p.get("para"), &mut para);
+ explicit_default_kinsoku(&mut para, p.get("para"));
+ check_grep_styles("style.paragraph.create", &para)?;
- let mut chars: CharAttrs = attrs(p.get("chars"), |a: &mut CharAttrs, k, v| a.set_json(k, v))?;
+ let mut chars: CharAttrs = attrs("style.paragraph.create", "chars", p, |a: &mut CharAttrs, k, v| a.set_json(k, v))?;
+ // Validate caller-supplied fills, not selection fills retained after a swatch is deleted.
+ super::text::validate_char_fill(&s.doc()?.doc, &chars, "style.paragraph.create")?;
if p.get("fromSelection").and_then(Value::as_bool).unwrap_or(false)
&& let Ok(cur) = s.execute("type.selectionAttrs", &json!({}))
&& !cur.is_null()
@@@ -590,10 -497,7 +597,10 @@@ fn edit_para(s: &mut Session, p: &Value
}
// A rule object changes only the rule fields it names, over the style's resolved rule.
let (current, _) = d.styles.resolve_para_style(&name);
- let mut para: ParaAttrs = attrs(p.get("para"), |a: &mut ParaAttrs, k, v| a.set_json_over(k, v, &current))?;
- let para: ParaAttrs = attrs("style.paragraph.edit", "para", p, |a: &mut ParaAttrs, k, v| a.set_json_over(k, v, &current))?;
++ let mut para: ParaAttrs = attrs("style.paragraph.edit", "para", p, |a: &mut ParaAttrs, k, v| a.set_json_over(k, v, &current))?;
+ continue_numbering(p.get("para"), &mut para);
+ explicit_default_kinsoku(&mut para, p.get("para"));
+ check_grep_styles("style.paragraph.edit", &para)?;
let st = d.styles_mut().para_mut(&name).ok_or_else(|| bad("style.paragraph.edit", format!("no style `{name}`")))?;
st.para.merge(&para);
st.chars.merge(&chars);
@@@ -1550,6 -1190,63 +1557,48 @@@ fn swatch_options(s: &mut Session, p: &
})
}
-/// Point fills and strokes of an item (and its children) at a renamed swatch.
-fn rename_in(it: &mut designcraft_doc::Item, from: &str, to: &str) {
- if it.fill.swatch == from {
- it.fill.swatch = to.to_string();
- }
- if it.stroke.swatch == from {
- it.stroke.swatch = to.to_string();
- }
- if let Some(kids) = it.children_mut() {
- for k in kids {
- rename_in(std::sync::Arc::make_mut(k), from, to);
- }
- }
-}
-
+ /// #777: the params descriptions name the attributes, and a wrong one is the command's error.
+ #[cfg(test)]
+ mod attribute_error_tests {
+ use super::*;
+ use crate::cmd::find_command;
+
+ #[test]
+ fn a_wrong_attribute_names_the_command_and_the_right_attribute() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let err = |s: &mut Session, id: &str, p: Value| s.execute(id, &p).unwrap_err().to_string();
+ assert_eq!(
+ err(&mut s, "style.paragraph.create", json!({"name": "H", "chars": {"font": "Source Sans 3"}})),
+ "invalid parameters for `style.paragraph.create`: chars: unknown attribute `font`; did you mean `fontFamily`?"
+ );
+ assert_eq!(
+ err(&mut s, "style.paragraph.create", json!({"name": "H", "para": {"spaceBefor": 6}})),
+ "invalid parameters for `style.paragraph.create`: para: unknown attribute `spaceBefor`; did you mean `spaceBefore`?"
+ );
+ assert!(
+ err(&mut s, "style.character.create", json!({"name": "C", "chars": {"font": "X"}}))
+ .starts_with("invalid parameters for `style.character.create`: chars:")
+ );
+ assert!(err(&mut s, "style.paragraph.create", json!({"name": "H", "chars": "bold"})).contains("chars must be an object of attributes"));
+ s.execute("style.paragraph.create", &json!({"name": "H", "chars": {"fontFamily": "Source Sans 3", "size": 14}})).unwrap();
+ assert!(
+ err(&mut s, "style.paragraph.edit", json!({"name": "H", "chars": {"font": "X"}}))
+ .starts_with("invalid parameters for `style.paragraph.edit`: chars:")
+ );
+ s.execute("style.character.create", &json!({"name": "C", "chars": {"fontStyle": "Bold"}})).unwrap();
+ assert!(
+ err(&mut s, "style.character.edit", json!({"name": "C", "chars": {"font": "X"}}))
+ .starts_with("invalid parameters for `style.character.edit`: chars:")
+ );
+ for id in ["style.paragraph.create", "style.paragraph.edit", "style.character.create", "style.character.edit"] {
+ let doc = find_command(id).unwrap().params;
+ assert!(doc.contains("as type.char names them: fontFamily"), "{id}: {doc}");
+ }
+ assert!(find_command("style.paragraph.create").unwrap().params.contains("as type.para names them: align"));
+ }
+ }
+
#[cfg(test)]
mod color_tests {
use super::*;