requirements.references.selector_raw_lines
Requirements index · View definition
Example
For a function preceded by a doc comment, an attribute, and an attached citation comment, an outline input’s displayed range starts at the earliest attached line and ends at the function’s final line. Inserting lines before the function changes the displayed range without changing the input. Editing the citation comment leaves its hash unchanged; editing the doc comment changes it.
![]()
Rationale
The example is concrete. It describes a function preceded by a doc comment, an attribute and an attached citation comment. It states four expected outcomes:
- The displayed range starts at the earliest attached line and ends at the function’s last line.
- Inserting lines before the function changes the range but not the input.
- Editing the citation comment leaves the hash unchanged.
- Editing the doc comment changes the hash.
Two tests carry {test} references to it.
src/outline/tests.rs#fn[hashes_skip_whitespace_and_plain_comments_but_not_doc_comments] builds exactly this scenario. The source is a doc comment, #[inline], a citation comment, then fn run() {...}.
- Range: it asserts the node’s
(line_start, line_end)is(1, 4). That runs from the doc comment’s first line to the function’s last line. - Inserted lines: it prepends two blank lines. The structural hash is unchanged and
line_startmoves to 3. - Citation edit: it changes the citation comment’s text and also reformats the function body’s whitespace. The hash is unchanged.
- Doc edit: changing
Doc.toDocs.changes the hash. - Other edits: changing the attribute or the code also changes the hash, which is consistent with the example.
tests/evidence_boundaries.rs#fn[outline_inputs_survive_moves_and_unhashed_comments_but_not_edits] covers the same behavior through input validation.
- Inserted lines: it prepends a comment and a blank line and reformats
alpha. The recorded inputs still validate, so a moved range does not invalidate them. - Doc edit: editing beta’s doc comment makes its input stale. The error reports the current range as the doc comment’s first line through the function’s last line.
Together the tests cover the scenario’s setup and assert each stated outcome. I found no gaps.
Inputs
Tests
Input 4: outline src/outline/tests.rs fn[hashes_skip_whitespace_and_plain_comments_but_not_doc_comments]
- File:
src/outline/tests.rs - Selector:
fn[hashes_skip_whitespace_and_plain_comments_but_not_doc_comments] - Kind:
fn src/outline/tests.rs:338–377
// {test}`requirements.references.structural_hash`
// {test}`requirements.references.selector_raw_lines`
// {test}`requirements.references.outlines`
#[test]
fn hashes_skip_whitespace_and_plain_comments_but_not_doc_comments() {
let base = concat!(
"/// Doc.\n#[inline]\n// {cite}",
"`a.b`\nfn run() { let x = 1; }\n"
);
let hash = |source: &str| {
let outline = outline("src/lib.rs", source);
let node = &outline.nodes[outline.resolve("fn[run]").unwrap()];
(node.structural_hash.clone(), node.line_start, node.line_end)
};
let (original, start, end) = hash(base);
assert_eq!((start, end), (1, 4));
let (moved, start, _) = hash(&format!("\n\n{base}"));
assert_eq!((moved.as_str(), start), (original.as_str(), 3));
assert_eq!(
hash(concat!(
"/// Doc.\n#[inline]\n// {cite}",
"`c.d`\nfn run() {\n let x = 1;\n}\n"
))
.0,
original
);
assert_ne!(hash(&base.replace("Doc.", "Docs.")).0, original);
assert_ne!(hash(&base.replace("inline", "cold")).0, original);
assert_ne!(hash(&base.replace("= 1", "= 2")).0, original);
let parent = |source: &str| {
let outline = outline("src/lib.rs", source);
let node = &outline.nodes[outline.resolve("mod[m]").unwrap()];
(node.structural_hash.clone(), node.identity_hash.clone())
};
let (structural, identity) = parent("mod m { fn a() {} }");
let (edited_structural, edited_identity) = parent("mod m { fn a() { 1; } }");
assert_ne!(structural, edited_structural);
assert_eq!(identity, edited_identity);
}
Input 5: outline tests/evidence_boundaries.rs fn[outline_inputs_survive_moves_and_unhashed_comments_but_not_edits]
- File:
tests/evidence_boundaries.rs - Selector:
fn[outline_inputs_survive_moves_and_unhashed_comments_but_not_edits] - Kind:
fn tests/evidence_boundaries.rs:64–182
// {test}`requirements.ci.outline_inputs`
// {test}`requirements.ci.input_containment`
// {test}`requirements.ci.diagnostics`
// {test}`requirements.ci.schema`
// {test}`requirements.ci.fixme_stale_input`
// {test}`requirements.references.selector_raw_lines`
// {test}`requirements.references.structural_hash`
#[test]
fn outline_inputs_survive_moves_and_unhashed_comments_but_not_edits() {
let temp = fixture();
let root = temp.path().join("project");
// Citation reviews of the cited functions are checked elsewhere.
let errors = |inputs: Vec<Value>| {
write_inputs(&root, inputs);
without_citation_reviews(validate_repository(&root).unwrap().errors).join("\n")
};
let rust = "// {cite}`rules.ready`\nfn alpha() { work(1); }\n\n/// Docs.\n// {cite}`rules.ready`\n#[allow(dead_code)]\nfn beta() {}\n";
fs::write(root.join("src/lib.rs"), rust).unwrap();
let inputs = vec![
outline(&root, "src/lib.rs", "fn[alpha]"),
outline(&root, "src/lib.rs", "fn[beta]"),
habeas::outline_inputs_for_lines(&root, "data.txt", 2, 2)
.unwrap()
.remove(0),
];
assert_eq!(errors(inputs.clone()), "");
// Moving nodes and lines, and editing unhashed comments, keeps inputs valid.
fs::write(
root.join("src/lib.rs"),
format!("// {{cite}}`rules.ready` moved below a new line\n\n{rust}")
.replace("fn alpha() { work(1); }", "fn alpha() {\n work(1);\n}"),
)
.unwrap();
fs::write(root.join("data.txt"), "zero\nfirst\nsecond\n").unwrap();
assert_eq!(errors(inputs.clone()), "");
// Editing another node must not invalidate beta's recorded input.
fs::write(root.join("src/lib.rs"), rust.replace("work(1)", "work(2)")).unwrap();
assert_eq!(errors(vec![inputs[1].clone()]), "");
// Editing a node's code or its doc comment makes the input stale, naming the
// file, selector, and current line range.
fs::write(
root.join("src/lib.rs"),
rust.replace("work(1)", "work(2)")
.replace("/// Docs.", "/// Changed docs."),
)
.unwrap();
let found = errors(inputs.clone());
for expected in [
"inputs[0]: outline input changed: src/lib.rs fn[alpha] (now lines 1-2) no longer has the recorded hash",
"inputs[1]: outline input changed: src/lib.rs fn[beta] (now lines 4-7) no longer has the recorded hash",
] {
assert!(found.contains(expected), "missing {expected:?}: {found}");
}
assert!(!found.contains("inputs[2]"), "{found}");
assert!(!found.contains("work(2)"), "no diff: {found}");
// An edited plain-text line is a different node.
fs::write(root.join("data.txt"), "first\nchanged\n").unwrap();
let found = errors(inputs[2..].to_vec());
assert!(
found.contains("data.txt: no outline node matches \"line@"),
"{found}"
);
fs::write(
root.join("src/lib.rs"),
"// {cite}`rules.ready`\n#[cfg(a)]\nfn dup() {}\n// {cite}`rules.ready`\n#[cfg(b)]\nfn dup() {}\n",
)
.unwrap();
let hash = "0".repeat(64);
for (input, expected) in [
(
json!({"file":"src/lib.rs", "item":"fn[missing]", "hash":hash}),
"src/lib.rs: no outline node matches \"fn[missing]\"",
),
(
json!({"file":"src/lib.rs", "item":"fn[dup]", "hash":hash}),
"src/lib.rs: outline selector \"fn[dup]\" is ambiguous",
),
(
json!({"file":"src/lib.rs", "item":"fn[unclosed", "hash":hash}),
"src/lib.rs: invalid outline selector \"fn[unclosed\"",
),
(
json!({"file":"src/lib.rs", "item":"fn[dup]#2", "hash":"ABC"}),
"hash must be a lowercase hexadecimal SHA-256 digest",
),
(
json!({"file":"missing.txt", "item":"", "hash":hash}),
"cannot open missing.txt",
),
(
json!({"file":"data.txt", "line_start":1, "line_end":1, "text":["first"]}),
"unexpected field \"line_start\"",
),
(
json!({"rust_file":"src/lib.rs", "rust_item":"fn[dup]#1", "text":["fn dup() {}"]}),
"unexpected field \"rust_file\"",
),
(
json!({"file":"src/lib.rs", "item":"fn[dup]#1", "hash":hash, "text":["fn dup() {}"]}),
"unexpected field \"text\"",
),
] {
let found = errors(vec![input]);
assert!(found.contains(expected), "missing {expected:?}: {found}");
}
fs::write(root.join("src/broken.rs"), "fn {").unwrap();
let found = errors(vec![
json!({"file":"src/broken.rs", "item":"", "hash":hash}),
]);
assert!(
found.contains("src/broken.rs: cannot parse rust source"),
"{found}"
);
}
Referenced definitions
Input 2: requirements selection requirements.ci.outline_inputs
requirements.ci.outline_inputs:md/requirements/ci.md:172–183
:::{req} outline_inputs
For an outline input, Habeas MUST parse `file` with its language's grammar under
{cite}`requirements.references.outlines` and resolve `item` to exactly one
outline node under {cite}`requirements.references.outline_selectors`. It MUST
compare `hash` with the node's current structural hash under
{cite}`requirements.references.structural_hash`. It MUST derive the node's
inclusive line range, including attached comments and attributes, during
validation for display and reference coverage. Moving the node, editing other
nodes, or editing comments its profile does not hash MUST NOT invalidate the
input. Habeas MUST report a missing file, unsupported language, parse failure,
invalid or ambiguous selector, or changed hash.
:::
Input 3: requirements selection requirements.references.structural_hash
requirements.references.structural_hash:md/requirements/references.md:394–413
:::{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.selector_raw_lines
requirements.references.selector_raw_lines:md/requirements/references.md:461–463
:::{example} selector_raw_lines
For a function preceded by a doc comment, an attribute, and an attached citation comment, an outline input's displayed range starts at the earliest attached line and ends at the function's final line. Inserting lines before the function changes the displayed range without changing the input. Editing the citation comment leaves its hash unchanged; editing the doc comment changes it. {cite}`requirements.ci.outline_inputs` {cite}`requirements.references.structural_hash`
:::