Rainbow depth counts only the pairs the rainbow shows

User-reported: a begin inside a case (or any scope the rainbow filters
off the wire) painted one level deeper than its siblings — the hidden
scope read as an invisible begin/end layer. Depth now counts only
enclosing RAINBOW_KEYWORDS frames, one const shared by the counter and
the wire filter. Corpus: same 8,146 wire pairs, max depth 13 -> 7;
depth-0 bucket goes from near-empty to 2,881 (the palette was rotated).

Also closes M4 QA's e2e gap: the exclude test now asserts the republish
half of reindex(), and that a no-op settings push publishes nothing.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
lucioandClaude Fable 5 committed 2026-08-24 03:34:37 -07:00
1 parent 9a3a92cabb
commit 78eeef6050
4 files changed
+473 -37

No files matched your search

+137 -22
View File
@@ -56,6 +56,25 @@ const RULES: &[Rule] = &[
/// `typedef class C;`. The flag they raise is dropped at the next `;`.
const PROTOTYPE_STARTERS: &[&str] = &["extern", "pure", "import", "export", "typedef"];
/// The openers the rainbow colours: block delimiters only.
///
/// **One list, two jobs**, and they must never diverge: `scope_pairs` filters
/// the wire with it, and `analyze` counts `Pair::depth` with it. If a scope
/// were counted but not sent, the colours would skip a level for an invisible
/// reason — which is exactly the bug M5 fixed.
///
/// The other nineteen scopes stay off the wire. A whole-file `module` would
/// tint its own keywords a colour that means nothing, and `case`/`endcase`
/// alongside the `begin`s inside it makes the nesting read as deeper than it
/// is. What the user asked for is the `begin`/`end` ladder.
const RAINBOW_KEYWORDS: &[&str] = &["begin", "fork"];
/// Whether an opener is one the rainbow shows — and therefore one that counts
/// toward [`Pair::depth`].
fn is_rainbow(keyword: &str) -> bool {
RAINBOW_KEYWORDS.contains(&keyword)
}
/// A matched pair of keywords.
pub struct Pair {
pub open: TextRange,
@@ -63,10 +82,12 @@ pub struct Pair {
/// The opener as written, so a consumer can take only the scopes it wants
/// — the rainbow colours block delimiters and not `module`/`endmodule`.
pub keyword: &'static str,
/// How many scopes were already open when this one opened; 0 is outermost.
/// Counts *every* kind of frame, not just the one the caller kept, so
/// nesting differences survive filtering: the rainbow picks its colour
/// with `depth % 6`.
/// How many **rainbow** scopes were already open when this one opened; 0
/// is outermost. Only [`RAINBOW_KEYWORDS`] frames count, so a `begin`
/// directly inside a `module` is 0, and so is a `begin` inside a `case`
/// inside that module — the scopes the rainbow does not draw must not
/// shift the colour of the ones it does. The rainbow picks its colour with
/// `depth % 6`.
pub depth: u32,
}
@@ -96,6 +117,8 @@ pub struct ScopeAnalysis {
struct Frame {
rule: &'static Rule,
span: TextRange,
/// Enclosing rainbow scopes at the moment this one was pushed — see
/// [`Pair::depth`], which is where it ends up.
depth: u32,
}
@@ -126,7 +149,12 @@ pub fn analyze(tokens: &[Token]) -> ScopeAnalysis {
}
if let Some(rule) = opener_rule(text) {
if !prototype && opens_a_scope(tokens, i) {
let depth = stack.len() as u32;
// Count only the enclosing scopes the rainbow actually draws.
// Scanning the stack rather than keeping a running counter is
// both trivially cheap (it is a dozen frames deep at worst on
// the whole corpus) and immune to the mismatch recovery in
// `close`, which pops frames from the middle.
let depth = stack.iter().filter(|f| is_rainbow(f.rule.open)).count() as u32;
stack.push(Frame { rule, span: token.span, depth });
}
} else if let Some(closer) = closer_keyword(text) {
@@ -317,17 +345,11 @@ pub struct ScopePairs {
pub pairs: Vec<ScopePair>,
}
/// The keywords the rainbow colours: block delimiters only.
///
/// `module`/`endmodule`, `class`/`endclass` and the rest are matched by the
/// engine and deliberately dropped here. A whole-file `module` would tint its
/// own keywords a colour that means nothing, and `case`/`endcase` alongside
/// the `begin`s inside it makes the nesting read as deeper than it is. What
/// the user asked for is the `begin`/`end` ladder.
const RAINBOW_KEYWORDS: &[&str] = &["begin", "fork"];
/// Every block-delimiter pair in a buffer, for the extension to colour.
///
/// The filter and `Pair::depth`'s counter read the same [`RAINBOW_KEYWORDS`],
/// so what reaches the wire and what was counted can never disagree.
///
/// Unmatched keywords are not reported: there is nothing to colour when only
/// half a pair exists, and the missing-closer warning already says so.
pub fn scope_pairs(doc: &Document) -> ScopePairs {
@@ -335,7 +357,7 @@ pub fn scope_pairs(doc: &Document) -> ScopePairs {
let pairs = analyze(&doc.tokens)
.pairs
.iter()
.filter(|p| RAINBOW_KEYWORDS.contains(&p.keyword))
.filter(|p| is_rainbow(p.keyword))
.map(|p| ScopePair {
open: positions.range(p.open),
close: positions.range(p.close),
@@ -471,10 +493,11 @@ mod tests {
assert_eq!(analysis.pairs.len(), 3);
assert!(analysis.unmatched_open.is_empty());
assert!(analysis.unmatched_close.is_empty());
// Depth counts every open frame, not just the begins: the module is
// outermost, so the first begin is already one deep.
// Depth counts only the scopes the rainbow draws. The module is one of
// the nineteen it does not, so it is 0 and does not push the `begin`
// under it off colour 0.
let depths: Vec<u32> = analysis.pairs.iter().map(|p| p.depth).collect();
assert_eq!(depths, vec![0, 1, 2], "module, outer begin, inner begin");
assert_eq!(depths, vec![0, 0, 1], "module (uncounted), outer begin, inner begin");
}
#[test]
@@ -873,9 +896,101 @@ mod tests {
let text = "module top;\n initial begin\n if (a) begin\n b = 1;\n end\n end\n initial fork\n c = 1;\n join\nendmodule\n";
let doc = doc_of(text);
let depths: Vec<u32> = scope_pairs(&doc).pairs.iter().map(|p| p.depth).collect();
// The module is depth 0 and filtered out; the begins under it are 1
// and 2, and the fork is a sibling of the outer begin.
assert_eq!(depths, vec![1, 2, 1]);
// The module does not count, so the outer begin is 0; the begin inside
// it is 1; the fork is the outer begin's sibling, so 0 again.
assert_eq!(depths, vec![0, 1, 0]);
}
#[test]
fn a_scope_the_rainbow_hides_does_not_shift_the_colour_of_one_it_shows() {
// The M5 bug, as the user reported it: "for blocks that don't have
// begin/end, like inside of a case statement, I can see an invisible
// begin/end being highlighted." The `case` was counted but never
// drawn, so the `begin` inside it came out one colour deeper than its
// sibling outside — a phantom level with no keyword on screen.
let text = concat!(
"module m;\n",
" initial begin\n", // depth 0
" x = 1;\n",
" end\n",
" always_comb begin\n", // depth 0 — the sibling to match
" case (y)\n", // hidden: must not count
" 1: begin\n", // depth 1, NOT 2
" z = 1;\n",
" end\n",
" endcase\n",
" end\n",
"endmodule\n",
);
let doc = doc_of(text);
let sent = scope_pairs(&doc);
let depths: Vec<u32> = sent.pairs.iter().map(|p| p.depth).collect();
assert_eq!(depths, vec![0, 0, 1], "the two outer begins are siblings");
assert_eq!(sent.pairs.len(), 3, "the case itself is still not on the wire");
}
#[test]
fn no_hidden_scope_counts_toward_the_depth() {
// Every scope kind the rainbow filters out, one at a time, wrapping a
// `begin` that must still come out at depth 0. A new entry in `RULES`
// that forgets to stay out of `RAINBOW_KEYWORDS` fails here.
let cases = [
("module m;\n initial begin\n end\nendmodule\n", "module"),
(
"package p;\n function f;\n begin\n end\n endfunction\nendpackage\n",
"package+function",
),
(
"module m;\n generate\n if (A) begin\n end\n endgenerate\nendmodule\n",
"generate",
),
("module m;\n task t;\n begin\n end\n endtask\nendmodule\n", "task"),
(
"class c;\n function new();\n begin\n end\n endfunction\nendclass\n",
"class+function",
),
("interface i;\n initial begin\n end\nendinterface\n", "interface"),
("program p;\n initial begin\n end\nendprogram\n", "program"),
(
"module m;\n specify\n if (a) begin\n end\n endspecify\nendmodule\n",
"specify",
),
];
for (text, what) in cases {
let doc = doc_of(text);
let sent = scope_pairs(&doc);
assert_eq!(sent.pairs.len(), 1, "{what}: exactly one begin on the wire");
assert_eq!(sent.pairs[0].depth, 0, "{what} must not count toward depth");
}
}
#[test]
fn only_begins_and_forks_stack_up_the_colours() {
// A begin in a fork in a begin is three distinct levels, because all
// three are drawn.
let text = "module m;\n initial begin\n fork\n begin\n x = 1;\n end\n join\n end\nendmodule\n";
let doc = doc_of(text);
let depths: Vec<u32> = scope_pairs(&doc).pairs.iter().map(|p| p.depth).collect();
assert_eq!(depths, vec![0, 1, 2], "begin, fork, begin");
}
#[test]
fn the_depth_counter_and_the_wire_filter_read_the_same_list() {
// The invariant that keeps the M5 bug from coming back: a scope is
// counted if and only if it is sent. Asserted over the whole rule
// table rather than by re-reading the const.
for rule in RULES {
let counted = is_rainbow(rule.open);
let text = format!("module m;\n {} \nendmodule\n", rule.open);
let _ = doc_of(&text); // never panics on any opener, closed or not
assert_eq!(
counted,
RAINBOW_KEYWORDS.contains(&rule.open),
"`{}` must be counted exactly when it is sent",
rule.open
);
}
assert_eq!(RAINBOW_KEYWORDS, ["begin", "fork"], "the set the extension was built for");
}
#[test]
@@ -930,7 +1045,7 @@ mod tests {
"start": { "line": 2, "character": 2 },
"end": { "line": 2, "character": 5 }
},
"depth": 1
"depth": 0
}]
})
);