Skip to content

Commit

Permalink
Introduce Token element (#7048)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Sep 2, 2023
1 parent 2f3a950 commit c05e462
Show file tree
Hide file tree
Showing 78 changed files with 733 additions and 723 deletions.
16 changes: 8 additions & 8 deletions crates/ruff_formatter/src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<'fmt, Context> Argument<'fmt, Context> {
///
/// # fn main() -> FormatResult<()> {
/// let formatted = format!(SimpleFormatContext::default(), [
/// format_args!(text("a"), space(), text("b"))
/// format_args!(token("a"), space(), token("b"))
/// ])?;
///
/// assert_eq!("a b", formatted.print()?.as_code());
Expand Down Expand Up @@ -135,26 +135,26 @@ mod tests {
write!(
&mut buffer,
[
text("function"),
token("function"),
space(),
text("a"),
token("a"),
space(),
group(&format_args!(text("("), text(")")))
group(&format_args!(token("("), token(")")))
]
)
.unwrap();

assert_eq!(
buffer.into_vec(),
vec![
FormatElement::StaticText { text: "function" },
FormatElement::Token { text: "function" },
FormatElement::Space,
FormatElement::StaticText { text: "a" },
FormatElement::Token { text: "a" },
FormatElement::Space,
// Group
FormatElement::Tag(Tag::StartGroup(tag::Group::new())),
FormatElement::StaticText { text: "(" },
FormatElement::StaticText { text: ")" },
FormatElement::Token { text: "(" },
FormatElement::Token { text: ")" },
FormatElement::Tag(Tag::EndGroup)
]
);
Expand Down
34 changes: 17 additions & 17 deletions crates/ruff_formatter/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub trait Buffer {
/// let mut state = FormatState::new(SimpleFormatContext::default());
/// let mut buffer = VecBuffer::new(&mut state);
///
/// buffer.write_element(FormatElement::StaticText { text: "test"});
/// buffer.write_element(FormatElement::Token { text: "test"});
///
/// assert_eq!(buffer.into_vec(), vec![FormatElement::StaticText { text: "test" }]);
/// assert_eq!(buffer.into_vec(), vec![FormatElement::Token { text: "test" }]);
/// ```
fn write_element(&mut self, element: FormatElement);

Expand All @@ -50,9 +50,9 @@ pub trait Buffer {
/// let mut state = FormatState::new(SimpleFormatContext::default());
/// let mut buffer = VecBuffer::new(&mut state);
///
/// buffer.write_fmt(format_args!(text("Hello World"))).unwrap();
/// buffer.write_fmt(format_args!(token("Hello World"))).unwrap();
///
/// assert_eq!(buffer.into_vec(), vec![FormatElement::StaticText{ text: "Hello World" }]);
/// assert_eq!(buffer.into_vec(), vec![FormatElement::Token{ text: "Hello World" }]);
/// ```
fn write_fmt(mut self: &mut Self, arguments: Arguments<Self::Context>) -> FormatResult<()> {
write(&mut self, arguments)
Expand Down Expand Up @@ -316,11 +316,11 @@ where
/// write!(
/// buffer,
/// [
/// text("The next soft line or space gets replaced by a space"),
/// token("The next soft line or space gets replaced by a space"),
/// soft_line_break_or_space(),
/// text("and the line here"),
/// token("and the line here"),
/// soft_line_break(),
/// text("is removed entirely.")
/// token("is removed entirely.")
/// ]
/// )
/// })]
Expand All @@ -329,10 +329,10 @@ where
/// assert_eq!(
/// formatted.document().as_ref(),
/// &[
/// FormatElement::StaticText { text: "The next soft line or space gets replaced by a space" },
/// FormatElement::Token { text: "The next soft line or space gets replaced by a space" },
/// FormatElement::Space,
/// FormatElement::StaticText { text: "and the line here" },
/// FormatElement::StaticText { text: "is removed entirely." }
/// FormatElement::Token { text: "and the line here" },
/// FormatElement::Token { text: "is removed entirely." }
/// ]
/// );
///
Expand Down Expand Up @@ -488,19 +488,19 @@ pub trait BufferExtensions: Buffer + Sized {
/// let formatted = format!(SimpleFormatContext::default(), [format_with(|f| {
/// let mut recording = f.start_recording();
///
/// write!(recording, [text("A")])?;
/// write!(recording, [text("B")])?;
/// write!(recording, [token("A")])?;
/// write!(recording, [token("B")])?;
///
/// write!(recording, [format_with(|f| write!(f, [text("C"), text("D")]))])?;
/// write!(recording, [format_with(|f| write!(f, [token("C"), token("D")]))])?;
///
/// let recorded = recording.stop();
/// assert_eq!(
/// recorded.deref(),
/// &[
/// FormatElement::StaticText{ text: "A" },
/// FormatElement::StaticText{ text: "B" },
/// FormatElement::StaticText{ text: "C" },
/// FormatElement::StaticText{ text: "D" }
/// FormatElement::Token{ text: "A" },
/// FormatElement::Token{ text: "B" },
/// FormatElement::Token{ text: "C" },
/// FormatElement::Token{ text: "D" }
/// ]
/// );
///
Expand Down

0 comments on commit c05e462

Please sign in to comment.