Files
lucisv/crates/vlog_formatter/tests/styles.rs
T
Guoliang ChenandClaude Mythos 5.2 80017a59f2
release / build-and-publish (push) Successful in 5m22s
Release 0.65.5: named formatting styles
TODO §71.5. One setting picks a house layout — [format] style in
lucisv.toml, --style, the lucisv.format.style drop-down, the server and MCP:
lucisv (default), google, lowrisc, ansi, microsoft, nxp, and auto. Seven
layout rules (indent, width, begin placement, else placement, else after a
labelled end, one-per-line lists, list indent, alignment), one row per style
in one table in crates/vlog_formatter/src/style.rs. google and lowrisc follow
those published guides; ansi, microsoft and nxp are lucisv's guesses and say
so. auto formats with each preset and keeps the result that changes the
fewest lines (crates/vlog_formatter/src/auto.rs); the language server
remembers each document's preset (lucisv_server/src/auto_style.rs) so
selection and on-type formatting do not repeat the search. The style is part
of the format cache key.

Proof: default output byte-identical to 0.65.4 on 4 921 files (corpus +
OpenTitan hw) against an independently rebuilt 0.65.4; every style formatted
three times over the same files with no verification error, crash, token or
comment difference (independent lexer) or second-run change. verify.rs is
untouched.

Dev (three rounds), QA (two rounds, pass with findings) and lead review are
in docs/features/format-styles/README.md. Three one-line calls were added to
lucisv_server/src/server/notification.rs for the memory's invalidation.
TODO.md files the pre-existing bugs QA met on the way.

Version bump (Cargo.toml, Cargo.lock, editors/vscode/package.json and both
package-lock.json spots); notes in CHANGELOG.md (## 0.65.5) and
editors/vscode/CHANGELOG.md.

Gates: tools/gates.sh PASS on this tree; vlog_formatter 376/0,
lucisv_server 1996/0, corpus 814/6.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-19 07:03:20 -08:00

687 lines
16 KiB
Rust

//! The named styles (`vlog_formatter::style`): one test per layout switch,
//! each flipping that switch alone on top of the default, then every preset
//! on one module.
//!
//! Every switch only moves whitespace, so each test also asserts the two
//! properties that matter more than the layout itself: the output is a fixed
//! point under the same style, and the default never moves a byte.
use vlog_formatter::style::{self, Alignment, ListIndent, Placement, PortLists, Style};
use vlog_formatter::{
format_range, format_selection, format_source, resolve_style, Config, LineRanges,
};
fn format(src: &str, style: Style) -> String {
let out = format_source(src, &Config::styled(style)).expect("formats");
let again = format_source(&out, &Config::styled(style)).expect("formats again");
assert_eq!(again, out, "not a fixed point under `{}`", style.name);
out
}
/// The default with one switch changed.
fn with(change: impl FnOnce(&mut Style)) -> Style {
let mut style = style::LUCISV;
style.name = "test";
change(&mut style);
style
}
const BRANCHES: &str = "\
module m;
always_ff @(posedge clk) begin
if (a) begin
x <= 1;
end else if (b) begin
x <= 2;
end else begin
x <= 3;
end
end
always_comb begin
case (s)
0: begin
y = 1;
end
default: y = 0;
endcase
end
if (G) begin : gen_a
assign z = 1;
end else begin : gen_b
assign z = 0;
end
endmodule
";
#[test]
fn begin_on_its_own_line_leaves_case_items_alone() {
let out = format(BRANCHES, with(|s| s.begin = Placement::OwnLine));
assert_eq!(
out,
"\
module m;
always_ff @(posedge clk)
begin
if (a)
begin
x <= 1;
end else if (b)
begin
x <= 2;
end else
begin
x <= 3;
end
end
always_comb
begin
case (s)
0: begin
y = 1;
end
default: y = 0;
endcase
end
if (G)
begin : gen_a
assign z = 1;
end else
begin : gen_b
assign z = 0;
end
endmodule
"
);
}
#[test]
fn else_on_its_own_line() {
let out = format(BRANCHES, with(|s| s.else_keyword = Placement::OwnLine));
assert_eq!(
out,
"\
module m;
always_ff @(posedge clk) begin
if (a) begin
x <= 1;
end
else if (b) begin
x <= 2;
end
else begin
x <= 3;
end
end
always_comb begin
case (s)
0: begin
y = 1;
end
default: y = 0;
endcase
end
if (G) begin : gen_a
assign z = 1;
end
else begin : gen_b
assign z = 0;
end
endmodule
"
);
}
#[test]
fn allman_is_both_switches_together() {
let allman = with(|s| {
s.begin = Placement::OwnLine;
s.else_keyword = Placement::OwnLine;
});
let src = "module m;\n initial if (a) begin x = 1; end else begin x = 2; end\nendmodule\n";
assert_eq!(
format(src, allman),
"\
module m;
initial
if (a)
begin
x = 1;
end
else
begin
x = 2;
end
endmodule
"
);
}
/// A comment written before `begin` or before `else` stays exactly where it
/// was in the token stream, whichever line the keyword moves to.
#[test]
fn comments_around_moved_keywords_stay_put() {
let src = "\
module m;
always_comb
if (a) /* why */ begin
x = 1;
end
// otherwise
else begin
x = 2;
end
endmodule
";
let allman = with(|s| {
s.begin = Placement::OwnLine;
s.else_keyword = Placement::OwnLine;
});
let out = format(src, allman);
assert!(out.contains("if (a)\n /* why */\n begin"), "{out}");
assert!(out.contains("end\n // otherwise\n else\n begin"), "{out}");
// And back: the default puts every keyword where it always did.
assert_eq!(format(&out, style::LUCISV), format(src, style::LUCISV));
}
const HEADER: &str = "\
module m #(parameter int W = 8) (input logic clk, output logic [W-1:0] q);
sub #(.W(W)) u_sub (.clk(clk), .q(q));
sub #(8) u_pos (clk, q);
sub u_wild (.*);
endmodule
";
#[test]
fn one_port_per_line_breaks_headers_and_named_lists_only() {
let out = format(HEADER, with(|s| s.port_lists = PortLists::OnePerLine));
assert_eq!(
out,
"\
module m #(
parameter int W = 8
) (
input logic clk,
output logic [W-1:0] q
);
sub #(
.W(W)
) u_sub (
.clk(clk),
.q(q)
);
sub #(8) u_pos (clk, q);
sub u_wild (
.*
);
endmodule
"
);
// An empty list has nothing to break.
let empty = "module m ();\nendmodule\n";
assert_eq!(format(empty, with(|s| s.port_lists = PortLists::OnePerLine)), empty);
}
#[test]
fn a_wrapped_list_indents_its_entries_two_levels() {
let style = with(|s| {
s.port_lists = PortLists::OnePerLine;
s.list_indent = ListIndent::Wrap;
});
assert_eq!(
format(HEADER, style),
"\
module m #(
parameter int W = 8
) (
input logic clk,
output logic [W-1:0] q
);
sub #(
.W(W)
) u_sub (
.clk(clk),
.q(q)
);
sub #(8) u_pos (clk, q);
sub u_wild (
.*
);
endmodule
"
);
// Wrap alone changes only lists that break anyway.
assert_eq!(
format(HEADER, with(|s| s.list_indent = ListIndent::Wrap)),
format(HEADER, style::LUCISV)
);
}
/// Written without a single padding space, so the default keeps it flush.
const FLUSH: &str = "\
module m (input logic clk, input logic [7:0] data, output logic valid);
localparam int A = 1;
localparam int LONGER = 22;
sub u_sub (.clk(clk), .data_in(data), .v(valid));
endmodule
";
#[test]
fn always_aligned_pads_ports_parameters_and_connections() {
// The default reads the source and keeps this flush...
assert_eq!(format(FLUSH, style::LUCISV), FLUSH);
// ...and `Always` aligns it, which breaks the lists: a column only means
// anything one entry per line.
assert_eq!(
format(FLUSH, with(|s| s.alignment = Alignment::Always)),
"\
module m (
input logic clk,
input logic [7:0] data,
output logic valid
);
localparam int A = 1;
localparam int LONGER = 22;
sub u_sub (
.clk (clk),
.data_in (data),
.v (valid)
);
endmodule
"
);
}
#[test]
fn always_aligned_still_refuses_what_alignment_cannot_do() {
let always = with(|s| s.alignment = Alignment::Always);
// Nothing to pad: every row is the same width.
let same = "module m;\n localparam int A = 1;\n localparam int B = 2;\nendmodule\n";
assert_eq!(format(same, always), same);
// Too wide for the line: the whole group stays flush.
let wide = with(|s| {
s.alignment = Alignment::Always;
s.line_length = 40;
});
let src = "module m;\n localparam int A = 32'hFFFF_FFFF;\n localparam int LONGER_NAME_HERE = 2;\nendmodule\n";
assert_eq!(format(src, wide), src);
// A blank line still splits two tables.
let two = "module m;\n localparam int A = 1;\n\n localparam int LONGER = 2;\nendmodule\n";
assert_eq!(format(two, always), two);
}
/// One module touching every switch, through every preset.
const SAMPLE: &str = "\
module top #(parameter int W = 8) (input logic clk, input logic [W-1:0] d, output logic [W-1:0] q);
localparam int A = 1;
localparam int LONGER = 22;
always_ff @(posedge clk) begin
if (d == 0) begin
q <= '0;
end else begin
q <= d;
end
end
sub #(.W(W)) u_sub (.clk(clk), .data_in(d));
endmodule
";
#[test]
fn the_lucisv_preset_is_the_default() {
assert_eq!(style::preset("lucisv"), Some(style::LUCISV));
let default = Config::default();
let lucisv = Config::styled(style::LUCISV);
assert_eq!((default.max_width, default.indent_width), (lucisv.max_width, lucisv.indent_width));
assert_eq!(default.style, lucisv.style);
assert!(!default.uvm_utils_indent);
}
#[test]
fn every_preset_formats_the_sample() {
let expected = [
(
"lucisv",
"\
module top #(parameter int W = 8) (input logic clk, input logic [W-1:0] d, output logic [W-1:0] q);
localparam int A = 1;
localparam int LONGER = 22;
always_ff @(posedge clk) begin
if (d == 0) begin
q <= '0;
end else begin
q <= d;
end
end
sub #(.W(W)) u_sub (.clk(clk), .data_in(d));
endmodule
",
),
(
"google",
"\
module top #(
parameter int W = 8
) (
input logic clk,
input logic [W-1:0] d,
output logic [W-1:0] q
);
localparam int A = 1;
localparam int LONGER = 22;
always_ff @(posedge clk) begin
if (d == 0) begin
q <= '0;
end else begin
q <= d;
end
end
sub #(
.W(W)
) u_sub (
.clk(clk),
.data_in(d)
);
endmodule
",
),
(
"lowrisc",
"\
module top #(
parameter int W = 8
) (
input logic clk,
input logic [W-1:0] d,
output logic [W-1:0] q
);
localparam int A = 1;
localparam int LONGER = 22;
always_ff @(posedge clk) begin
if (d == 0) begin
q <= '0;
end else begin
q <= d;
end
end
sub #(
.W(W)
) u_sub (
.clk (clk),
.data_in (d)
);
endmodule
",
),
(
"ansi",
"\
module top #(
parameter int W = 8
) (input logic clk, input logic [W-1:0] d, output logic [W-1:0] q);
localparam int A = 1;
localparam int LONGER = 22;
always_ff @(posedge clk)
begin
if (d == 0)
begin
q <= '0;
end
else
begin
q <= d;
end
end
sub #(.W(W)) u_sub (.clk(clk), .data_in(d));
endmodule
",
),
(
"microsoft",
"\
module top #(
parameter int W = 8
) (
input logic clk,
input logic [W-1:0] d,
output logic [W-1:0] q
);
localparam int A = 1;
localparam int LONGER = 22;
always_ff @(posedge clk)
begin
if (d == 0)
begin
q <= '0;
end
else
begin
q <= d;
end
end
sub #(
.W(W)
) u_sub (
.clk(clk),
.data_in(d)
);
endmodule
",
),
(
"nxp",
"\
module top #(
parameter int W = 8
) (
input logic clk,
input logic [W-1:0] d,
output logic [W-1:0] q
);
localparam int A = 1;
localparam int LONGER = 22;
always_ff @(posedge clk) begin
if (d == 0) begin
q <= '0;
end
else begin
q <= d;
end
end
sub #(
.W(W)
) u_sub (
.clk (clk),
.data_in (d)
);
endmodule
",
),
];
assert_eq!(expected.len(), style::PRESETS.len());
for (name, want) in expected {
let preset = style::preset(name).expect("a preset");
assert_eq!(format(SAMPLE, preset), want, "preset `{name}`");
}
}
/// Formatting with A, then B, then A again lands where A alone did, for
/// every pair — except where A *reads* the source's spacing (`Infer`) and B
/// wrote columns into it, which A then faithfully keeps.
#[test]
fn a_round_trip_through_another_preset_comes_back() {
for a in style::PRESETS {
let once = format(SAMPLE, a);
for b in style::PRESETS {
let back = format(&format(&once, b), a);
if a.alignment == Alignment::Infer && b.alignment == Alignment::Always {
// The columns B wrote are now the author's; A keeps them.
assert_eq!(back, format(&format(SAMPLE, b), a), "{} via {}", a.name, b.name);
} else {
assert_eq!(back, once, "{} via {}", a.name, b.name);
}
}
}
}
/// `--lines` and an editor selection lay their constructs out in the style
/// and leave every other byte exactly as written.
#[test]
fn range_and_selection_formatting_follow_the_style() {
let src = "\
module m;
always_comb if (a) begin x = 1; end
always_comb if (b) begin y = 1; end
endmodule
";
let ansi = Config::styled(style::ANSI);
let lines = LineRanges::parse("2").expect("a range");
let want = "\
module m;
always_comb
if (a)
begin
x = 1;
end
always_comb if (b) begin y = 1; end
endmodule
";
assert_eq!(format_range(src, &ansi, &lines).expect("formats"), want);
let selected = format_selection(src, &ansi, &lines).expect("formats");
assert!(selected.contains("\n always_comb\n if (a)\n begin\n"), "{selected}");
assert!(selected.ends_with(" always_comb if (b) begin y = 1; end\nendmodule\n"), "{selected}");
}
fn auto() -> Config {
Config::named("auto").expect("auto is a style")
}
fn format_auto(src: &str) -> String {
let out = format_source(src, &auto()).expect("formats");
assert_eq!(format_source(&out, &auto()).expect("formats again"), out, "auto is idempotent");
out
}
#[test]
fn auto_keeps_each_file_in_the_layout_it_already_has() {
// Written Allman: auto keeps `begin` and `else` on their own lines...
let allman = "\
module m;
always_comb
if (a)
begin
x = 1;
end
else
begin
x = 2;
end
endmodule
";
assert_eq!(format_auto(allman), allman);
assert_eq!(resolve_style(allman, &auto()).style.begin, Placement::OwnLine);
// ...and cuddled stays cuddled, which is also what the default does.
let cuddled = format(allman, style::LUCISV);
assert_eq!(format_auto(&cuddled), cuddled);
assert_eq!(resolve_style(&cuddled, &auto()).style, style::LUCISV);
// A messy file moves to the nearest preset, not to an arbitrary one.
let messy = "module m;\n always_comb\n if (a)\n begin x = 1; end\n else\n begin x = 2; end\nendmodule\n";
assert_eq!(format_auto(messy), allman);
}
#[test]
fn auto_picks_a_layout_never_a_size() {
// An Allman file at 4 spaces keeps auto's 2: indent is the config's, and
// auto only ever picks among layouts at that indent.
let wide = format(BRANCHES, style::ANSI);
let chosen = resolve_style(&wide, &auto());
assert_eq!((chosen.indent_width, chosen.max_width), (2, 100));
let four = Config { indent_width: 4, ..auto() };
assert_eq!(resolve_style(&wide, &four).style.begin, Placement::OwnLine);
let out = format_source(&wide, &four).expect("formats");
assert_eq!(out, wide, "with indent 4 configured, the ansi file is already formatted");
}
#[test]
fn auto_with_nothing_to_tell_the_presets_apart_is_the_default() {
let plain = "module m;\n assign a = b;\nendmodule\n";
assert_eq!(format_auto(plain), plain);
assert_eq!(resolve_style(plain, &auto()).style, style::LUCISV);
assert_eq!(auto().style_name(), "auto");
assert!(!resolve_style(plain, &auto()).auto_style);
}
/// A range under auto lays its lines out in the style of the whole file, so
/// a selection in an Allman file comes out Allman even when the selected
/// lines themselves were written cuddled.
#[test]
fn auto_ranges_take_the_whole_files_style() {
let src = "\
module m;
always_comb
begin
if (a)
begin
x = 1;
end
else
begin
x = 2;
end
end
always_comb if (b) begin y = 1; end
endmodule
";
let lines = LineRanges::parse("13").expect("a range");
let out = format_range(src, &auto(), &lines).expect("formats");
assert!(
out.ends_with(" always_comb\n if (b)\n begin\n y = 1;\n end\nendmodule\n"),
"{out}"
);
let selected = format_selection(src, &auto(), &lines).expect("formats");
assert_eq!(selected, out);
}
/// Google's "Eschatology" and lowRISC's "Begin / End": `end else begin` on
/// one line, except after a labelled `end`, where `else` starts a new line.
#[test]
fn else_after_a_labelled_end_starts_its_own_line_in_the_guides() {
let src = "\
module m;
always_comb
if (a) begin : l1
x = 1;
end : l1 else begin
x = 2;
end
if (G) begin : g1
assign z = 1;
end : g1 else begin : g2
assign z = 0;
end : g2
endmodule
";
let guide = "\
module m;
always_comb
if (a) begin : l1
x = 1;
end : l1
else begin
x = 2;
end
if (G) begin : g1
assign z = 1;
end : g1
else begin : g2
assign z = 0;
end : g2
endmodule
";
assert_eq!(format(src, style::GOOGLE), guide);
assert_eq!(format(src, style::LOWRISC), guide);
// The default keeps what it always printed.
assert_eq!(format(src, style::LUCISV), src);
// And an unlabelled `end` still cuddles in the guides.
let plain = format(BRANCHES, style::GOOGLE);
assert!(plain.contains(" end else if (b) begin\n"), "{plain}");
}