Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

requirements.references.outline_selectors

Requirements index · View definition

evidence Valid

Requirement

An outline selector MUST be a /-separated path of segments from the file’s root node to an outline node; the empty selector MUST select the root node. A named node’s segment MUST be kind[name]. An anonymous node’s segment MUST be kind@hash, where hash is the first twelve hexadecimal digits of its identity hash under cite requirements.references.structural_hash. Siblings whose segments would otherwise be identical MUST be disambiguated by #N, their one-based position among those siblings in source order, except line nodes: identical lines are interchangeable, so a line selector MUST resolve when any line with that content exists in the file. In names, %, [, ], /, #, @, and whitespace MUST be percent-encoded as UTF-8 bytes, so selectors are reversible. Line numbers MUST NOT form part of a selector.

Rationale

I judged the tests adequate for requirements.references.outline_selectors. I did not request implementation source, because the supplied test text was enough to map each obligation. The requirements.references.structural_hash citation was treated as context only.

Obligations and the tests that cover them

  • Empty selector selects the root.
    • src/outline/selector.rs#mod[tests]/fn[parses_named_anonymous_and_ordinal_segments] asserts that parse_selector("") yields no segments.
    • src/outline/tests.rs#fn[rust_selectors_follow_syntactic_ancestry] asserts outline.resolve("") == 0.
  • /-separated path from the root, with named segments as kind[name].
    • rust_selectors_follow_syntactic_ancestry checks about 20 selectors. Examples are mod[local]/fn[run], fn[outer]/fn[inner] and trait[Trait]/fn[read].
    • For each, it asserts that the selector is produced and that resolve round-trips to a node carrying that same selector.
    • It also checks that a missing name and an ambiguous name fail to resolve.
  • Anonymous segments as kind@hash, with the first twelve hex digits of the identity hash.
    • anonymous_selectors_use_identity_hashes asserts the use@ selector has exactly 12 digits after the @. It asserts that selectors survive an inserted sibling and that an edited node’s selector stops resolving.
    • anonymous_container_selectors_use_identity_hashes_and_survive_descendant_edits asserts the selector equals pair[items]/item@{identity_hash[..12]}. It asserts that the identity hash differs from the structural hash, and that the selector still resolves after a descendant edit.
    • parses_named_anonymous_and_ordinal_segments covers parsing and normalisation of kind@hash and rejects a malformed hash (use@123).
  • #N disambiguation, one-based and in source order.
    • fn[repeated]#1 and fn[repeated]#2 in the Rust test.
    • Identical use statements give #1 and #2 in anonymous_selectors_use_identity_hashes.
    • The parser rejects #0 and accepts #2.
    • Unique nodes such as struct[Thing] carry no ordinal.
  • line nodes are interchangeable. plain_text_lines_are_interchangeable_anonymous_nodes asserts that two identical lines give identical line@ selectors. It asserts that resolve succeeds on that selector. It also checks the word-based hash, where two words and two words share a selector and twowords differs.
  • Percent-encoding of %, [, ], /, #, @ and whitespace as UTF-8 bytes, reversibly.
    • names_round_trip_through_percent_encoding asserts the exact encoding of all listed characters, plus tab, NBSP and ideographic space as multi-byte UTF-8.
    • It asserts that decoding restores the original name, and that raw /, a bad escape and raw whitespace are rejected.
    • rust_selectors_follow_syntactic_ancestry covers a real node, impl[%5Bu8;%204%5D].
    • profile_rules_map_only_matching_contexts_and_keep_other_nodes_transparent checks the error for a malformed escape.
  • No line numbers in selectors. Every exact-string selector assertion above contains none. Nothing in the tests contradicts this obligation, and it has no separate branching behaviour to test.

Gaps and acknowledgments: I found none, and there are no {fixme} acknowledgments.

Inputs

Tests

Input 3: outline src/outline/selector.rs mod[tests]/fn[names_round_trip_through_percent_encoding]
    // {test}`requirements.references.outline_selectors`
    #[test]
    fn names_round_trip_through_percent_encoding() {
        let name = "a b/c[d]#e@f%g\té\u{a0}\u{3000}";
        let encoded = encode_name(name);
        assert_eq!(encoded, "a%20b%2Fc%5Bd%5D%23e%40f%25g%09é%C2%A0%E3%80%80");
        assert_eq!(decode_name(&encoded).unwrap(), name);
        assert!(decode_name("a/b").is_err());
        assert!(decode_name("%zz").is_err());
        assert!(decode_name("a b").is_err());
    }
Input 4: outline src/outline/selector.rs mod[tests]/fn[parses_named_anonymous_and_ordinal_segments]
    // {test}`requirements.references.outline_selectors`
    #[test]
    fn parses_named_anonymous_and_ordinal_segments() {
        assert!(parse_selector("").unwrap().is_empty());
        let segments =
            parse_selector("impl[Thing/Trait%3CT%3E]/fn[run]#2/use@0123456789AB").unwrap();
        assert_eq!(
            segments[0].label,
            Label::Named(vec!["Thing".into(), "Trait<T>".into()])
        );
        assert_eq!(segments[1].ordinal, Some(2));
        assert_eq!(segments[2].label, Label::Hash("0123456789ab".into()));
        assert_eq!(segments[2].to_string(), "use@0123456789ab");
        for invalid in [
            "fn",
            "fn[run",
            "[run]",
            "fn[run]#0",
            "use@123",
            "fn[run]x",
            "a//b",
        ] {
            assert!(parse_selector(invalid).is_err(), "{invalid}");
        }
    }
Input 5: outline src/outline/tests.rs fn[profile_rules_map_only_matching_contexts_and_keep_other_nodes_transparent]
  • File: src/outline/tests.rs
  • Selector: fn[profile_rules_map_only_matching_contexts_and_keep_other_nodes_transparent]
  • Kind: fn
  • src/outline/tests.rs:16–53
// {test}`requirements.references.outlines`
// {test}`requirements.references.outline_selectors`
#[test]
fn profile_rules_map_only_matching_contexts_and_keep_other_nodes_transparent() {
    use super::profile::{KindRule, NameRule, Profile};

    let rules = vec![
        KindRule::new("function_item", "fn", NameRule::Field("name")).when(|node| {
            node.child_by_field_name("name")
                .is_some_and(|name| name.start_position().column == 3)
        }),
        KindRule::new("struct_item", "struct", NameRule::Field("name")),
    ];
    let profile = Box::leak(Box::new(Profile {
        kinds: Box::leak(rules.into_boxed_slice()),
        ..super::rust::PROFILE
    }));
    // A function indented by two spaces is not mapped, but its descendants
    // still appear under the nearest mapped ancestor, here the file root.
    let source = "fn selected() {}\n  fn skipped() {\nfn nested() {}\n}\nstruct Packet;\n";
    let outline = super::parse_with(profile, source).unwrap();
    assert_eq!(
        selectors(&outline),
        ["fn[selected]", "fn[nested]", "struct[Packet]"]
    );
    assert!(outline.nodes[1..].iter().all(|node| node.parent == Some(0)));
    assert_eq!(format!("{outline:?}").matches("profile: rust").count(), 1);
    let missing = outline.resolve("fn[skipped]").unwrap_err();
    assert_eq!(
        missing.to_string(),
        "no outline node matches \"fn[skipped]\""
    );
    let malformed = outline.resolve("fn[bad%ZZ]").unwrap_err();
    assert!(matches!(malformed, super::ResolveError::Invalid(_)));
    let message = malformed.to_string();
    assert!(message.contains("invalid outline selector"), "{message}");
    assert!(message.contains("malformed percent escape"), "{message}");
}
Input 6: outline src/outline/tests.rs fn[rust_selectors_follow_syntactic_ancestry]
// {test}`requirements.references.outline_selectors`
// {test}`requirements.references.rust_item_selectors`
// {test}`requirements.references.selector_nested`
// {test}`requirements.references.selector_impl_and_trait`
// {test}`requirements.references.selector_inline_and_foreign`
// {test}`requirements.references.selector_duplicate_names`
// {test}`requirements.references.outlines`
#[test]
fn rust_selectors_follow_syntactic_ancestry() {
    let source = r#"
mod local { fn run() {} }
mod external;
struct Thing;
impl Thing { fn run() {} }
impl Trait for Thing { fn run() {} }
impl [u8; 4] { fn edge() {} }
trait Trait { fn read(&self); type Item; }
extern "C" { fn puts(); }
extern { fn other(); }
fn outer() { fn inner() {} }
#[cfg(a)] fn repeated() {}
#[cfg(b)] fn repeated() {}
macro_rules! make { () => {} }
union U { a: u8 }
enum E { A }
const C: u8 = 1;
static S: u8 = 1;
type Alias = u8;
extern crate core;
"#;
    let outline = outline("src/lib.rs", source);
    let selectors = selectors(&outline);
    for selector in [
        "mod[local]/fn[run]",
        "mod[external]",
        "struct[Thing]",
        "impl[Thing]/fn[run]",
        "impl[Thing/Trait]/fn[run]",
        "impl[%5Bu8;%204%5D]/fn[edge]",
        "trait[Trait]/fn[read]",
        "trait[Trait]/type[Item]",
        "extern[C]/fn[puts]",
        "extern[Rust]/fn[other]",
        "fn[outer]/fn[inner]",
        "fn[repeated]#1",
        "fn[repeated]#2",
        "macro[make]",
        "union[U]",
        "enum[E]",
        "const[C]",
        "static[S]",
        "type[Alias]",
        "extern-crate[core]",
    ] {
        assert!(
            selectors.contains(&selector.to_owned()),
            "{selector}: {selectors:?}"
        );
        assert_eq!(
            outline.nodes[outline.resolve(selector).unwrap()].selector,
            selector
        );
    }
    assert_eq!(outline.resolve("").unwrap(), 0);
    assert!(outline.resolve("fn[repeated]").is_err());
    assert!(outline.resolve("fn[missing]").is_err());
}
Input 7: outline src/outline/tests.rs fn[anonymous_selectors_use_identity_hashes]
// {test}`requirements.references.selector_use`
// {test}`requirements.references.outline_selectors`
// {test}`requirements.references.structural_hash`
#[test]
fn anonymous_selectors_use_identity_hashes() {
    let before = outline(
        "src/lib.rs",
        "use alpha::A;\nuse beta::B;\nstruct Packet;\n",
    );
    let inserted = outline(
        "src/lib.rs",
        "use gamma::C;\n\nuse alpha::A;\nuse beta::B;\nstruct Packet;\n",
    );
    let changed = outline(
        "src/lib.rs",
        "use alpha::Z;\nuse beta::B;\nstruct Packet;\n",
    );
    let before_selectors = selectors(&before);
    assert!(before_selectors[0].starts_with("use@"));
    assert_eq!(before_selectors[0].len(), "use@".len() + 12);
    assert_eq!(before_selectors[2], "struct[Packet]");
    for selector in &before_selectors {
        assert!(inserted.resolve(selector).is_ok(), "{selector}");
    }
    assert!(changed.resolve(&before_selectors[0]).is_err());
    assert!(changed.resolve(&before_selectors[1]).is_ok());
    let twice = outline("src/lib.rs", "use a::A;\nuse a::A;\n");
    let twice = selectors(&twice);
    assert!(
        twice[0].ends_with("#1") && twice[1].ends_with("#2"),
        "{twice:?}"
    );
}
Input 8: outline src/outline/tests.rs fn[plain_text_lines_are_interchangeable_anonymous_nodes]
// {test}`requirements.references.outlines`
// {test}`requirements.references.outline_selectors`
#[test]
fn plain_text_lines_are_interchangeable_anonymous_nodes() {
    let outline = outline("notes.txt", "alpha\n\n  beta  \nalpha\n");
    let selectors = selectors(&outline);
    assert_eq!(selectors.len(), 3);
    assert!(
        selectors
            .iter()
            .all(|selector| selector.starts_with("line@"))
    );
    assert_eq!(selectors[0], selectors[2]);
    assert_eq!(outline.resolve(&selectors[0]).unwrap(), 1);
    assert_eq!(outline.nodes[2].structural_hash, super::hash::line("beta"));
    let spaced = super::parse(Path::new("notes.txt"), "two  words\n").unwrap();
    let single = super::parse(Path::new("notes.txt"), "two words\n").unwrap();
    let joined = super::parse(Path::new("notes.txt"), "twowords\n").unwrap();
    assert_eq!(spaced.nodes[1].selector, single.nodes[1].selector);
    assert_ne!(single.nodes[1].selector, joined.nodes[1].selector);
    assert_eq!(outline.innermost_at_line(2), [0]);
    assert_eq!(outline.innermost_at_line(3), [2]);
}
Input 9: outline src/outline/tests.rs fn[anonymous_container_selectors_use_identity_hashes_and_survive_descendant_edits]
  • File: src/outline/tests.rs
  • Selector: fn[anonymous_container_selectors_use_identity_hashes_and_survive_descendant_edits]
  • Kind: fn
  • src/outline/tests.rs:817–838
// {test}`requirements.references.outline_selectors`
#[test]
fn anonymous_container_selectors_use_identity_hashes_and_survive_descendant_edits() {
    let before = outline("a.yml", "items:\n  - f: 1\n");
    let after = outline("a.yml", "items:\n  - f: 200\n");
    let index = before
        .nodes
        .iter()
        .position(|node| node.kind == "item")
        .unwrap();
    let node = &before.nodes[index];
    assert_ne!(node.identity_hash, node.structural_hash);
    assert_eq!(
        node.selector,
        format!("pair[items]/item@{}", &node.identity_hash[..12])
    );
    let edited = &after.nodes[after.resolve(&node.selector).unwrap()];
    assert_eq!(edited.identity_hash, node.identity_hash);
    assert_ne!(edited.structural_hash, node.structural_hash);
    assert_eq!(edited.selector, node.selector);
    assert!(after.resolve(&format!("{}/pair[f]", node.selector)).is_ok());
}

Referenced definitions

Input 2: requirements selection requirements.references.structural_hash
:::{req} structural_hash
A node's structural hash MUST be the SHA-256 digest of an unambiguous encoding of
its outline-relevant structure, including siblings its profile attaches to it:
the kinds of its named grammar nodes and, in source order, its text pieces. A
text piece MUST be the text of a leaf token, or text inside a node that no child
token covers. Each text piece MUST be hashed as its ordered list of words, where
words are separated by whitespace, the whitespace itself, including leading and
trailing whitespace, is never hashed, and each word is encoded separately, so
`ab cd` and `abcd` differ. The same rule MUST apply in every language. Comments
MUST be excluded, except comment kinds the profile includes; the Rust profile MUST include doc comments. For an included comment, only its text and its
profile's comment group MUST be hashed, not its delimiter tokens such as `//`,
`///`, `//!`, `/*`, `*/`, or a leading `*`; the Rust profile's groups are inner
and outer doc comments. The words of consecutive included comments of one group
MUST be hashed as one ordered list, so reflowing them across more or fewer lines
or comments does not change the hash, while moving text between groups does. A `line`
node's hash MUST be computed by the same word rule over the line's text. A node's
identity hash MUST be computed the same way but MUST also exclude its outline
descendants and their attached siblings, so editing a descendant does not change
its ancestors' selectors.
:::

Other sources

Input 1: requirements selection requirements.references.outline_selectors
:::{req} outline_selectors
An outline selector MUST be a `/`-separated path of segments from the file's
root node to an outline node; the empty selector MUST select the root node. A
named node's segment MUST be `kind[name]`. An anonymous node's segment MUST be
`kind@hash`, where `hash` is the first twelve hexadecimal digits of its identity
hash under {cite}`requirements.references.structural_hash`. Siblings whose
segments would otherwise be identical MUST be disambiguated by `#N`, their
one-based position among those siblings in source order, except `line` nodes:
identical lines are interchangeable, so a `line` selector MUST resolve when any
line with that content exists in the file. In names, `%`, `[`, `]`, `/`, `#`,
`@`, and whitespace MUST be percent-encoded as UTF-8 bytes, so selectors are
reversible. Line numbers MUST NOT form part of a selector.
:::