Rainbow 0.3.1: never paint begin/end invisible; unmatched go red

The rainbow painted depth 3-5 pairs with editorBracketHighlight
.foreground4..6, whose VS Code defaults are fully transparent — a begin
typed three levels deep vanished with its end. The extension now uses
contributed vfmt.rainbow1..6 colors with opaque defaults in every theme
variant (dark/light/HC/HC-light), and vfmt/scopePairs additionally
reports genuinely unmatched begin/end/fork/join tokens, painted
vfmt.rainbowUnmatched bright red — file characters are never hidden.
Matched pairs, depths and 78eeef6's counting are unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
lucioandClaude Fable 5 committed 2026-08-24 15:45:54 -07:00
1 parent d8e37086df
commit ab89152264
13 files changed
+827 -60

No files matched your search

+142 -12
View File
@@ -343,18 +343,27 @@ pub struct ScopePair {
#[derive(serde::Serialize)]
pub struct ScopePairs {
pub pairs: Vec<ScopePair>,
/// Block delimiters whose counterpart never arrived, in source order. The
/// extension paints these in its error colour: a keyword with no partner
/// has no depth and therefore no rainbow colour, and leaving it out of the
/// answer entirely is what used to leave it looking like ordinary text.
pub unmatched: Vec<Range>,
}
/// Every block-delimiter pair in a buffer, for the extension to colour.
/// Every block-delimiter pair in a buffer, plus the block delimiters that never
/// found a partner, 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.
/// so what reaches the wire and what was counted can never disagree. The same
/// list decides `unmatched`: a lone `end` is on the wire, a lone `endmodule` is
/// not, exactly as for matched pairs.
///
/// Unmatched keywords are not reported: there is nothing to colour when only
/// half a pair exists, and the missing-closer warning already says so.
/// A keyword is in `pairs` or in `unmatched`, never in both — `analyze` puts a
/// scope on exactly one of the three lists it returns.
pub fn scope_pairs(doc: &Document) -> ScopePairs {
let positions = doc.positions();
let pairs = analyze(&doc.tokens)
let analysis = analyze(&doc.tokens);
let pairs = analysis
.pairs
.iter()
.filter(|p| is_rainbow(p.keyword))
@@ -364,7 +373,19 @@ pub fn scope_pairs(doc: &Document) -> ScopePairs {
depth: p.depth,
})
.collect();
ScopePairs { pairs }
// An unmatched opener is a rainbow keyword itself; an unmatched closer
// names its opener in `expected`, which is the one to ask about (`end`
// belongs to `begin`, `endmodule` to `module`).
let mut lonely: Vec<TextRange> = analysis
.unmatched_open
.iter()
.filter(|u| is_rainbow(u.keyword))
.chain(analysis.unmatched_close.iter().filter(|u| is_rainbow(u.expected)))
.map(|u| u.span)
.collect();
lonely.sort_by_key(|span| span.start);
let unmatched = lonely.into_iter().map(|span| positions.range(span)).collect();
ScopePairs { pairs, unmatched }
}
// ----------------------------------------------------------------------
@@ -1008,13 +1029,15 @@ mod tests {
#[test]
fn scope_pairs_omits_a_pair_that_never_closed() {
// Half a pair is nothing to colour; the missing-closer warning is what
// speaks about it.
// Half a pair is nothing to *colour by depth* — it has none — so it
// stays out of `pairs` and goes on the wire as `unmatched` instead.
let text = "module m;\n initial begin\n if (x) begin\n end\nendmodule\n";
let doc = doc_of(text);
let sent = scope_pairs(&doc);
assert_eq!(sent.pairs.len(), 1, "only the inner begin closed");
assert_eq!(sent.pairs[0].open.start.line, 2);
assert_eq!(sent.unmatched.len(), 1, "the outer begin is reported, not dropped");
assert_eq!(sent.unmatched[0].start.line, 1);
}
#[test]
@@ -1022,16 +1045,119 @@ mod tests {
let doc = doc_of("module m;\n wire a;\nendmodule\n");
let sent = scope_pairs(&doc);
assert!(sent.pairs.is_empty());
assert!(sent.unmatched.is_empty());
let json = serde_json::to_value(&sent).expect("serializes");
assert_eq!(json, serde_json::json!({ "pairs": [] }), "the shape is stable");
assert_eq!(
json,
serde_json::json!({ "pairs": [], "unmatched": [] }),
"the shape is stable"
);
}
// ------------------------------------------------------------------
// vfmt/scopePairs — unmatched block delimiters
// ------------------------------------------------------------------
/// The unmatched ranges of a buffer as `(line, character)` starts.
fn unmatched_starts(text: &str) -> Vec<(u32, u32)> {
scope_pairs(&doc_of(text))
.unmatched
.iter()
.map(|r| (r.start.line, r.start.character))
.collect()
}
#[test]
fn an_opener_that_never_closes_is_reported_as_unmatched() {
// The 0.3.1 bug's own shape: three nested `begin`s in a task, the
// outermost one missing its `end`. Before this, the wire said nothing
// about it and the extension painted it with no decoration at all.
let text = concat!(
"module m;\n",
" task t;\n",
" begin\n", // line 2 — never closes
" begin\n", // line 3 — closes on line 6
" begin\n",
" end\n",
" end\n",
" endtask\n",
"endmodule\n",
);
assert_eq!(unmatched_starts(text), vec![(2, 4)], "the `begin` that lost its `end`");
// And the pairs that *did* close still carry the depth they always
// did: an unmatched frame is still an enclosing scope.
let depths: Vec<u32> = scope_pairs(&doc_of(text)).pairs.iter().map(|p| p.depth).collect();
assert_eq!(depths, vec![1, 2], "the two inner begins, unshifted");
}
#[test]
fn a_closer_that_opens_nothing_is_reported_as_unmatched() {
let text = "module m;\n initial begin\n end\n end\nendmodule\n";
assert_eq!(unmatched_starts(text), vec![(3, 2)], "the second `end` closes nothing");
assert_eq!(scope_pairs(&doc_of(text)).pairs.len(), 1, "the real pair is untouched");
}
#[test]
fn a_balanced_file_reports_nothing_unmatched() {
let text =
"module m;\n initial begin\n fork\n x = 1;\n join\n end\nendmodule\n";
assert!(scope_pairs(&doc_of(text)).unmatched.is_empty());
assert_eq!(scope_pairs(&doc_of(text)).pairs.len(), 2);
}
#[test]
fn a_keyword_is_never_both_paired_and_unmatched() {
// The two lists are painted with different colours, so an overlap
// would be two decorations fighting over one keyword.
let text = concat!(
"module m;\n",
" initial begin\n",
" begin\n",
" end\n",
" end\n",
" initial begin\n",
" end\n",
" end\n",
" initial fork\n",
"endmodule\n",
);
let sent = scope_pairs(&doc_of(text));
let paired: Vec<Range> = sent.pairs.iter().flat_map(|p| [p.open, p.close]).collect();
assert_eq!(paired.len(), 6, "three pairs");
assert_eq!(sent.unmatched.len(), 2, "the stray `end` and the unclosed `fork`");
for lonely in &sent.unmatched {
assert!(!paired.contains(lonely), "{lonely:?} is on both lists");
}
}
#[test]
fn unmatched_carries_only_block_delimiters() {
// Same filter as `pairs`: the rainbow does not draw `module` or
// `function`, so it does not report their halves either.
let text = "module m;\n function f;\n begin\n";
let sent = scope_pairs(&doc_of(text));
assert!(sent.pairs.is_empty());
assert_eq!(sent.unmatched.len(), 1, "the begin only, not the module or the function");
assert_eq!(sent.unmatched[0].start.line, 2);
// The engine itself still saw all three unclosed.
assert_eq!(analyze(&doc_of(text).tokens).unmatched_open.len(), 3);
}
#[test]
fn an_unmatched_join_is_reported_but_an_unmatched_endcase_is_not() {
// `unmatched_close` carries the closer; the filter asks about the
// *opener* it wanted, which is what `expected` holds.
assert_eq!(unmatched_starts("module m;\n join\nendmodule\n"), vec![(1, 2)]);
assert!(unmatched_starts("module m;\n endcase\nendmodule\n").is_empty());
}
#[test]
fn the_scope_pairs_wire_shape_is_what_the_extension_reads() {
// Pinned against the Handshakes section of the feature README: the
// extension indexes `pairs[].depth % 6` and hands `open`/`close`
// straight to the client's Range converter.
let doc = doc_of("module m;\n initial begin\n end\nendmodule\n");
// extension indexes `pairs[].depth % 6`, paints `unmatched` in its
// error colour, and hands every range straight to the client's Range
// converter.
let doc = doc_of("module m;\n initial begin\n end\n begin\nendmodule\n");
let json = serde_json::to_value(scope_pairs(&doc)).expect("serializes");
assert_eq!(
json,
@@ -1046,6 +1172,10 @@ mod tests {
"end": { "line": 2, "character": 5 }
},
"depth": 0
}],
"unmatched": [{
"start": { "line": 3, "character": 2 },
"end": { "line": 3, "character": 7 }
}]
})
);