Struct biome_formatter::PreambleBuffer
source · pub struct PreambleBuffer<'buf, Preamble, Context> { /* private fields */ }
Expand description
This struct wraps an existing buffer and emits a preamble text when the first text is written.
This can be useful if you, for example, want to write some content if what gets written next isn’t empty.
§Examples
use biome_formatter::{FormatState, Formatted, PreambleBuffer, SimpleFormatContext, VecBuffer, write};
use biome_formatter::prelude::*;
struct Preamble;
impl Format<SimpleFormatContext> for Preamble {
fn fmt(&self, f: &mut Formatter<SimpleFormatContext>) -> FormatResult<()> {
write!(f, [text("# heading"), hard_line_break()])
}
}
let mut state = FormatState::new(SimpleFormatContext::default());
let mut buffer = VecBuffer::new(&mut state);
{
let mut with_preamble = PreambleBuffer::new(&mut buffer, Preamble);
write!(&mut with_preamble, [text("this text will be on a new line")])?;
}
let formatted = Formatted::new(Document::from(buffer.into_vec()), SimpleFormatContext::default());
assert_eq!("# heading\nthis text will be on a new line", formatted.print()?.as_code());
The pre-amble does not get written if no content is written to the buffer.
use biome_formatter::{FormatState, Formatted, PreambleBuffer, SimpleFormatContext, VecBuffer, write};
use biome_formatter::prelude::*;
struct Preamble;
impl Format<SimpleFormatContext> for Preamble {
fn fmt(&self, f: &mut Formatter<SimpleFormatContext>) -> FormatResult<()> {
write!(f, [text("# heading"), hard_line_break()])
}
}
let mut state = FormatState::new(SimpleFormatContext::default());
let mut buffer = VecBuffer::new(&mut state);
{
let mut with_preamble = PreambleBuffer::new(&mut buffer, Preamble);
}
let formatted = Formatted::new(Document::from(buffer.into_vec()), SimpleFormatContext::default());
assert_eq!("", formatted.print()?.as_code());
Implementations§
source§impl<'buf, Preamble, Context> PreambleBuffer<'buf, Preamble, Context>
impl<'buf, Preamble, Context> PreambleBuffer<'buf, Preamble, Context>
Trait Implementations§
source§impl<Preamble, Context> Buffer for PreambleBuffer<'_, Preamble, Context>where
Preamble: Format<Context>,
impl<Preamble, Context> Buffer for PreambleBuffer<'_, Preamble, Context>where
Preamble: Format<Context>,
source§fn write_element(&mut self, element: FormatElement) -> FormatResult<()>
fn write_element(&mut self, element: FormatElement) -> FormatResult<()>
Writes a crate::FormatElement into this buffer, returning whether the write succeeded. Read more
source§fn state(&self) -> &FormatState<Self::Context>
fn state(&self) -> &FormatState<Self::Context>
Returns the formatting state relevant for this formatting session.
source§fn state_mut(&mut self) -> &mut FormatState<Self::Context>
fn state_mut(&mut self) -> &mut FormatState<Self::Context>
Returns the mutable formatting state relevant for this formatting session.
source§fn snapshot(&self) -> BufferSnapshot
fn snapshot(&self) -> BufferSnapshot
Takes a snapshot of the Buffers state, excluding the formatter state.
source§fn restore_snapshot(&mut self, snapshot: BufferSnapshot)
fn restore_snapshot(&mut self, snapshot: BufferSnapshot)
Restores the snapshot buffer Read more
Auto Trait Implementations§
impl<'buf, Preamble, Context> Freeze for PreambleBuffer<'buf, Preamble, Context>where
Preamble: Freeze,
impl<'buf, Preamble, Context> !RefUnwindSafe for PreambleBuffer<'buf, Preamble, Context>
impl<'buf, Preamble, Context> !Send for PreambleBuffer<'buf, Preamble, Context>
impl<'buf, Preamble, Context> !Sync for PreambleBuffer<'buf, Preamble, Context>
impl<'buf, Preamble, Context> Unpin for PreambleBuffer<'buf, Preamble, Context>where
Preamble: Unpin,
impl<'buf, Preamble, Context> !UnwindSafe for PreambleBuffer<'buf, Preamble, Context>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> BufferExtensions for Twhere
T: Buffer,
impl<T> BufferExtensions for Twhere
T: Buffer,
source§fn inspect<F>(&mut self, inspector: F) -> Inspect<'_, Self::Context, F>where
F: FnMut(&FormatElement),
fn inspect<F>(&mut self, inspector: F) -> Inspect<'_, Self::Context, F>where
F: FnMut(&FormatElement),
Returns a new buffer that calls the passed inspector for every element that gets written to the output
source§fn start_recording(&mut self) -> Recording<'_, Self>
fn start_recording(&mut self) -> Recording<'_, Self>
Starts a recording that gives you access to all elements that have been written between the start
and end of the recording Read more
source§fn write_elements<I>(&mut self, elements: I) -> FormatResult<()>where
I: IntoIterator<Item = FormatElement>,
fn write_elements<I>(&mut self, elements: I) -> FormatResult<()>where
I: IntoIterator<Item = FormatElement>,
Writes a sequence of elements into this buffer.