release / build-and-publish (push) Successful in 5m44s
TODO §71.4. Each conditional-compilation branch (`ifdef, `ifndef, `elsif, `else) is a folding range from its directive line to the line before the next branch or `endif, kind region, nested to any depth; sticky scroll, which already follows the server's folds, pins the branch line. Built on the lexer's directive tokens (folding/directives.rs), so directives in comments, strings and `define bodies are not folded; no parser or formatter change. An existing fold is never lost or changed: scope and comment folds are computed as before and sent first at a shared start line (folding::in_answer_order). Proven on OpenTitan hw (4 061 files, 1 061 branch folds) and the corpus (860 files, 511): non-directive folds byte-identical to 0.65.3 (QA, base built from git archive), 10 000-case fuzz against an independent model. Dev, QA (pass with findings, one fix round) and lead review are in docs/features/ifdef-folding/README.md. Not seen in a running editor. Version bump (Cargo.toml, Cargo.lock, editors/vscode/package.json and both package-lock.json spots); notes in CHANGELOG.md (## 0.65.4) and editors/vscode/CHANGELOG.md. Gates: tools/gates.sh PASS on this tree, lucisv_server 1977/0. Co-Authored-By: Claude Mythos 5.2 <[email protected]>
338 lines
11 KiB
Rust
338 lines
11 KiB
Rust
//! `` `ifdef `` branch folds, through the whole [`folding_ranges`] answer so
|
|
//! every test also sees how they sit beside the scope and comment folds.
|
|
|
|
use lsp_types::FoldingRangeKind;
|
|
use vlog_semantic::WorkspaceIndex;
|
|
|
|
use super::folding_ranges;
|
|
use crate::state::Document;
|
|
|
|
fn doc_of(text: &str) -> Document {
|
|
Document::new(None, text.to_string(), &WorkspaceIndex::default())
|
|
}
|
|
|
|
/// Every fold as `(start line, end line, kind)`, kinds spelled short, after
|
|
/// checking the one ordering promise every answer keeps: by start line.
|
|
fn folds(text: &str) -> Vec<(u32, u32, &'static str)> {
|
|
let ranges = folding_ranges(&doc_of(text));
|
|
assert!(ranges.windows(2).all(|w| w[0].start_line <= w[1].start_line), "{ranges:?}");
|
|
ranges
|
|
.into_iter()
|
|
.map(|range| {
|
|
let kind = match range.kind {
|
|
Some(FoldingRangeKind::Comment) => "comment",
|
|
Some(FoldingRangeKind::Region) => "region",
|
|
other => panic!("unexpected kind {other:?}"),
|
|
};
|
|
(range.start_line, range.end_line, kind)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// The folds as `(start, end)` alone, for tests about lines rather than kinds.
|
|
fn lines(text: &str) -> Vec<(u32, u32)> {
|
|
folds(text).into_iter().map(|(start, end, _)| (start, end)).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn each_branch_folds_from_its_directive_to_the_line_before_the_next() {
|
|
// Collapsing the `ifdef` branch leaves `else` on screen and separately
|
|
// collapsible; collapsing the `else` branch leaves `endif` on screen.
|
|
let text = concat!(
|
|
"module m;\n", // 0
|
|
"`ifdef A\n", // 1
|
|
" wire a;\n", // 2
|
|
" wire b;\n", // 3
|
|
"`else\n", // 4
|
|
" wire c;\n", // 5
|
|
"`endif\n", // 6
|
|
"endmodule\n", // 7
|
|
);
|
|
assert_eq!(folds(text), vec![(0, 6, "region"), (1, 3, "region"), (4, 5, "region")]);
|
|
}
|
|
|
|
#[test]
|
|
fn an_elsif_chain_gives_every_arm_its_own_fold() {
|
|
let text = concat!(
|
|
"`ifndef A\n", // 0
|
|
" wire a;\n", // 1
|
|
"`elsif B\n", // 2
|
|
" wire b;\n", // 3
|
|
"`elsif C\n", // 4
|
|
" wire c;\n", // 5
|
|
"`else\n", // 6
|
|
" wire d;\n", // 7
|
|
"`endif\n", // 8
|
|
);
|
|
assert_eq!(lines(text), vec![(0, 1), (2, 3), (4, 5), (6, 7)]);
|
|
}
|
|
|
|
#[test]
|
|
fn nested_regions_fold_at_every_depth() {
|
|
let text = concat!(
|
|
"`ifdef A\n", // 0
|
|
" `ifdef B\n", // 1
|
|
" `ifndef C\n", // 2
|
|
" wire c;\n", // 3
|
|
" `endif\n", // 4
|
|
" `else\n", // 5
|
|
" wire nb;\n", // 6
|
|
" `endif\n", // 7
|
|
"`else\n", // 8
|
|
" `ifdef D\n", // 9
|
|
" wire d;\n", // 10
|
|
" `endif\n", // 11
|
|
"`endif\n", // 12
|
|
);
|
|
assert_eq!(
|
|
lines(text),
|
|
vec![(0, 7), (1, 4), (2, 3), (5, 6), (8, 11), (9, 10)],
|
|
"A, B, C, B's else, A's else, D"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_branch_with_nothing_in_it_folds_nothing() {
|
|
let text = concat!(
|
|
"`ifdef A\n", // 0
|
|
"`else\n", // 1
|
|
" wire b;\n", // 2
|
|
"`endif\n", // 3
|
|
"`ifdef C\n", // 4
|
|
"`endif\n", // 5
|
|
);
|
|
assert_eq!(lines(text), vec![(1, 2)], "only the else branch holds a line");
|
|
}
|
|
|
|
#[test]
|
|
fn a_one_line_region_is_one_token_and_folds_nothing() {
|
|
// A directive runs to the end of its line, so the `endif` here is the
|
|
// `ifdef` token's text, not a directive of its own.
|
|
let text = "module m;\n`ifdef A wire a; `else wire b; `endif\nendmodule\n";
|
|
let doc = doc_of(text);
|
|
let directives = doc.tokens.iter().filter(|t| t.text.starts_with("`ifdef")).count();
|
|
assert_eq!(directives, 1);
|
|
assert_eq!(lines(text), vec![(0, 1)], "the module only");
|
|
}
|
|
|
|
#[test]
|
|
fn an_unterminated_region_folds_only_the_branches_it_finished() {
|
|
// Same policy as an unclosed `begin`: never fold to the end of the file.
|
|
let text = concat!(
|
|
"`ifdef A\n", // 0
|
|
" wire a;\n", // 1
|
|
"`else\n", // 2
|
|
" wire b;\n", // 3
|
|
" wire c;\n", // 4
|
|
);
|
|
assert_eq!(lines(text), vec![(0, 1)]);
|
|
assert!(lines("`ifdef A\n wire a;\n wire b;\n").is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn stray_else_and_endif_are_ignored_without_disturbing_what_follows() {
|
|
let text = concat!(
|
|
"`else\n", // 0
|
|
" wire x;\n", // 1
|
|
"`endif\n", // 2
|
|
"`endif\n", // 3
|
|
"`ifdef A\n", // 4
|
|
" wire a;\n", // 5
|
|
"`endif\n", // 6
|
|
"`elsif B\n", // 7
|
|
" wire b;\n", // 8
|
|
);
|
|
assert_eq!(lines(text), vec![(4, 5)]);
|
|
}
|
|
|
|
#[test]
|
|
fn directives_between_ports_fold() {
|
|
let text = concat!(
|
|
"module m (\n", // 0
|
|
" input a,\n", // 1
|
|
"`ifdef B\n", // 2
|
|
" input b,\n", // 3
|
|
" input c,\n", // 4
|
|
"`endif\n", // 5
|
|
" input d\n", // 6
|
|
");\n", // 7
|
|
"endmodule\n", // 8
|
|
);
|
|
assert!(lines(text).contains(&(2, 4)), "got {:?}", lines(text));
|
|
}
|
|
|
|
#[test]
|
|
fn directives_inside_case_and_generate_fold() {
|
|
let text = concat!(
|
|
"module m;\n", // 0
|
|
" generate\n", // 1
|
|
"`ifdef G\n", // 2
|
|
" if (A) begin : g\n", // 3
|
|
" wire a;\n", // 4
|
|
" end\n", // 5
|
|
"`endif\n", // 6
|
|
" endgenerate\n", // 7
|
|
" always_comb begin\n", // 8
|
|
" case (s)\n", // 9
|
|
" 0: x = 0;\n", // 10
|
|
"`ifdef C\n", // 11
|
|
" 1: x = 1;\n", // 12
|
|
"`else\n", // 13
|
|
" 1: x = 2;\n", // 14
|
|
"`endif\n", // 15
|
|
" endcase\n", // 16
|
|
" end\n", // 17
|
|
"endmodule\n", // 18
|
|
);
|
|
assert_eq!(
|
|
lines(text),
|
|
vec![(0, 17), (1, 6), (2, 5), (3, 4), (8, 16), (9, 15), (11, 12), (13, 14)]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_directive_inside_a_define_body_is_text_not_a_fold() {
|
|
let text = concat!(
|
|
"`define M \\\n", // 0
|
|
" `ifdef X \\\n", // 1
|
|
" a = 1; \\\n", // 2
|
|
" `else \\\n", // 3
|
|
" a = 2; \\\n", // 4
|
|
" `endif\n", // 5
|
|
"module m;\n", // 6
|
|
" wire a;\n", // 7
|
|
"endmodule\n", // 8
|
|
);
|
|
assert_eq!(lines(text), vec![(6, 7)], "the module only");
|
|
}
|
|
|
|
#[test]
|
|
fn a_directive_in_a_comment_or_a_string_is_not_one() {
|
|
let text = concat!(
|
|
"module m;\n", // 0
|
|
" // `ifdef A\n", // 1
|
|
" /* `ifdef B\n", // 2
|
|
" `endif */\n", // 3
|
|
" initial $display(\"`ifdef\");\n", // 4
|
|
" // `endif\n", // 5
|
|
"endmodule\n", // 6
|
|
);
|
|
assert_eq!(folds(text), vec![(0, 5, "region"), (2, 3, "comment")]);
|
|
}
|
|
|
|
#[test]
|
|
fn leading_whitespace_trailing_comments_and_crlf_change_nothing() {
|
|
let plain = concat!(
|
|
"module m;\n", // 0
|
|
" `ifdef A // why\n", // 1
|
|
" wire a;\n", // 2
|
|
"\t`else /* other */\n", // 3
|
|
" wire b;\n", // 4
|
|
" `endif // A\n", // 5
|
|
"endmodule\n", // 6
|
|
);
|
|
assert_eq!(lines(plain), vec![(0, 5), (1, 2), (3, 4)]);
|
|
assert_eq!(lines(&plain.replace('\n', "\r\n")), lines(plain));
|
|
}
|
|
|
|
#[test]
|
|
fn a_region_crossing_a_begin_keeps_both_sets_of_folds() {
|
|
// Real RTL opens one `begin` in each arm and closes it once below the
|
|
// `endif`. The branch folds cross the `begin` folds; VS Code keeps
|
|
// crossing ranges (`sanitizeRanges` adds them at the level of the range
|
|
// they cross; see the feature README), so nothing is dropped here and
|
|
// nothing is invented either.
|
|
let text = concat!(
|
|
"module m;\n", // 0
|
|
" always_comb begin\n", // 1
|
|
"`ifdef A\n", // 2
|
|
" if (a) begin\n", // 3
|
|
"`else\n", // 4
|
|
" if (b) begin\n", // 5
|
|
"`endif\n", // 6
|
|
" x = 1;\n", // 7
|
|
" end\n", // 8
|
|
" end\n", // 9
|
|
"endmodule\n", // 10
|
|
);
|
|
// The scope pairing sees three `begin`s and two `end`s, matches the inner
|
|
// two (`(5, 7)`, `(3, 8)`) and leaves the `always_comb` one out — that is
|
|
// `pairs::analyze`, unchanged by this feature. The branches are added
|
|
// beside it: `(2, 3)` crosses `(3, 8)`, `(4, 5)` crosses `(5, 7)`.
|
|
assert_eq!(lines(text), vec![(0, 9), (2, 3), (3, 8), (4, 5), (5, 7)]);
|
|
}
|
|
|
|
#[test]
|
|
fn branch_folds_are_regions() {
|
|
let text = "`ifdef A\n wire a;\n`endif\n";
|
|
assert_eq!(folds(text), vec![(0, 1, "region")]);
|
|
}
|
|
|
|
#[test]
|
|
fn back_to_back_regions_each_fold() {
|
|
let text = concat!(
|
|
"`ifdef A\n", // 0
|
|
" wire a;\n", // 1
|
|
"`endif\n", // 2
|
|
"`ifdef B\n", // 3
|
|
" wire b;\n", // 4
|
|
"`endif\n", // 5
|
|
);
|
|
assert_eq!(lines(text), vec![(0, 1), (3, 4)]);
|
|
}
|
|
|
|
// A shared start line. VS Code keeps only the first range it receives for a
|
|
// start line, so a fold that existed before branch folds must come first and
|
|
// the branch fold is the one that yields (QA findings 1 and 2, 2026-11-19).
|
|
|
|
#[test]
|
|
fn a_module_header_sharing_its_line_with_an_ifdef_keeps_its_fold_first() {
|
|
let text = "module m; `ifdef A\n a;\n`endif\nendmodule\n";
|
|
assert_eq!(lines(text), vec![(0, 2), (0, 1)], "the module first, the branch after it");
|
|
}
|
|
|
|
#[test]
|
|
fn a_begin_sharing_its_line_with_an_ifdef_keeps_its_fold_first() {
|
|
let text = concat!(
|
|
"module m;\n", // 0
|
|
"initial begin `ifdef X\n", // 1
|
|
" a;\n", // 2
|
|
" b;\n", // 3
|
|
" `endif\n", // 4
|
|
" c;\n", // 5
|
|
"end\n", // 6
|
|
"endmodule\n", // 7
|
|
);
|
|
assert_eq!(lines(text), vec![(0, 6), (1, 5), (1, 3)]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_opened_on_a_directive_line_keeps_its_fold_first() {
|
|
// The comment is the shorter range but the older fold, so it is first,
|
|
// and in VS Code the branch from line 0 is the one that yields.
|
|
let text = concat!(
|
|
"`ifdef A /* c1\n", // 0
|
|
" c2 */\n", // 1
|
|
" x;\n", // 2
|
|
"`else\n", // 3
|
|
" b;\n", // 4
|
|
"`endif\n", // 5
|
|
);
|
|
assert_eq!(folds(text), vec![(0, 1, "comment"), (0, 2, "region"), (3, 4, "region")]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_spanning_exactly_a_branch_stays_a_comment() {
|
|
// Same two lines as the branch: one range, and it keeps the kind it had
|
|
// before branch folds existed, so *Fold All Block Comments* still folds it.
|
|
let text = concat!(
|
|
"`ifdef A /* c1\n", // 0
|
|
" c2\n", // 1
|
|
" c3 */\n", // 2
|
|
"`else\n", // 3
|
|
" b;\n", // 4
|
|
"`endif\n", // 5
|
|
);
|
|
assert_eq!(folds(text), vec![(0, 2, "comment"), (3, 4, "region")]);
|
|
}
|