diff --git a/crates/doc/src/history_names.rs b/crates/doc/src/history_names.rs index aa1cd5e5e..0bb149c5b 100644 --- a/crates/doc/src/history_names.rs +++ b/crates/doc/src/history_names.rs @@ -428,6 +428,12 @@ pub(crate) fn matches(names: &Names, index: usize, name: &str) -> Option if info.is_some_and(|i| i.aliases.iter().any(|a| a == name)) { return Some(Match::Alias); } + // A pattern copy's faces carry their instance after the name (`…@F9.1`, after any + // qualifier): the names compare as the original's do, within the same copy only. + let ((name, at), (current, here)) = (instance_suffix(name), instance_suffix(current)); + if at != here { + return None; + } // The named face (or piece) was split further: this is one of its pieces. if descends(current, name) { return Some(Match::Piece); @@ -443,6 +449,21 @@ pub(crate) fn matches(names: &Names, index: usize, name: &str) -> Option (crate::naming::strip_piece(current) == root).then_some(Match::Stem) } +/// A name and the pattern instances after it (`F3:top#[F4:side]@F9.1` is `F3:top#[F4:side]` and +/// `@F9.1`; a copy of a copy carries one instance after another). +fn instance_suffix(n: &str) -> (&str, &str) { + let mut core = n; + while let Some(at) = core.rfind('@') { + let instance = core.get(at + 1..).and_then(|t| t.strip_prefix('F')).and_then(|t| t.split_once('.')); + let digits = |t: &str| !t.is_empty() && t.bytes().all(|b| b.is_ascii_digit()); + if !instance.is_some_and(|(feature, k)| digits(feature) && digits(k)) { + break; + } + core = core.get(..at).unwrap_or_default(); + } + (core, n.get(core.len()..).unwrap_or_default()) +} + #[cfg(test)] #[path = "history_names_tests.rs"] mod tests; diff --git a/crates/doc/src/history_names_tests.rs b/crates/doc/src/history_names_tests.rs index d938c1a48..7c67e5a71 100644 --- a/crates/doc/src/history_names_tests.rs +++ b/crates/doc/src/history_names_tests.rs @@ -44,6 +44,32 @@ fn pieces_are_told_apart_by_the_faces_only_they_touch() { assert_eq!(matches(&names, 2, "top"), None); } +/// A pattern's copy names its faces after the original's with the instance after them +/// (`…@F9.1`, after any qualifier): a stored piece name of a copy finds its piece as the +/// original's does, in that copy only. +#[test] +fn a_pattern_copys_piece_names_match_as_the_originals_do() { + let faces = named(&["top#[left,slot:a]@F9.1", "top#[right,slot:b]@F9.1", "front@F9.1", "top#[left,slot:a]@F9.2", "top#[left,slot:a]"]); + let names = qualify(faces, &[]); + // The faces around the piece have changed since the name was stored (other slot walls). + let stored = "top#[left,slot:c]@F9.1"; + assert_eq!(matches(&names, 0, stored), Some(Match::Stem)); + assert_eq!(matches(&names, 1, stored), Some(Match::Stem)); + assert_eq!(matches(&names, 2, stored), None); + assert_eq!(matches(&names, 3, stored), None, "another copy"); + assert_eq!(matches(&names, 4, stored), None, "the original"); + assert_eq!(matches(&names, 0, "top@F9.1"), Some(Match::Piece), "a piece of the copy's face"); + assert_eq!(matches(&names, 0, "top#[left,slot:a]@F9.1"), Some(Match::Exact)); + // Copies of copies carry one instance after another. + let names = qualify(named(&["top#[left]@F9.1@F12.2", "top#[left]@F9.1@F12.1"]), &[]); + assert_eq!(matches(&names, 0, "top#[slot:c]@F9.1@F12.2"), Some(Match::Stem)); + assert_eq!(matches(&names, 1, "top#[slot:c]@F9.1@F12.2"), None); + // Hostile suffixes never panic. + for n in ["@", "@F", "@F.", "@F1.", "a@F1.1@", "a#[b]@F1.1]", "@F1.1"] { + let _ = matches(&names, 0, n); + } +} + #[test] fn pieces_nothing_tells_apart_are_numbered_and_unverified() { let faces = named(&["top", "top", "ring"]);