diff --git a/crates/vlog_formatter/src/doc.rs b/crates/vlog_formatter/src/doc.rs index 5417b83..d4997a7 100644 --- a/crates/vlog_formatter/src/doc.rs +++ b/crates/vlog_formatter/src/doc.rs @@ -8,7 +8,9 @@ pub enum Doc { /// Verbatim text. Must not contain newlines (use `HardLine`). Text(String), /// Verbatim text spliced into the *middle* of a line, newlines and all: - /// the `{ ... }` of a `randomize() with { ... }` constraint block. It + /// the `{ ... }` of a `randomize() with { ... }` constraint block, the + /// `#( ... )` of a parameterized type reference, or raw text that + /// carries a string literal across a line break. It /// carries its own indentation, so the printer copies it through /// untouched and resumes measuring from its last line. Unlike /// [`Doc::Verbatim`] it does not force enclosing groups to break — the diff --git a/crates/vlog_formatter/src/format.rs b/crates/vlog_formatter/src/format.rs index dc43a9c..53027fa 100644 --- a/crates/vlog_formatter/src/format.rs +++ b/crates/vlog_formatter/src/format.rs @@ -219,12 +219,14 @@ impl<'a> Fmt<'a> { /// A token that carries a string literal across a line break is emitted /// completely untouched instead: the leading whitespace of such a /// continuation line is part of the string's *value*, and re-indenting it - /// would rewrite the string. Emitting the text as a single `Doc::Text` - /// means the printer copies the newlines through without adding indent, - /// so nothing changes and the result is a fixed point by construction. + /// would rewrite the string. Emitting the text as a `Doc::Inline` means + /// the printer copies the newlines through without adding indent — so + /// nothing changes and the result is a fixed point by construction — + /// while its column tracking resumes from the text's own last line + /// (`Doc::Text` must not contain newlines). fn raw_text(&self, raw: &RawText) -> Doc { if crosses_line_inside_string(&raw.text) { - return text(raw.text.clone()); + return Doc::Inline(raw.text.clone()); } self.raw_lines(&raw.text, raw.col) } @@ -236,9 +238,11 @@ impl<'a> Fmt<'a> { /// `raw_lines`'s dedent-by-column only reaches a fixed point when the text /// begins its own line — the dedent has to cancel the indent the printer /// adds back — and this text never does, so re-indenting it would drift. - /// Emitting it unchanged is idempotent by construction. + /// Emitting it unchanged is idempotent by construction. `Doc::Inline` + /// rather than `Doc::Text` so a multi-line `#( ... )` keeps the printer's + /// column honest (`Doc::Text` must not contain newlines). fn raw_inline(&self, raw: &RawText) -> Doc { - text(raw.text.clone()) + Doc::Inline(raw.text.clone()) } /// The `{ ... }` of a `randomize() with { ... }`: verbatim source spliced diff --git a/crates/vlog_formatter/src/printer.rs b/crates/vlog_formatter/src/printer.rs index 5c0ec52..accfefd 100644 --- a/crates/vlog_formatter/src/printer.rs +++ b/crates/vlog_formatter/src/printer.rs @@ -254,6 +254,22 @@ mod tests { ); } + /// After a multi-line `Inline` is *printed*, `col` is the width of its + /// last line — so a group that follows it on that line is measured from + /// there, not from an accumulated character count. Raw text that carries + /// a string literal across a line break rides this arm; when it was + /// emitted as `Doc::Text` (whose printer arm never resets `col` on the + /// embedded newlines it is not supposed to contain), the stale column + /// forced every later fit check on the line into a conservative break. + #[test] + fn a_group_after_a_multi_line_inline_measures_from_its_last_line() { + let d = concat(vec![ + Doc::Inline("x = \"aaaaaaaaaaaaaaaaaaaaaaaa\nbbb\" ".into()), + group(vec![text("+"), indent(vec![Doc::Line, text("yyyy")]), text(";")]), + ]); + assert_eq!(p(20, d), "x = \"aaaaaaaaaaaaaaaaaaaaaaaa\nbbb\" + yyyy;"); + } + #[test] fn hard_line_forces_break() { let d = group(vec![text("a"), Doc::HardLine, text("b")]); diff --git a/crates/vlog_formatter/tests/cases/classes.cases b/crates/vlog_formatter/tests/cases/classes.cases index 42d076f..4f84214 100644 --- a/crates/vlog_formatter/tests/cases/classes.cases +++ b/crates/vlog_formatter/tests/cases/classes.cases @@ -560,3 +560,16 @@ class c; g = 1; endfunction endclass +==== +# the printer's column stays honest after a multi-line #( ... ): the +# initializer is measured from the raw text's last line, not from an +# accumulated count, so a call that fits the real line stays on it +class c; + uvm_analysis_port #(bit [31:0], + foo_t) p = new("a_fairly_long_component_name", some_parent_handle, extra_argument); +endclass +---- +class c; + uvm_analysis_port #(bit [31:0], + foo_t) p = new("a_fairly_long_component_name", some_parent_handle, extra_argument); +endclass