Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix(rome_formatter): dbg_write API compatibility with write
Browse files Browse the repository at this point in the history
This PR ensures that the `buffer` argument of `dbg_write!` is compatible with the `buffer` argument of `write!` so that the two can be swapped without any changes to the debugged code.

The current implementation hasn't been compatible because the `dbg_write!` macro called into `Inspect::new` that expected a `&mut Buffer` which requires the use of `&mut buffer` if an implementation uses a local `Buffer` variable.

This PR changes the macro implementation to use `Buffer.inspect` instead that works for any mutable `Buffer`.
  • Loading branch information
MichaReiser committed Jun 14, 2022
1 parent 89641b4 commit 6e1147f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
14 changes: 13 additions & 1 deletion crates/rome_formatter/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
Expand Down Expand Up @@ -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 {}
5 changes: 3 additions & 2 deletions crates/rome_formatter/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" }));
Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions crates/rome_js_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}

Expand Down Expand Up @@ -623,16 +623,15 @@ impl Format<JsFormatContext> for FormatDelimited<'_, '_> {
close_token_leading_trivia
]
)?;
let content = buffer.into_element();
let content = buffer.into_vec();

if !content.is_empty() {
write!(
f,
[
indent(&format_once(|f| {
write!(f, [soft_line_break_or_space()])?;
f.write_element(content)
}),),
soft_line_indent_or_space(&format_once(|f| {
f.write_elements(content)
})),
soft_line_break_or_space()
]
)?;
Expand Down

0 comments on commit 6e1147f

Please sign in to comment.