Timeline: resolve exact feature names before ID fallbacks (#9689) (#9748)

* claim: #9689

* Resolve timeline names before falling back to feature IDs
This commit is contained in:
Colin Faulkingham
2026-10-31 00:08:24 +00:00
committed by GitHub
parent 526cdfd247
commit 0fe5bf114c
7 changed files with 94 additions and 48 deletions
+3 -6
View File
@@ -8,7 +8,7 @@ use serde_json::{Value, json};
use solidcraft_doc::{FeatureKind, PlaneRef};
use super::CommandSpec;
use crate::params::{bad, name_or_id_list, vec3};
use crate::params::{bad, name_or_id_values, vec3};
use crate::{Result, Session};
pub static COMMANDS: &[CommandSpec] = &[
@@ -24,14 +24,11 @@ pub static COMMANDS: &[CommandSpec] = &[
fn copy(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "timeline.copy";
// The features are named or numbered, and both kinds can sit in one list.
let keys = name_or_id_list(cmd, p, "features")?;
let keys = name_or_id_values(cmd, p, "features")?;
if keys.is_empty() || keys.len() > 1000 {
return Err(bad(cmd, "`features` must list 1…1000 features"));
}
let mut ids = Vec::new();
for k in &keys {
ids.push(s.doc.find_feature(k).map(|f| f.id).ok_or_else(|| bad(cmd, format!("no feature `{k}`")))?);
}
let ids = keys.iter().map(|key| super::edit::feature_id(s, Some(key), cmd)).collect::<Result<Vec<_>>>()?;
// Keep timeline order so sketches come before the features that use them.
let copied: Vec<solidcraft_doc::Feature> = s.doc.features.iter().filter(|f| ids.contains(&f.id)).cloned().collect();
let names: Vec<String> = copied.iter().map(|f| f.name.clone()).collect();
+1 -2
View File
@@ -160,8 +160,7 @@ fn apply(s: &mut Session, p: &Value) -> Result<Value> {
done = true;
}
if !features.is_empty() {
let ids: Vec<String> = features.iter().map(u64::to_string).collect();
sub(s, "timeline.delete", json!({ "features": ids }))?;
sub(s, "timeline.delete", json!({ "features": features }))?;
done = true;
}
// Bodies still there (not made by a feature deleted above) get a Remove feature.
+17 -27
View File
@@ -6,7 +6,7 @@ use serde_json::{Value, json};
use solidcraft_doc::{Feature, FeatureKind};
use super::CommandSpec;
use crate::params::{bad, bool_, name_or_id_list, num, str_};
use crate::params::{bad, bool_, name_or_id_values, num, str_};
use crate::{EngineError, Result, Sel, Session, Snapshot};
pub static COMMANDS: &[CommandSpec] = &[
@@ -179,47 +179,37 @@ fn compute_all(s: &mut Session, _p: &Value) -> Result<Value> {
Ok(json!({"features": s.model.results.len(), "errors": errors}))
}
fn feature_id(s: &Session, v: Option<&Value>, cmd: &str) -> Result<u64> {
let key = match v {
Some(Value::Number(n)) => n.to_string(),
Some(Value::String(x)) => x.clone(),
_ => return Err(bad(cmd, "`feature` must be an id or name")),
};
s.doc.find_feature(&key).map(|f| f.id).ok_or_else(|| bad(cmd, format!("no feature `{key}`")))
pub(super) fn feature_id(s: &Session, v: Option<&Value>, cmd: &str) -> Result<u64> {
match v {
Some(Value::Number(n)) => n.as_u64().and_then(|id| s.doc.feature(id)).map(|f| f.id).ok_or_else(|| bad(cmd, format!("no feature `{n}`"))),
Some(Value::String(name)) => s.doc.resolve_feature_name(name).map(|f| f.id).map_err(|e| bad(cmd, e.to_string())),
_ => Err(bad(cmd, "`feature` must be an id or name")),
}
}
fn delete(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "timeline.delete";
// The features are named or numbered, and both kinds can sit in one list.
let mut keys = name_or_id_list(cmd, p, "features")?;
if let Some(f) = p.get("feature") {
keys.push(match f {
Value::Number(n) => n.to_string(),
Value::String(x) => x.clone(),
_ => String::new(),
});
}
// Resolve every key against the same timeline. A cascade or a repeated key must never
// redirect a later delete to another feature that happens to match after the first one.
let keys = name_or_id_values(cmd, p, "features")?;
let mut ids = keys.iter().chain(p.get("feature")).map(|v| feature_id(s, Some(v), cmd)).collect::<Result<Vec<_>>>()?;
let groups = group_ids(s, p, false, cmd)?;
if keys.is_empty() && groups.is_empty() {
if ids.is_empty() && groups.is_empty() {
return Err(bad(cmd, "`features` must list features, or `groups` timeline folders"));
}
// A folder goes with its features and nested folders, its features in timeline order.
let (folders, contents) = group_tree(&s.doc, &groups);
let joints = group_joint_ids(&s.doc, &groups);
keys.extend(s.doc.features.iter().filter(|f| contents.contains(&f.id)).map(|f| f.id.to_string()));
ids.extend(s.doc.features.iter().filter(|f| contents.contains(&f.id)).map(|f| f.id));
// Features that work now; any of them failing after the delete is reported (a later
// feature that names a deleted body, #267).
let worked: std::collections::BTreeSet<u64> = s.model.results.iter().filter(|r| r.error.is_none() && !r.skipped).map(|r| r.id).collect();
super::joints::delete_joints(s.doc_mut(), &joints);
let mut gone = Vec::new();
for k in keys {
let Some(id) = s.doc.find_feature(&k).map(|f| f.id) else {
if gone.is_empty() {
return Err(bad(cmd, format!("no feature `{k}`")));
}
continue;
};
gone.extend(s.doc_mut().delete_feature(id)?);
for id in ids {
if s.doc.feature(id).is_some() {
gone.extend(s.doc_mut().delete_feature(id)?);
}
}
if !folders.is_empty() {
s.doc_mut().timeline_groups.retain(|group| !folders.contains(&group.id));
+39
View File
@@ -774,6 +774,45 @@ fn feature_names(s: &Session) -> Vec<String> {
s.doc.features.iter().map(|feature| feature.name.clone()).collect()
}
#[test]
fn timeline_keys_distinguish_numeric_names_from_ids() {
let mut s = four_boxes();
let id = s.doc.find_feature("Second").unwrap().id;
let named = s.doc.find_feature("Third").unwrap().id;
let key = id.to_string();
run(&mut s, "timeline.rename", json!({"feature": named, "name": key}));
assert_eq!(run(&mut s, "timeline.inspect", json!({"feature": key}))["id"], named);
assert_eq!(run(&mut s, "timeline.inspect", json!({"feature": id}))["id"], id);
assert_eq!(run(&mut s, "timeline.inspect", json!({"feature": named.to_string()}))["id"], named);
assert_eq!(run(&mut s, "timeline.copy", json!({"features": [key, id]}))["copied"], json!(["Second", key]));
// Resolve the whole request before deleting: a repeated name must not turn into an id
// after the first deletion removes the feature with that name.
assert_eq!(run(&mut s, "timeline.delete", json!({"features": [key, key]}))["deleted"], json!([named]));
assert!(s.doc.feature(id).is_some());
run(&mut s, "edit.undo", json!({}));
run(&mut s, "selection.delete", json!({"items": [Sel::Feature { id }]}));
assert!(s.doc.feature(id).is_none());
assert!(s.doc.feature(named).is_some());
}
#[test]
fn timeline_keys_prefer_exact_case_and_refuse_an_ambiguous_fallback() {
let mut s = four_boxes();
let second = s.doc.find_feature("Second").unwrap().id;
let third = s.doc.find_feature("Third").unwrap().id;
run(&mut s, "timeline.rename", json!({"feature": third, "name": "second"}));
let before = s.doc.clone();
let error = s.execute("timeline.delete", &json!({"features": ["First", "SECOND"]})).unwrap_err().to_string();
assert!(error.contains("ambiguous"), "{error}");
assert_eq!(s.doc, before);
assert_eq!(run(&mut s, "timeline.inspect", json!({"feature": "Second"}))["id"], second);
assert_eq!(run(&mut s, "timeline.suppress", json!({"feature": "second"}))["feature"], third);
assert_eq!(run(&mut s, "timeline.inspect", json!({"feature": "FIRST"}))["name"], "First");
}
/// `timeline.delete {groups}` deletes a folder with its features and nested folders in one undo
/// step; other folders and features stay, and `timeline.ungroup` still keeps the features (#7219).
#[test]