range-formatting: --lines M-N[,M-N...] formats only the top-level
items intersecting the ranges; modules/packages split head/body/tail
with independent decisions, everything else is atomic, and
unselected units are emitted as merged verbatim source slices (the
run-merge is load-bearing: per-item slices let separators collapse
blank lines between untouched items). Convergence via Selection
replay -- same-selection re-runs are fixed points, sound because the
equivalence check has already proven token identity; QA's sabotage
of the replay failed immediately on real RTL. The honest limit
(same line numbers can sweep in a neighbor after lines move;
449/8236 corpus trials, all settled next run) is measured and
pinned. Fence wins over range; alignment groups split at verbatim
boundaries; cache fully bypassed; LSP rangeFormatting advertised and
consumes only lines (no UTF-16 column math); tools/vfmt-changed.sh
derives ranges from git diff. Default behavior provably unchanged
(TSV md5 + all-578-file byte-compare vs a pre-round binary).
lint-rules-2: visit.rs walks class bodies (closing the classes
handshake; +6 real findings in UVM code), five new rules with
evidence-based defaults -- parameter/enum/struct-union-name-style ON
with Verible's exact names and published defaults (vendored waivers
now honored), module-name-style OFF (Verible has no such rule; the
brief's premise was checked and corrected), wire-reg-mixup ON (0
corpus noise in .v, 209 findings on a .sv rename -- both zeroes
proven). rules_markdown() generates the rule reference; 13 rules
total, zero CLI/server wiring needed.
Also: lead notes in classes and shared-fence-filter READMEs
documenting the fence asymmetry lint-rules-2 QA found (a fence in a
class body protects fixes via the scan backstop even though the
formatter ignores it there).
768 workspace tests. Dev: 2 Opus agents; QA: 2 Sonnet agents;
reviews in docs/features/{range-formatting,lint-rules-2}/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
418 lines
14 KiB
Rust
418 lines
14 KiB
Rust
//! `--lines` range formatting: only the selected constructs are laid out, and
|
|
//! every other byte comes back exactly as it was written.
|
|
//!
|
|
//! `.cases` tables cannot express a range, so this is where the engine's
|
|
//! contract lives. Spec: `docs/features/range-formatting/README.md`.
|
|
|
|
use std::fs;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use vlog_formatter::{format_range, format_source, Config, LineRanges};
|
|
|
|
fn ranged(src: &str, spec: &str) -> String {
|
|
let lines = LineRanges::parse(spec).expect("range spec");
|
|
format_range(src, &Config::default(), &lines).expect("formats")
|
|
}
|
|
|
|
fn whole(src: &str) -> String {
|
|
format_source(src, &Config::default()).expect("formats")
|
|
}
|
|
|
|
/// Format twice with the same ranges and assert the second run changed
|
|
/// nothing.
|
|
///
|
|
/// This holds whenever the first run did not move the selected lines, which is
|
|
/// every case below; `a_line_shift_can_sweep_in_the_next_construct` is the
|
|
/// counterexample and the reason the *runtime* convergence check replays the
|
|
/// per-item decisions instead of the line numbers.
|
|
fn fixed_point(src: &str, spec: &str) -> String {
|
|
let out = ranged(src, spec);
|
|
assert_eq!(ranged(&out, spec), out, "a second run with the same ranges must change nothing");
|
|
out
|
|
}
|
|
|
|
const TWO_MODULES: &str = "\
|
|
// top note
|
|
module m (input a,output b);
|
|
wire x;
|
|
|
|
wire y;
|
|
|
|
|
|
assign x=a;
|
|
// dangling
|
|
endmodule // done
|
|
|
|
module n;
|
|
wire z;
|
|
endmodule
|
|
";
|
|
|
|
// ------------------------------------------------------------------
|
|
// The unit of formatting
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn only_the_selected_body_item_is_laid_out() {
|
|
let out = fixed_point(TWO_MODULES, "3");
|
|
assert_eq!(
|
|
out,
|
|
"\
|
|
// top note
|
|
module m (input a,output b);
|
|
wire x;
|
|
|
|
wire y;
|
|
|
|
|
|
assign x=a;
|
|
// dangling
|
|
endmodule // done
|
|
|
|
module n;
|
|
wire z;
|
|
endmodule
|
|
"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_module_header_is_laid_out_without_its_body() {
|
|
// This is the whole point of descending: a three-line edit in the port
|
|
// list must not reflow two thousand lines of body.
|
|
let out = fixed_point(TWO_MODULES, "2");
|
|
assert!(out.contains("module m (input a, output b);"), "{out}");
|
|
assert!(out.contains(" wire x;"), "the body is untouched: {out}");
|
|
assert!(out.contains("endmodule // done"), "and so is the footer: {out}");
|
|
}
|
|
|
|
#[test]
|
|
fn the_closing_keyword_is_laid_out_on_its_own() {
|
|
let out = fixed_point(TWO_MODULES, "10");
|
|
assert!(out.contains("endmodule // done"), "{out}");
|
|
assert!(out.contains("module m (input a,output b);"), "the header stays: {out}");
|
|
assert!(out.contains(" wire x;"), "the body stays: {out}");
|
|
}
|
|
|
|
#[test]
|
|
fn an_unselected_module_is_emitted_verbatim() {
|
|
let out = fixed_point(TWO_MODULES, "3");
|
|
assert!(out.contains("module n;\n wire z;\nendmodule\n"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_range_that_meets_no_construct_changes_nothing() {
|
|
// Line 6 is blank, and a blank line belongs to no item. Selecting one is
|
|
// deliberately *not* an excuse to reformat the module around it.
|
|
assert_eq!(fixed_point(TWO_MODULES, "6"), TWO_MODULES);
|
|
// Neither is a range past the end of the file.
|
|
assert_eq!(fixed_point(TWO_MODULES, "900-1000"), TWO_MODULES);
|
|
}
|
|
|
|
#[test]
|
|
fn a_range_covering_the_file_is_a_whole_file_format() {
|
|
for src in [TWO_MODULES, PACKAGE, ALIGNED, WITH_FENCE] {
|
|
assert_eq!(ranged(src, "1-10000"), whole(src), "src: {src}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn formatting_everything_afterwards_still_reaches_the_normal_output() {
|
|
// A partial run is not a fixed point of the *whole-file* formatter, and
|
|
// must not be: reformatting more later is the feature, not a bug.
|
|
let partial = ranged(TWO_MODULES, "3");
|
|
assert_ne!(partial, whole(TWO_MODULES));
|
|
assert_eq!(whole(&partial), whole(TWO_MODULES));
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Packages, generate regions, statements
|
|
// ------------------------------------------------------------------
|
|
|
|
const PACKAGE: &str = "\
|
|
package p;
|
|
localparam int A=1;
|
|
localparam int BBB=2;
|
|
localparam int C=3;
|
|
endpackage
|
|
";
|
|
|
|
#[test]
|
|
fn a_package_splits_the_same_way_a_module_does() {
|
|
assert_eq!(fixed_point(PACKAGE, "1"), PACKAGE.replacen("package p;", "package p;", 1));
|
|
let out = fixed_point(PACKAGE, "3");
|
|
assert_eq!(
|
|
out,
|
|
"package p;\n localparam int A=1;\n localparam int BBB = 2;\n \
|
|
localparam int C=3;\nendpackage\n"
|
|
);
|
|
}
|
|
|
|
const GENERATE: &str = "\
|
|
module m;
|
|
generate
|
|
if (X) begin
|
|
wire a;
|
|
wire bb;
|
|
end
|
|
endgenerate
|
|
endmodule
|
|
";
|
|
|
|
#[test]
|
|
fn a_generate_region_is_one_unit_and_reformats_whole() {
|
|
// Documented coarseness: only modules and packages are descended into,
|
|
// so selecting one line of a `generate` lays out all of it.
|
|
let out = fixed_point(GENERATE, "4");
|
|
assert!(out.contains(" wire a;"), "{out}");
|
|
assert!(out.contains(" wire bb;"), "the sibling reflowed too: {out}");
|
|
}
|
|
|
|
const ALWAYS: &str = "\
|
|
module m;
|
|
always @(posedge clk) begin
|
|
x <= 1;
|
|
y <= 2;
|
|
end
|
|
endmodule
|
|
";
|
|
|
|
#[test]
|
|
fn a_statement_is_never_the_unit_the_item_around_it_is() {
|
|
// Statements are deliberately not descended into this round.
|
|
let out = fixed_point(ALWAYS, "3");
|
|
assert!(out.contains(" x <= 1;"), "{out}");
|
|
assert!(out.contains(" y <= 2;"), "the whole block reflowed: {out}");
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Interaction rules
|
|
// ------------------------------------------------------------------
|
|
|
|
const WITH_FENCE: &str = "\
|
|
module m;
|
|
wire a;
|
|
// vfmt: off
|
|
wire b;
|
|
wire c;
|
|
// vfmt: on
|
|
wire d;
|
|
endmodule
|
|
";
|
|
|
|
#[test]
|
|
fn a_fence_outranks_a_range() {
|
|
// Selecting a fenced line must not lay it out: `off` means hands off
|
|
// across all of vfmt, whatever the caller asked for.
|
|
for spec in ["3", "4", "5", "3-5"] {
|
|
assert_eq!(fixed_point(WITH_FENCE, spec), WITH_FENCE, "spec {spec}");
|
|
}
|
|
// Line 6 is the `on` fence, which is the *next* item's leading comment,
|
|
// so selecting it selects that item — and still not the region.
|
|
let out = fixed_point(WITH_FENCE, "6");
|
|
assert!(out.contains(" wire b;\n wire c;\n"), "the region is untouched: {out}");
|
|
assert!(out.contains(" wire d;"), "{out}");
|
|
// And a range next to the fence still works, leaving the region alone.
|
|
let out = fixed_point(WITH_FENCE, "2");
|
|
assert!(out.contains(" wire a;"), "{out}");
|
|
assert!(out.contains(" wire b;\n wire c;\n"), "the region is untouched: {out}");
|
|
}
|
|
|
|
const ALIGNED: &str = "\
|
|
module m;
|
|
localparam int A = 1;
|
|
localparam int BBBBB = 2;
|
|
localparam int C = 3;
|
|
endmodule
|
|
";
|
|
|
|
#[test]
|
|
fn an_alignment_group_breaks_at_a_range_boundary() {
|
|
// Alignment groups run across consecutive *items*, so a range boundary
|
|
// can fall inside one. The unselected members keep their bytes and leave
|
|
// the group; the selected ones align among themselves, exactly as a
|
|
// fenced member already does.
|
|
let out = fixed_point(ALIGNED, "2-3");
|
|
assert!(out.contains(" localparam int A = 1;\n localparam int BBBBB = 2;\n"), "{out}");
|
|
assert!(out.contains(" localparam int C = 3;\n"), "the outsider is untouched: {out}");
|
|
|
|
// A group of one has nothing to align to and formats flush.
|
|
let out = fixed_point(ALIGNED, "2");
|
|
assert!(out.contains(" localparam int A = 1;\n"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_file_that_does_not_parse_is_refused_whatever_the_range() {
|
|
// Range formatting cannot rescue broken input: the strict parse still has
|
|
// to succeed, so the answer is an error and an untouched file.
|
|
let broken = "module m;\n wire ;\nendmodule\n";
|
|
let lines = LineRanges::parse("2").expect("range spec");
|
|
let error = format_range(broken, &Config::default(), &lines).expect_err("must refuse");
|
|
assert!(error.to_string().contains("parse error"), "{error}");
|
|
// Even when the range names none of the broken lines.
|
|
let lines = LineRanges::parse("1").expect("range spec");
|
|
assert!(format_range(broken, &Config::default(), &lines).is_err());
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Bytes outside the range
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn blank_lines_inside_a_verbatim_run_survive_untouched() {
|
|
let src = "module m;\n wire a;\n\n\n\n wire b;\nendmodule\n";
|
|
// Nothing selected inside the module body: the run keeps all three blanks.
|
|
assert_eq!(fixed_point(src, "3"), src);
|
|
// A boundary is where the item separator applies, and it collapses runs
|
|
// of blank lines to one — the same rule a fence boundary follows.
|
|
assert_eq!(fixed_point(src, "2"), "module m;\n wire a;\n\n wire b;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn comments_are_never_dropped() {
|
|
let src = "\
|
|
module m;
|
|
// leading
|
|
wire a; // trailing
|
|
/* block */
|
|
wire b;
|
|
// dangling
|
|
endmodule
|
|
";
|
|
for spec in ["1", "2", "3", "4", "5", "6", "7", "2-4"] {
|
|
let out = fixed_point(src, spec);
|
|
for comment in ["// leading", "// trailing", "/* block */", "// dangling"] {
|
|
assert!(out.contains(comment), "{comment} lost at {spec}: {out}");
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn multi_byte_content_is_sliced_on_character_boundaries() {
|
|
let src = "module m;\n // é ✓ 🎉 note\n wire a;\n wire b;\nendmodule\n";
|
|
let out = fixed_point(src, "3");
|
|
assert!(out.contains("// é ✓ 🎉 note"), "{out}");
|
|
assert!(out.contains(" wire a;\n wire b;\n"), "{out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_line_shift_can_sweep_in_the_next_construct() {
|
|
// The honest limit of "same ranges, same result": a range is line numbers,
|
|
// and laying out a construct can change how many lines it takes. Here the
|
|
// header collapses from three lines to one, so the second run's lines 1-3
|
|
// reach a body item the first run's did not. That is another correct,
|
|
// verified partial run — not a broken fixed point — and it is why the
|
|
// runtime check replays the *decisions*, which do not move.
|
|
let src = "module m #(\n parameter P = 1\n) (input a);\n wire x;\nendmodule\n";
|
|
let once = ranged(src, "1-3");
|
|
assert_eq!(once, "module m #(parameter P = 1) (input a);\n wire x;\nendmodule\n");
|
|
let twice = ranged(&once, "1-3");
|
|
assert_eq!(twice, "module m #(parameter P = 1) (input a);\n wire x;\nendmodule\n");
|
|
// And it settles there.
|
|
assert_eq!(ranged(&twice, "1-3"), twice);
|
|
}
|
|
|
|
#[test]
|
|
fn a_module_written_on_one_line_has_nowhere_to_be_cut_and_formats_whole() {
|
|
let src = "module m; wire a; endmodule\n";
|
|
assert_eq!(fixed_point(src, "1"), "module m;\n wire a;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn an_empty_module_body_is_one_unit() {
|
|
let src = "module m;\nendmodule\n";
|
|
assert_eq!(fixed_point(src, "1"), "module m;\nendmodule\n");
|
|
assert_eq!(fixed_point(src, "9"), src);
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// The spec grammar
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn several_ranges_select_several_constructs() {
|
|
let out = fixed_point(TWO_MODULES, "3,13");
|
|
assert!(out.contains(" wire x;"), "{out}");
|
|
assert!(out.contains(" wire z;"), "{out}");
|
|
assert!(out.contains(" wire y;"), "and nothing between them: {out}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_bad_spec_is_rejected_before_anything_is_formatted() {
|
|
for spec in ["", "0", "5-1", "1,,2", "x", "1-", "-3"] {
|
|
assert!(LineRanges::parse(spec).is_err(), "spec {spec:?} must be refused");
|
|
}
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// Corpus spot check
|
|
// ------------------------------------------------------------------
|
|
|
|
/// Five real files, formatted at lines 1-5 only: the run must succeed (which
|
|
/// means the safety net passed on the whole output), be a fixed point under
|
|
/// the same ranges, and leave the bulk of the file byte-identical.
|
|
#[test]
|
|
fn a_corpus_spot_check_touches_only_the_top_of_the_file() {
|
|
let Some(files) = corpus_files(5) else { return };
|
|
for path in &files {
|
|
let src = fs::read_to_string(path).expect("read");
|
|
let lines = LineRanges::parse("1-5").expect("range spec");
|
|
let out = format_range(&src, &Config::default(), &lines)
|
|
.unwrap_or_else(|e| panic!("{}: {e}", path.display()));
|
|
// A second run is another verified partial run. It may reach further
|
|
// than the first if the first moved a line (see
|
|
// `a_line_shift_can_sweep_in_the_next_construct`), so what is asserted
|
|
// here is that it succeeds, not that it changes nothing.
|
|
format_range(&out, &Config::default(), &lines)
|
|
.unwrap_or_else(|e| panic!("{} second run: {e}", path.display()));
|
|
// Compared after the tail normalization every run applies (exactly
|
|
// one final newline), which is not a range decision.
|
|
let (src, out) = (src.trim_end(), out.trim_end());
|
|
let shared = common_suffix(src, out);
|
|
assert!(
|
|
shared * 2 > src.len(),
|
|
"{}: only {shared} of {} bytes came back untouched",
|
|
path.display(),
|
|
src.len()
|
|
);
|
|
}
|
|
}
|
|
|
|
/// How many trailing bytes two texts share.
|
|
fn common_suffix(a: &str, b: &str) -> usize {
|
|
a.bytes().rev().zip(b.bytes().rev()).take_while(|(x, y)| x == y).count()
|
|
}
|
|
|
|
/// The first `want` corpus files over 100 lines that format cleanly, or `None`
|
|
/// when the corpus is not checked out (`tools/corpus_fetch.sh`).
|
|
fn corpus_files(want: usize) -> Option<Vec<PathBuf>> {
|
|
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../corpus");
|
|
if !root.is_dir() {
|
|
return None;
|
|
}
|
|
let mut found = Vec::new();
|
|
let mut stack = vec![root];
|
|
while let (Some(dir), true) = (stack.pop(), found.len() < want) {
|
|
let mut entries: Vec<PathBuf> =
|
|
fs::read_dir(&dir).ok()?.filter_map(|e| e.ok()).map(|e| e.path()).collect();
|
|
entries.sort();
|
|
for path in entries {
|
|
if path.is_dir() {
|
|
stack.push(path);
|
|
continue;
|
|
}
|
|
if path.extension().is_none_or(|e| e != "sv" && e != "v") {
|
|
continue;
|
|
}
|
|
let Ok(src) = fs::read_to_string(&path) else { continue };
|
|
if src.lines().count() > 100 && format_source(&src, &Config::default()).is_ok() {
|
|
found.push(path);
|
|
if found.len() == want {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
(found.len() == want).then_some(found)
|
|
}
|