fix(rome_formatter): `dbg_write` API compatibility with `write`
MODIFY
crates/rome_formatter/src/buffer.rs
crates/rome_formatter/src/buffer.rs
@@ -391,7 +391,7 @@ pub struct Inspect<'inner, Context, Inspector> {
}
impl<'inner, Context, Inspector> Inspect<'inner, Context, Inspector> {
- pub fn new(inner: &'inner mut dyn Buffer<Context = Context>, inspector: Inspector) -> Self {
+ fn new(inner: &'inner mut dyn Buffer<Context = Context>, inspector: Inspector) -> Self {
Self { inner, inspector }
}
}
@@ -433,6 +433,18 @@ pub trait BufferExtensions: Buffer + Sized {
{
Inspect::new(self, inspector)
}
+
+ /// Writes a sequence of elements into this buffer.
+ fn write_elements<I>(&mut self, elements: I) -> FormatResult<()>
+ where
+ I: IntoIterator<Item = FormatElement>,
+ {
+ for element in elements {
+ self.write_element(element)?;
+ }
+
+ Ok(())
+ }
}
impl<T> BufferExtensions for T where T: Buffer {}

MODIFY
crates/rome_formatter/src/macros.rs
crates/rome_formatter/src/macros.rs
@@ -84,7 +84,7 @@ macro_rules! write {
/// let mut state = FormatState::new(SimpleFormatContext::default());
/// let mut buffer = VecBuffer::new(&mut state);
///
-/// dbg_write!(&mut buffer, [token("Hello")]).unwrap();
+/// dbg_write!(buffer, [token("Hello")]).unwrap();
/// // ^-- prints: [src/main.rs:7][0] = StaticToken("Hello")
///
/// assert_eq!(buffer.into_element(), FormatElement::Token(Token::Static { text: "Hello" }));
@@ -96,8 +96,9 @@ macro_rules! write {
#[macro_export]
macro_rules! dbg_write {
($dst:expr, [$($arg:expr),+ $(,)?]) => {{
+ use $crate::BufferExtensions;
let mut count = 0;
- let mut inspect = $crate::Inspect::new($dst, |element: &FormatElement| {
+ let mut inspect = $dst.inspect(|element: &FormatElement| {
std::eprintln!(
"[{}:{}][{}] = {element:#?}",
std::file!(), std::line!(), count

MODIFY
crates/rome_js_formatter/src/builders.rs
crates/rome_js_formatter/src/builders.rs
@@ -578,10 +578,10 @@ impl Format<JsFormatContext> for FormatDelimited<'_, '_> {
write!(buffer, [format_trailing_trivia(open_token)])?;
- let trivia = buffer.into_element();
+ let trivia = buffer.into_vec();
if !trivia.is_empty() {
- f.write_element(trivia)?;
+ f.write_elements(trivia)?;
soft_line_break_or_space().fmt(f)?;
}
@@ -614,7 +614,15 @@ impl Format<JsFormatContext> for FormatDelimited<'_, '_> {
])
.fmt(f)?,
DelimitedMode::SoftBlockSpaces(_) => {
- let mut buffer = VecBuffer::new(f.state_mut());
+ let mut is_empty = true;
+
+ let format_content = format_once(|f| {
+ let mut buffer = f.inspect(|element| {
+ if !element.is_empty() {
+ is_empty = false
+ }
+ });
+
write!(
buffer,
[
@@ -622,20 +630,13 @@ impl Format<JsFormatContext> for FormatDelimited<'_, '_> {
format_content,
close_token_leading_trivia
]
- )?;
- let content = buffer.into_element();
+ )
+ });
- if !content.is_empty() {
- write!(
- f,
- [
- indent(&format_once(|f| {
- write!(f, [soft_line_break_or_space()])?;
- f.write_element(content)
- }),),
- soft_line_break_or_space()
- ]
- )?;
+ soft_line_indent_or_space(&format_content).fmt(f)?;
+
+ if !is_empty {
+ soft_line_break_or_space().fmt(f)?;
}
}
};

fix(rome_formatter): `dbg_write` API compatibility with `write` (#2708)