diff --git a/crates/ruff_formatter/src/arguments.rs b/crates/ruff_formatter/src/arguments.rs index 850a734f04b2a..28a1638db85c7 100644 --- a/crates/ruff_formatter/src/arguments.rs +++ b/crates/ruff_formatter/src/arguments.rs @@ -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()); @@ -135,11 +135,11 @@ 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(); @@ -147,14 +147,14 @@ mod tests { 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) ] ); diff --git a/crates/ruff_formatter/src/buffer.rs b/crates/ruff_formatter/src/buffer.rs index d2eec095fbca1..80ba8f15e7e36 100644 --- a/crates/ruff_formatter/src/buffer.rs +++ b/crates/ruff_formatter/src/buffer.rs @@ -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); @@ -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) -> FormatResult<()> { write(&mut self, arguments) @@ -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.") /// ] /// ) /// })] @@ -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." } /// ] /// ); /// @@ -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" } /// ] /// ); /// diff --git a/crates/ruff_formatter/src/builders.rs b/crates/ruff_formatter/src/builders.rs index 05fce999fb201..0f6962448425b 100644 --- a/crates/ruff_formatter/src/builders.rs +++ b/crates/ruff_formatter/src/builders.rs @@ -26,7 +26,7 @@ use crate::{Buffer, VecBuffer}; /// /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ -/// group(&format_args![text("a,"), soft_line_break(), text("b")]) +/// group(&format_args![token("a,"), soft_line_break(), token("b")]) /// ])?; /// /// assert_eq!( @@ -52,9 +52,9 @@ use crate::{Buffer, VecBuffer}; /// /// let elements = format!(context, [ /// group(&format_args![ -/// text("a long word,"), +/// token("a long word,"), /// soft_line_break(), -/// text("so that the group doesn't fit on a single line"), +/// token("so that the group doesn't fit on a single line"), /// ]) /// ])?; /// @@ -83,9 +83,9 @@ pub const fn soft_line_break() -> Line { /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("a,"), +/// token("a,"), /// hard_line_break(), -/// text("b"), +/// token("b"), /// hard_line_break() /// ]) /// ])?; @@ -115,9 +115,9 @@ pub const fn hard_line_break() -> Line { /// let elements = format!( /// SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("a,"), +/// token("a,"), /// empty_line(), -/// text("b"), +/// token("b"), /// empty_line() /// ]) /// ])?; @@ -146,9 +146,9 @@ pub const fn empty_line() -> Line { /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("a,"), +/// token("a,"), /// soft_line_break_or_space(), -/// text("b"), +/// token("b"), /// ]) /// ])?; /// @@ -173,9 +173,9 @@ pub const fn empty_line() -> Line { /// /// let elements = format!(context, [ /// group(&format_args![ -/// text("a long word,"), +/// token("a long word,"), /// soft_line_break_or_space(), -/// text("so that the group doesn't fit on a single line"), +/// token("so that the group doesn't fit on a single line"), /// ]) /// ])?; /// @@ -215,12 +215,8 @@ impl std::fmt::Debug for Line { } } -/// Creates a token that gets written as is to the output. Make sure to properly escape the text if -/// it's user generated (e.g. a string and not a language keyword). -/// -/// # Line feeds -/// Tokens may contain line breaks but they must use the line feeds (`\n`). -/// The [`crate::Printer`] converts the line feed characters to the character specified in the [`crate::PrinterOptions`]. +/// Creates a token that gets written as is to the output. A token must be ASCII only and is not allowed +/// to contain any line breaks or tab characters. /// /// # Examples /// @@ -229,7 +225,7 @@ impl std::fmt::Debug for Line { /// use ruff_formatter::prelude::*; /// /// # fn main() -> FormatResult<()> { -/// let elements = format!(SimpleFormatContext::default(), [text("Hello World")])?; +/// let elements = format!(SimpleFormatContext::default(), [token("Hello World")])?; /// /// assert_eq!( /// "Hello World", @@ -248,34 +244,38 @@ impl std::fmt::Debug for Line { /// /// # fn main() -> FormatResult<()> { /// // the tab must be encoded as \\t to not literally print a tab character ("Hello{tab}World" vs "Hello\tWorld") -/// let elements = format!(SimpleFormatContext::default(), [text("\"Hello\\tWorld\"")])?; +/// let elements = format!(SimpleFormatContext::default(), [token("\"Hello\\tWorld\"")])?; /// /// assert_eq!(r#""Hello\tWorld""#, elements.print()?.as_code()); /// # Ok(()) /// # } /// ``` #[inline] -pub fn text(text: &'static str) -> StaticText { - debug_assert_no_newlines(text); +pub fn token(text: &'static str) -> Token { + debug_assert!(text.is_ascii(), "Token must be ASCII text only"); + debug_assert!( + !text.contains(['\n', '\r', '\t']), + "A token should not contain any newlines or tab characters" + ); - StaticText { text } + Token { text } } #[derive(Clone, Copy, Eq, PartialEq)] -pub struct StaticText { +pub struct Token { text: &'static str, } -impl Format for StaticText { +impl Format for Token { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { - f.write_element(FormatElement::StaticText { text: self.text }); + f.write_element(FormatElement::Token { text: self.text }); Ok(()) } } -impl std::fmt::Debug for StaticText { +impl std::fmt::Debug for Token { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::write!(f, "StaticToken({})", self.text) + std::write!(f, "Token({})", self.text) } } @@ -295,11 +295,11 @@ impl std::fmt::Debug for StaticText { /// /// let elements = format!(SimpleFormatContext::default(), [ /// source_position(TextSize::new(0)), -/// text("\"Hello "), +/// token("\"Hello "), /// source_position(TextSize::new(8)), -/// text("'Ruff'"), +/// token("'Ruff'"), /// source_position(TextSize::new(14)), -/// text("\""), +/// token("\""), /// source_position(TextSize::new(20)) /// ])?; /// @@ -336,25 +336,25 @@ impl Format for SourcePosition { /// Creates a text from a dynamic string with its optional start-position in the source document. /// This is done by allocating a new string internally. -pub fn dynamic_text(text: &str, position: Option) -> DynamicText { +pub fn text(text: &str, position: Option) -> Text { debug_assert_no_newlines(text); - DynamicText { text, position } + Text { text, position } } #[derive(Eq, PartialEq)] -pub struct DynamicText<'a> { +pub struct Text<'a> { text: &'a str, position: Option, } -impl Format for DynamicText<'_> { +impl Format for Text<'_> { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { if let Some(source_position) = self.position { f.write_element(FormatElement::SourcePosition(source_position)); } - f.write_element(FormatElement::DynamicText { + f.write_element(FormatElement::Text { text: self.text.to_string().into_boxed_str(), }); @@ -362,9 +362,9 @@ impl Format for DynamicText<'_> { } } -impl std::fmt::Debug for DynamicText<'_> { +impl std::fmt::Debug for Text<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - std::write!(f, "DynamicToken({})", self.text) + std::write!(f, "Text({})", self.text) } } @@ -446,9 +446,9 @@ fn debug_assert_no_newlines(text: &str) { /// /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ -/// text("a"), -/// line_suffix(&text("c"), 0), -/// text("b") +/// token("a"), +/// line_suffix(&token("c"), 0), +/// token("b") /// ])?; /// /// assert_eq!("abc", elements.print()?.as_code()); @@ -470,16 +470,16 @@ fn debug_assert_no_newlines(text: &str) { /// let elements = format!(context, [ /// // Breaks /// group(&format_args![ -/// if_group_breaks(&text("(")), -/// soft_block_indent(&format_args![text("a"), line_suffix(&text(" // a comment"), 13)]), -/// if_group_breaks(&text(")")) +/// if_group_breaks(&token("(")), +/// soft_block_indent(&format_args![token("a"), line_suffix(&token(" // a comment"), 13)]), +/// if_group_breaks(&token(")")) /// ]), /// /// // Fits /// group(&format_args![ -/// if_group_breaks(&text("(")), -/// soft_block_indent(&format_args![text("a"), line_suffix(&text(" // a comment"), 0)]), -/// if_group_breaks(&text(")")) +/// if_group_breaks(&token("(")), +/// soft_block_indent(&format_args![token("a"), line_suffix(&token(" // a comment"), 0)]), +/// if_group_breaks(&token(")")) /// ]), /// ])?; /// # assert_eq!("(\n\ta // a comment\n)a // a comment", elements.print()?.as_code()); @@ -533,11 +533,11 @@ impl std::fmt::Debug for LineSuffix<'_, Context> { /// /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ -/// text("a"), -/// line_suffix(&text("c"), 0), -/// text("b"), +/// token("a"), +/// line_suffix(&token("c"), 0), +/// token("b"), /// line_suffix_boundary(), -/// text("d") +/// token("d") /// ])?; /// /// assert_eq!( @@ -599,7 +599,7 @@ impl Format for LineSuffixBoundary { /// write!(recording, [ /// labelled( /// LabelId::of(MyLabels::Main), -/// &text("'I have a label'") +/// &token("'I have a label'") /// ) /// ])?; /// @@ -608,9 +608,9 @@ impl Format for LineSuffixBoundary { /// let is_labelled = recorded.first().is_some_and( |element| element.has_label(LabelId::of(MyLabels::Main))); /// /// if is_labelled { -/// write!(f, [text(" has label `Main`")]) +/// write!(f, [token(" has label `Main`")]) /// } else { -/// write!(f, [text(" doesn't have label `Main`")]) +/// write!(f, [token(" doesn't have label `Main`")]) /// } /// })] /// )?; @@ -670,7 +670,7 @@ impl std::fmt::Debug for FormatLabelled<'_, Context> { /// /// # fn main() -> FormatResult<()> { /// // the tab must be encoded as \\t to not literally print a tab character ("Hello{tab}World" vs "Hello\tWorld") -/// let elements = format!(SimpleFormatContext::default(), [text("a"), space(), text("b")])?; +/// let elements = format!(SimpleFormatContext::default(), [token("a"), space(), token("b")])?; /// /// assert_eq!("a b", elements.print()?.as_code()); /// # Ok(()) @@ -708,16 +708,16 @@ impl Format for Space { /// /// # fn main() -> FormatResult<()> { /// let block = format!(SimpleFormatContext::default(), [ -/// text("switch {"), +/// token("switch {"), /// block_indent(&format_args![ -/// text("default:"), +/// token("default:"), /// indent(&format_args![ /// // this is where we want to use a /// hard_line_break(), -/// text("break;"), +/// token("break;"), /// ]) /// ]), -/// text("}"), +/// token("}"), /// ])?; /// /// assert_eq!( @@ -772,22 +772,22 @@ impl std::fmt::Debug for Indent<'_, Context> { /// /// # fn main() -> FormatResult<()> { /// let block = format!(SimpleFormatContext::default(), [ -/// text("root"), +/// token("root"), /// align(2, &format_args![ /// hard_line_break(), -/// text("aligned"), +/// token("aligned"), /// dedent(&format_args![ /// hard_line_break(), -/// text("not aligned"), +/// token("not aligned"), /// ]), /// dedent(&indent(&format_args![ /// hard_line_break(), -/// text("Indented, not aligned") +/// token("Indented, not aligned") /// ])) /// ]), /// dedent(&format_args![ /// hard_line_break(), -/// text("Dedent on root level is a no-op.") +/// token("Dedent on root level is a no-op.") /// ]) /// ])?; /// @@ -841,23 +841,23 @@ impl std::fmt::Debug for Dedent<'_, Context> { /// /// # fn main() -> FormatResult<()> { /// let block = format!(SimpleFormatContext::default(), [ -/// text("root"), +/// token("root"), /// indent(&format_args![ /// hard_line_break(), -/// text("indent level 1"), +/// token("indent level 1"), /// indent(&format_args![ /// hard_line_break(), -/// text("indent level 2"), +/// token("indent level 2"), /// align(2, &format_args![ /// hard_line_break(), -/// text("two space align"), +/// token("two space align"), /// dedent_to_root(&format_args![ /// hard_line_break(), -/// text("starts at the beginning of the line") +/// token("starts at the beginning of the line") /// ]), /// ]), /// hard_line_break(), -/// text("end indent level 2"), +/// token("end indent level 2"), /// ]) /// ]), /// ])?; @@ -903,24 +903,24 @@ where /// /// # fn main() -> FormatResult<()> { /// let block = format!(SimpleFormatContext::default(), [ -/// text("a"), +/// token("a"), /// hard_line_break(), -/// text("?"), +/// token("?"), /// space(), /// align(2, &format_args![ -/// text("function () {"), +/// token("function () {"), /// hard_line_break(), -/// text("}"), +/// token("}"), /// ]), /// hard_line_break(), -/// text(":"), +/// token(":"), /// space(), /// align(2, &format_args![ -/// text("function () {"), -/// block_indent(&text("console.log('test');")), -/// text("}"), +/// token("function () {"), +/// block_indent(&token("console.log('test');")), +/// token("}"), /// ]), -/// text(";") +/// token(";") /// ])?; /// /// assert_eq!( @@ -953,24 +953,24 @@ where /// }); /// /// let block = format!(context, [ -/// text("a"), +/// token("a"), /// hard_line_break(), -/// text("?"), +/// token("?"), /// space(), /// align(2, &format_args![ -/// text("function () {"), +/// token("function () {"), /// hard_line_break(), -/// text("}"), +/// token("}"), /// ]), /// hard_line_break(), -/// text(":"), +/// token(":"), /// space(), /// align(2, &format_args![ -/// text("function () {"), -/// block_indent(&text("console.log('test');")), -/// text("}"), +/// token("function () {"), +/// block_indent(&token("console.log('test');")), +/// token("}"), /// ]), -/// text(";") +/// token(";") /// ])?; /// /// assert_eq!( @@ -1038,13 +1038,13 @@ impl std::fmt::Debug for Align<'_, Context> { /// let block = format![ /// SimpleFormatContext::default(), /// [ -/// text("{"), +/// token("{"), /// block_indent(&format_args![ -/// text("let a = 10;"), +/// token("let a = 10;"), /// hard_line_break(), -/// text("let c = a + 5;"), +/// token("let c = a + 5;"), /// ]), -/// text("}"), +/// token("}"), /// ] /// ]?; /// @@ -1083,13 +1083,13 @@ pub fn block_indent(content: &impl Format) -> BlockIndent(content: &impl Format) -> BlockIndent FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("5,"), +/// token("5,"), /// soft_line_break_or_space(), -/// text("10"), +/// token("10"), /// ]), -/// text("]"), +/// token("]"), /// ]) /// ])?; /// @@ -1157,15 +1157,15 @@ pub fn soft_block_indent(content: &impl Format) -> BlockIndent /// /// let elements = format!(context, [ /// group(&format_args![ -/// text("name"), +/// token("name"), /// space(), -/// text("="), +/// token("="), /// soft_line_indent_or_space(&format_args![ -/// text("firstName"), +/// token("firstName"), /// space(), -/// text("+"), +/// token("+"), /// space(), -/// text("lastName"), +/// token("lastName"), /// ]), /// ]) /// ])?; @@ -1186,10 +1186,10 @@ pub fn soft_block_indent(content: &impl Format) -> BlockIndent /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("a"), +/// token("a"), /// space(), -/// text("="), -/// soft_line_indent_or_space(&text("10")), +/// token("="), +/// soft_line_indent_or_space(&token("10")), /// ]) /// ])?; /// @@ -1289,14 +1289,14 @@ impl std::fmt::Debug for BlockIndent<'_, Context> { /// /// let elements = format!(context, [ /// group(&format_args![ -/// text("{"), +/// token("{"), /// soft_space_or_block_indent(&format_args![ -/// text("aPropertyThatExceeds"), -/// text(":"), +/// token("aPropertyThatExceeds"), +/// token(":"), /// space(), -/// text("'line width'"), +/// token("'line width'"), /// ]), -/// text("}") +/// token("}") /// ]) /// ])?; /// @@ -1316,14 +1316,14 @@ impl std::fmt::Debug for BlockIndent<'_, Context> { /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("{"), +/// token("{"), /// soft_space_or_block_indent(&format_args![ -/// text("a"), -/// text(":"), +/// token("a"), +/// token(":"), /// space(), -/// text("5"), +/// token("5"), /// ]), -/// text("}") +/// token("}") /// ]) /// ])?; /// @@ -1361,15 +1361,15 @@ pub fn soft_space_or_block_indent(content: &impl Format) -> Bl /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("1,"), +/// token("1,"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), +/// token("3"), /// ]), -/// text("]"), +/// token("]"), /// ]) /// ])?; /// @@ -1394,15 +1394,15 @@ pub fn soft_space_or_block_indent(content: &impl Format) -> Bl /// /// let elements = format!(context, [ /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("'Good morning! How are you today?',"), +/// token("'Good morning! How are you today?',"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), +/// token("3"), /// ]), -/// text("]"), +/// token("]"), /// ]) /// ])?; /// @@ -1495,37 +1495,37 @@ impl std::fmt::Debug for Group<'_, Context> { /// let content = format_with(|f| { /// let parentheses_id = f.group_id("parentheses"); /// group(&format_args![ -/// if_group_breaks(&text("(")), +/// if_group_breaks(&token("(")), /// indent_if_group_breaks(&format_args![ /// soft_line_break(), /// conditional_group(&format_args![ -/// text("'aaaaaaa'"), +/// token("'aaaaaaa'"), /// soft_line_break_or_space(), -/// text("+"), +/// token("+"), /// space(), /// fits_expanded(&conditional_group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("'Good morning!',"), +/// token("'Good morning!',"), /// soft_line_break_or_space(), -/// text("'How are you?'"), +/// token("'How are you?'"), /// ]), -/// text("]"), +/// token("]"), /// ], tag::Condition::if_group_fits_on_line(parentheses_id))), /// soft_line_break_or_space(), -/// text("+"), +/// token("+"), /// space(), /// conditional_group(&format_args![ -/// text("'bbbb'"), +/// token("'bbbb'"), /// soft_line_break_or_space(), -/// text("and"), +/// token("and"), /// space(), -/// text("'c'") +/// token("'c'") /// ], tag::Condition::if_group_fits_on_line(parentheses_id)) /// ], tag::Condition::if_breaks()), /// ], parentheses_id), /// soft_line_break(), -/// if_group_breaks(&text(")")) +/// if_group_breaks(&token(")")) /// ]) /// .with_group_id(Some(parentheses_id)) /// .fmt(f) @@ -1623,16 +1623,16 @@ impl std::fmt::Debug for ConditionalGroup<'_, Context> { /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("'Good morning! How are you today?',"), +/// token("'Good morning! How are you today?',"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// expand_parent(), // Forces the parent to expand /// soft_line_break_or_space(), -/// text("3"), +/// token("3"), /// ]), -/// text("]"), +/// token("]"), /// ]) /// ])?; /// @@ -1679,16 +1679,16 @@ impl Format for ExpandParent { /// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("1,"), +/// token("1,"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), -/// if_group_breaks(&text(",")) +/// token("3"), +/// if_group_breaks(&token(",")) /// ]), -/// text("]"), +/// token("]"), /// ]) /// ])?; /// @@ -1713,16 +1713,16 @@ impl Format for ExpandParent { /// /// let elements = format!(context, [ /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("'A somewhat longer string to force a line break',"), +/// token("'A somewhat longer string to force a line break',"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), -/// if_group_breaks(&text(",")) +/// token("3"), +/// if_group_breaks(&token(",")) /// ]), -/// text("]"), +/// token("]"), /// ]) /// ])?; /// @@ -1760,16 +1760,16 @@ where /// # fn main() -> FormatResult<()> { /// let formatted = format!(SimpleFormatContext::default(), [ /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("1,"), +/// token("1,"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), -/// if_group_fits_on_line(&text(",")) +/// token("3"), +/// if_group_fits_on_line(&token(",")) /// ]), -/// text("]"), +/// token("]"), /// ]) /// ])?; /// @@ -1794,16 +1794,16 @@ where /// /// let formatted = format!(context, [ /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("'A somewhat longer string to force a line break',"), +/// token("'A somewhat longer string to force a line break',"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), -/// if_group_fits_on_line(&text(",")) +/// token("3"), +/// if_group_fits_on_line(&token(",")) /// ]), -/// text("]"), +/// token("]"), /// ]) /// ])?; /// @@ -1860,21 +1860,21 @@ impl IfGroupBreaks<'_, Context> { /// write!(f, [ /// group( /// &format_args![ - /// text("["), + /// token("["), /// soft_block_indent(&format_with(|f| { /// f.fill() - /// .entry(&soft_line_break_or_space(), &text("1,")) - /// .entry(&soft_line_break_or_space(), &text("234568789,")) - /// .entry(&soft_line_break_or_space(), &text("3456789,")) + /// .entry(&soft_line_break_or_space(), &token("1,")) + /// .entry(&soft_line_break_or_space(), &token("234568789,")) + /// .entry(&soft_line_break_or_space(), &token("3456789,")) /// .entry(&soft_line_break_or_space(), &format_args!( - /// text("["), - /// soft_block_indent(&text("4")), - /// text("]"), - /// if_group_breaks(&text(",")).with_group_id(Some(group_id)) + /// token("["), + /// soft_block_indent(&token("4")), + /// token("]"), + /// if_group_breaks(&token(",")).with_group_id(Some(group_id)) /// )) /// .finish() /// })), - /// text("]") + /// token("]") /// ], /// ).with_group_id(Some(group_id)) /// ]) @@ -1931,9 +1931,9 @@ impl std::fmt::Debug for IfGroupBreaks<'_, Context> { /// let id = f.group_id("head"); /// /// write!(f, [ -/// group(&text("Head")).with_group_id(Some(id)), -/// if_group_breaks(&indent(&text("indented"))).with_group_id(Some(id)), -/// if_group_fits_on_line(&text("indented")).with_group_id(Some(id)) +/// group(&token("Head")).with_group_id(Some(id)), +/// if_group_breaks(&indent(&token("indented"))).with_group_id(Some(id)), +/// if_group_fits_on_line(&token("indented")).with_group_id(Some(id)) /// ]) /// /// # }); @@ -1956,8 +1956,8 @@ impl std::fmt::Debug for IfGroupBreaks<'_, Context> { /// let group_id = f.group_id("header"); /// /// write!(f, [ -/// group(&text("(aLongHeaderThatBreaksForSomeReason) =>")).with_group_id(Some(group_id)), -/// indent_if_group_breaks(&format_args![hard_line_break(), text("a => b")], group_id) +/// group(&token("(aLongHeaderThatBreaksForSomeReason) =>")).with_group_id(Some(group_id)), +/// indent_if_group_breaks(&format_args![hard_line_break(), token("a => b")], group_id) /// ]) /// }); /// @@ -1986,8 +1986,8 @@ impl std::fmt::Debug for IfGroupBreaks<'_, Context> { /// let group_id = f.group_id("header"); /// /// write!(f, [ -/// group(&text("(aLongHeaderThatBreaksForSomeReason) =>")).with_group_id(Some(group_id)), -/// indent_if_group_breaks(&format_args![hard_line_break(), text("a => b")], group_id) +/// group(&token("(aLongHeaderThatBreaksForSomeReason) =>")).with_group_id(Some(group_id)), +/// indent_if_group_breaks(&format_args![hard_line_break(), token("a => b")], group_id) /// ]) /// }); /// @@ -2059,17 +2059,17 @@ impl std::fmt::Debug for IndentIfGroupBreaks<'_, Context> { /// /// write!(f, [ /// group(&format_args![ -/// text("a"), +/// token("a"), /// soft_line_break_or_space(), -/// text("+"), +/// token("+"), /// space(), /// fits_expanded(&group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("a,"), space(), text("# comment"), expand_parent(), soft_line_break_or_space(), -/// text("b") +/// token("a,"), space(), token("# comment"), expand_parent(), soft_line_break_or_space(), +/// token("b") /// ]), -/// text("]") +/// token("]") /// ])) /// ]), /// ]) @@ -2161,17 +2161,17 @@ impl std::fmt::Debug for FormatWith { /// impl Format for MyFormat { /// fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { /// write!(f, [ -/// text("("), +/// token("("), /// block_indent(&format_with(|f| { /// let separator = space(); /// let mut join = f.join_with(&separator); /// /// for item in &self.items { -/// join.entry(&format_with(|f| write!(f, [dynamic_text(item, None)]))); +/// join.entry(&format_with(|f| write!(f, [text(item, None)]))); /// } /// join.finish() /// })), -/// text(")") +/// token(")") /// ]) /// } /// } @@ -2212,8 +2212,8 @@ where /// /// struct MyFormat; /// -/// fn generate_values() -> impl Iterator { -/// vec![text("1"), text("2"), text("3"), text("4")].into_iter() +/// fn generate_values() -> impl Iterator { +/// vec![token("1"), token("2"), token("3"), token("4")].into_iter() /// } /// /// impl Format for MyFormat { @@ -2244,7 +2244,7 @@ where /// /// Formatting the same value twice results in a panic. /// -/// ```panics +/// ```should_panic /// use ruff_formatter::prelude::*; /// use ruff_formatter::{SimpleFormatContext, format, write, Buffer}; /// use ruff_text_size::TextSize; @@ -2252,7 +2252,7 @@ where /// let mut count = 0; /// /// let value = format_once(|f| { -/// write!(f, [dynamic_token(&std::format!("Formatted {count}."), TextSize::default())]) +/// write!(f, [text(&std::format!("Formatted {count}."), None)]) /// }); /// /// format!(SimpleFormatContext::default(), [value]).expect("Formatting once works fine"); @@ -2476,54 +2476,54 @@ impl<'a, Context> BestFitting<'a, Context> { /// // Everything fits on a single line /// format_args!( /// group(&format_args![ - /// text("["), + /// token("["), /// soft_block_indent(&format_args![ - /// text("1,"), + /// token("1,"), /// soft_line_break_or_space(), - /// text("2,"), + /// token("2,"), /// soft_line_break_or_space(), - /// text("3"), + /// token("3"), /// ]), - /// text("]") + /// token("]") /// ]), /// space(), - /// text("+"), + /// token("+"), /// space(), - /// text("aVeryLongIdentifier") + /// token("aVeryLongIdentifier") /// ), /// /// // Breaks after `[` and prints each elements on a single line /// // The group is necessary because the variant, by default is printed in flat mode and a /// // hard line break indicates that the content doesn't fit. /// format_args!( - /// text("["), - /// group(&block_indent(&format_args![text("1,"), hard_line_break(), text("2,"), hard_line_break(), text("3")])).should_expand(true), - /// text("]"), + /// token("["), + /// group(&block_indent(&format_args![token("1,"), hard_line_break(), token("2,"), hard_line_break(), token("3")])).should_expand(true), + /// token("]"), /// space(), - /// text("+"), + /// token("+"), /// space(), - /// text("aVeryLongIdentifier") + /// token("aVeryLongIdentifier") /// ), /// /// // Adds parentheses and indents the body, breaks after the operator /// format_args!( - /// text("("), + /// token("("), /// block_indent(&format_args![ - /// text("["), + /// token("["), /// block_indent(&format_args![ - /// text("1,"), + /// token("1,"), /// hard_line_break(), - /// text("2,"), + /// token("2,"), /// hard_line_break(), - /// text("3"), + /// token("3"), /// ]), - /// text("]"), + /// token("]"), /// hard_line_break(), - /// text("+"), + /// token("+"), /// space(), - /// text("aVeryLongIdentifier") + /// token("aVeryLongIdentifier") /// ]), - /// text(")") + /// token(")") /// ) /// ).with_mode(BestFittingMode::AllLines) /// ] diff --git a/crates/ruff_formatter/src/format_element.rs b/crates/ruff_formatter/src/format_element.rs index 14a15cfaf246d..ee927292cd40c 100644 --- a/crates/ruff_formatter/src/format_element.rs +++ b/crates/ruff_formatter/src/format_element.rs @@ -30,12 +30,11 @@ pub enum FormatElement { /// formatted position. SourcePosition(TextSize), - /// Token constructed by the formatter from a static string - StaticText { text: &'static str }, + /// A ASCII only Token that contains no line breaks or tab characters. + Token { text: &'static str }, - /// Token constructed from the input source as a dynamic - /// string. - DynamicText { + /// An arbitrary text that can contain tabs, newlines, and unicode characters. + Text { /// There's no need for the text to be mutable, using `Box` safes 8 bytes over `String`. text: Box, }, @@ -72,12 +71,8 @@ impl std::fmt::Debug for FormatElement { FormatElement::Space => write!(fmt, "Space"), FormatElement::Line(mode) => fmt.debug_tuple("Line").field(mode).finish(), FormatElement::ExpandParent => write!(fmt, "ExpandParent"), - FormatElement::StaticText { text } => { - fmt.debug_tuple("StaticText").field(text).finish() - } - FormatElement::DynamicText { text, .. } => { - fmt.debug_tuple("DynamicText").field(text).finish() - } + FormatElement::Token { text } => fmt.debug_tuple("Token").field(text).finish(), + FormatElement::Text { text, .. } => fmt.debug_tuple("DynamicText").field(text).finish(), FormatElement::SourceCodeSlice { slice, contains_newlines, @@ -244,8 +239,8 @@ impl FormatElement { matches!( self, FormatElement::SourceCodeSlice { .. } - | FormatElement::DynamicText { .. } - | FormatElement::StaticText { .. } + | FormatElement::Text { .. } + | FormatElement::Token { .. } ) } @@ -260,8 +255,8 @@ impl FormatElements for FormatElement { FormatElement::ExpandParent => true, FormatElement::Tag(Tag::StartGroup(group)) => !group.mode().is_flat(), FormatElement::Line(line_mode) => matches!(line_mode, LineMode::Hard | LineMode::Empty), - FormatElement::StaticText { text } => text.contains('\n'), - FormatElement::DynamicText { text, .. } => text.contains('\n'), + + FormatElement::Text { text, .. } => text.contains('\n'), FormatElement::SourceCodeSlice { contains_newlines, .. } => *contains_newlines, @@ -275,6 +270,7 @@ impl FormatElements for FormatElement { FormatElement::LineSuffixBoundary | FormatElement::Space | FormatElement::Tag(_) + | FormatElement::Token { .. } | FormatElement::SourcePosition(_) => false, } } diff --git a/crates/ruff_formatter/src/format_element/document.rs b/crates/ruff_formatter/src/format_element/document.rs index 46ca5a7104944..ae6761296c4d1 100644 --- a/crates/ruff_formatter/src/format_element/document.rs +++ b/crates/ruff_formatter/src/format_element/document.rs @@ -104,8 +104,7 @@ impl Document { expands = false; continue; } - FormatElement::StaticText { text } => text.contains('\n'), - FormatElement::DynamicText { text, .. } => text.contains('\n'), + FormatElement::Text { text, .. } => text.contains('\n'), FormatElement::SourceCodeSlice { contains_newlines, .. } => *contains_newlines, @@ -249,20 +248,20 @@ impl Format> for &[FormatElement] { while let Some(element) = iter.next() { if !first_element && !in_text && !element.is_end_tag() { // Write a separator between every two elements - write!(f, [text(","), soft_line_break_or_space()])?; + write!(f, [token(","), soft_line_break_or_space()])?; } first_element = false; match element { element @ (FormatElement::Space - | FormatElement::StaticText { .. } - | FormatElement::DynamicText { .. } + | FormatElement::Token { .. } + | FormatElement::Text { .. } | FormatElement::SourceCodeSlice { .. }) => { fn write_escaped(element: &FormatElement, f: &mut Formatter) { let text = match element { - FormatElement::StaticText { text } => text, - FormatElement::DynamicText { text } => text.as_ref(), + FormatElement::Token { text } => text, + FormatElement::Text { text } => text.as_ref(), FormatElement::SourceCodeSlice { slice, .. } => { slice.text(f.context().source_code()) } @@ -270,7 +269,7 @@ impl Format> for &[FormatElement] { }; if text.contains('"') { - f.write_element(FormatElement::DynamicText { + f.write_element(FormatElement::Text { text: text.replace('"', r#"\""#).into(), }); } else { @@ -279,14 +278,14 @@ impl Format> for &[FormatElement] { } if !in_text { - write!(f, [text("\"")])?; + write!(f, [token("\"")])?; } in_text = true; match element { FormatElement::Space => { - write!(f, [text(" ")])?; + write!(f, [token(" ")])?; } element if element.is_text() => { write_escaped(element, f); @@ -297,45 +296,42 @@ impl Format> for &[FormatElement] { let is_next_text = iter.peek().is_some_and(|e| e.is_text() || e.is_space()); if !is_next_text { - write!(f, [text("\"")])?; + write!(f, [token("\"")])?; in_text = false; } } FormatElement::Line(mode) => match mode { LineMode::SoftOrSpace => { - write!(f, [text("soft_line_break_or_space")])?; + write!(f, [token("soft_line_break_or_space")])?; } LineMode::Soft => { - write!(f, [text("soft_line_break")])?; + write!(f, [token("soft_line_break")])?; } LineMode::Hard => { - write!(f, [text("hard_line_break")])?; + write!(f, [token("hard_line_break")])?; } LineMode::Empty => { - write!(f, [text("empty_line")])?; + write!(f, [token("empty_line")])?; } }, FormatElement::ExpandParent => { - write!(f, [text("expand_parent")])?; + write!(f, [token("expand_parent")])?; } FormatElement::SourcePosition(position) => { write!( f, - [dynamic_text( - &std::format!("source_position({position:?})"), - None - )] + [text(&std::format!("source_position({position:?})"), None)] )?; } FormatElement::LineSuffixBoundary => { - write!(f, [text("line_suffix_boundary")])?; + write!(f, [token("line_suffix_boundary")])?; } FormatElement::BestFitting { variants, mode } => { - write!(f, [text("best_fitting([")])?; + write!(f, [token("best_fitting([")])?; f.write_elements([ FormatElement::Tag(StartIndent), FormatElement::Line(LineMode::Hard), @@ -350,13 +346,13 @@ impl Format> for &[FormatElement] { FormatElement::Line(LineMode::Hard), ]); - write!(f, [text("]")])?; + write!(f, [token("]")])?; if *mode != BestFittingMode::FirstLine { - write!(f, [dynamic_text(&std::format!(", mode: {mode:?}"), None),])?; + write!(f, [text(&std::format!(", mode: {mode:?}"), None),])?; } - write!(f, [text(")")])?; + write!(f, [token(")")])?; } FormatElement::Interned(interned) => { @@ -370,7 +366,7 @@ impl Format> for &[FormatElement] { write!( f, [ - dynamic_text(&std::format!(""), None), + text(&std::format!(""), None), space(), &&**interned, ] @@ -379,10 +375,7 @@ impl Format> for &[FormatElement] { Some(reference) => { write!( f, - [dynamic_text( - &std::format!(""), - None - )] + [text(&std::format!(""), None)] )?; } } @@ -401,9 +394,9 @@ impl Format> for &[FormatElement] { write!( f, [ - text(">"), + token(">"), ] )?; first_element = false; @@ -414,13 +407,13 @@ impl Format> for &[FormatElement] { f, [ ContentArrayEnd, - text(")"), + token(")"), soft_line_break_or_space(), - text("ERROR>") + token("ERROR>") ] )?; first_element = false; @@ -434,7 +427,7 @@ impl Format> for &[FormatElement] { match tag { StartIndent => { - write!(f, [text("indent(")])?; + write!(f, [token("indent(")])?; } StartDedent(mode) => { @@ -443,16 +436,16 @@ impl Format> for &[FormatElement] { DedentMode::Root => "dedentRoot", }; - write!(f, [text(label), text("(")])?; + write!(f, [token(label), token("(")])?; } StartAlign(tag::Align(count)) => { write!( f, [ - text("align("), - dynamic_text(&count.to_string(), None), - text(","), + token("align("), + text(&count.to_string(), None), + token(","), space(), ] )?; @@ -462,27 +455,27 @@ impl Format> for &[FormatElement] { write!( f, [ - text("line_suffix("), - dynamic_text(&std::format!("{reserved_width:?}"), None), - text(","), + token("line_suffix("), + text(&std::format!("{reserved_width:?}"), None), + token(","), space(), ] )?; } StartVerbatim(_) => { - write!(f, [text("verbatim(")])?; + write!(f, [token("verbatim(")])?; } StartGroup(group) => { - write!(f, [text("group(")])?; + write!(f, [token("group(")])?; if let Some(group_id) = group.id() { write!( f, [ - dynamic_text(&std::format!("\"{group_id:?}\""), None), - text(","), + text(&std::format!("\"{group_id:?}\""), None), + token(","), space(), ] )?; @@ -491,10 +484,10 @@ impl Format> for &[FormatElement] { match group.mode() { GroupMode::Flat => {} GroupMode::Expand => { - write!(f, [text("expand: true,"), space()])?; + write!(f, [token("expand: true,"), space()])?; } GroupMode::Propagated => { - write!(f, [text("expand: propagated,"), space()])?; + write!(f, [token("expand: propagated,"), space()])?; } } } @@ -503,10 +496,10 @@ impl Format> for &[FormatElement] { write!( f, [ - text("conditional_group(condition:"), + token("conditional_group(condition:"), space(), group.condition(), - text(","), + token(","), space() ] )?; @@ -514,10 +507,10 @@ impl Format> for &[FormatElement] { match group.mode() { GroupMode::Flat => {} GroupMode::Expand => { - write!(f, [text("expand: true,"), space()])?; + write!(f, [token("expand: true,"), space()])?; } GroupMode::Propagated => { - write!(f, [text("expand: propagated,"), space()])?; + write!(f, [token("expand: propagated,"), space()])?; } } } @@ -526,9 +519,9 @@ impl Format> for &[FormatElement] { write!( f, [ - text("indent_if_group_breaks("), - dynamic_text(&std::format!("\"{id:?}\""), None), - text(","), + token("indent_if_group_breaks("), + text(&std::format!("\"{id:?}\""), None), + token(","), space(), ] )?; @@ -537,10 +530,10 @@ impl Format> for &[FormatElement] { StartConditionalContent(condition) => { match condition.mode { PrintMode::Flat => { - write!(f, [text("if_group_fits_on_line(")])?; + write!(f, [token("if_group_fits_on_line(")])?; } PrintMode::Expanded => { - write!(f, [text("if_group_breaks(")])?; + write!(f, [token("if_group_breaks(")])?; } } @@ -548,8 +541,8 @@ impl Format> for &[FormatElement] { write!( f, [ - dynamic_text(&std::format!("\"{group_id:?}\""), None), - text(","), + text(&std::format!("\"{group_id:?}\""), None), + token(","), space(), ] )?; @@ -560,36 +553,36 @@ impl Format> for &[FormatElement] { write!( f, [ - text("label("), - dynamic_text(&std::format!("\"{label_id:?}\""), None), - text(","), + token("label("), + text(&std::format!("\"{label_id:?}\""), None), + token(","), space(), ] )?; } StartFill => { - write!(f, [text("fill(")])?; + write!(f, [token("fill(")])?; } StartFitsExpanded(tag::FitsExpanded { condition, propagate_expand, }) => { - write!(f, [text("fits_expanded(propagate_expand:"), space()])?; + write!(f, [token("fits_expanded(propagate_expand:"), space()])?; if propagate_expand.get() { - write!(f, [text("true")])?; + write!(f, [token("true")])?; } else { - write!(f, [text("false")])?; + write!(f, [token("false")])?; } - write!(f, [text(","), space()])?; + write!(f, [token(","), space()])?; if let Some(condition) = condition { write!( f, - [text("condition:"), space(), condition, text(","), space()] + [token("condition:"), space(), condition, token(","), space()] )?; } } @@ -611,7 +604,7 @@ impl Format> for &[FormatElement] { | EndDedent | EndFitsExpanded | EndVerbatim => { - write!(f, [ContentArrayEnd, text(")")])?; + write!(f, [ContentArrayEnd, token(")")])?; } }; @@ -627,9 +620,9 @@ impl Format> for &[FormatElement] { f, [ ContentArrayEnd, - text(")"), + token(")"), soft_line_break_or_space(), - dynamic_text(&std::format!(">"), None), + text(&std::format!(">"), None), ] )?; } @@ -644,7 +637,7 @@ impl Format> for ContentArrayStart { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { use Tag::{StartGroup, StartIndent}; - write!(f, [text("[")])?; + write!(f, [token("[")])?; f.write_elements([ FormatElement::Tag(StartGroup(tag::Group::new())), @@ -667,7 +660,7 @@ impl Format> for ContentArrayEnd { FormatElement::Tag(EndGroup), ]); - write!(f, [text("]")]) + write!(f, [token("]")]) } } @@ -767,22 +760,22 @@ impl FormatElements for [FormatElement] { impl Format> for Condition { fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { match (self.mode, self.group_id) { - (PrintMode::Flat, None) => write!(f, [text("if_fits_on_line")]), + (PrintMode::Flat, None) => write!(f, [token("if_fits_on_line")]), (PrintMode::Flat, Some(id)) => write!( f, [ - text("if_group_fits_on_line("), - dynamic_text(&std::format!("\"{id:?}\""), None), - text(")") + token("if_group_fits_on_line("), + text(&std::format!("\"{id:?}\""), None), + token(")") ] ), - (PrintMode::Expanded, None) => write!(f, [text("if_breaks")]), + (PrintMode::Expanded, None) => write!(f, [token("if_breaks")]), (PrintMode::Expanded, Some(id)) => write!( f, [ - text("if_group_breaks("), - dynamic_text(&std::format!("\"{id:?}\""), None), - text(")") + token("if_group_breaks("), + text(&std::format!("\"{id:?}\""), None), + token(")") ] ), } @@ -805,11 +798,11 @@ mod tests { write!( f, [group(&format_args![ - text("("), + token("("), soft_block_indent(&format_args![ - text("Some longer content"), + token("Some longer content"), space(), - text("That should ultimately break"), + token("That should ultimately break"), ]) ])] ) @@ -838,7 +831,7 @@ mod tests { fn escapes_quotes() { let formatted = format!( SimpleFormatContext::default(), - [text(r#""""Python docstring""""#)] + [token(r#""""Python docstring""""#)] ) .unwrap(); @@ -859,7 +852,7 @@ mod tests { write!( f, [group(&format_args![ - text("("), + token("("), soft_block_indent(&format_args![ source_text_slice( TextRange::at(TextSize::new(0), TextSize::new(19)), @@ -899,16 +892,16 @@ mod tests { use Tag::*; let document = Document::from(vec![ - FormatElement::StaticText { text: "[" }, + FormatElement::Token { text: "[" }, FormatElement::Tag(StartGroup(tag::Group::new())), FormatElement::Tag(StartIndent), FormatElement::Line(LineMode::Soft), - FormatElement::StaticText { text: "a" }, + FormatElement::Token { text: "a" }, // Close group instead of indent FormatElement::Tag(EndGroup), FormatElement::Line(LineMode::Soft), FormatElement::Tag(EndIndent), - FormatElement::StaticText { text: "]" }, + FormatElement::Token { text: "]" }, // End tag without start FormatElement::Tag(EndIndent), // Start tag without an end diff --git a/crates/ruff_formatter/src/format_extensions.rs b/crates/ruff_formatter/src/format_extensions.rs index 1964ad246b4ca..6c2aa85e199b9 100644 --- a/crates/ruff_formatter/src/format_extensions.rs +++ b/crates/ruff_formatter/src/format_extensions.rs @@ -34,7 +34,7 @@ pub trait MemoizeFormat { /// let value = self.value.get(); /// self.value.set(value + 1); /// - /// write!(f, [dynamic_text(&std::format!("Formatted {value} times."), None)]) + /// write!(f, [text(&std::format!("Formatted {value} times."), None)]) /// } /// } /// @@ -110,9 +110,9 @@ where /// let current = self.value.get(); /// /// write!(f, [ - /// text("Count:"), + /// token("Count:"), /// space(), - /// dynamic_text(&std::format!("{current}"), None), + /// text(&std::format!("{current}"), None), /// hard_line_break() /// ])?; /// @@ -127,9 +127,9 @@ where /// let counter_content = counter.inspect(f)?; /// /// if counter_content.will_break() { - /// write!(f, [text("Counter:"), block_indent(&counter)]) + /// write!(f, [token("Counter:"), block_indent(&counter)]) /// } else { - /// write!(f, [text("Counter:"), counter]) + /// write!(f, [token("Counter:"), counter]) /// }?; /// /// write!(f, [counter]) diff --git a/crates/ruff_formatter/src/formatter.rs b/crates/ruff_formatter/src/formatter.rs index 9a4363580756c..8274485bc553b 100644 --- a/crates/ruff_formatter/src/formatter.rs +++ b/crates/ruff_formatter/src/formatter.rs @@ -52,11 +52,11 @@ impl<'buf, Context> Formatter<'buf, Context> { /// # fn main() -> FormatResult<()> { /// let formatted = format!(SimpleFormatContext::default(), [format_with(|f| { /// f.join() - /// .entry(&text("a")) + /// .entry(&token("a")) /// .entry(&space()) - /// .entry(&text("+")) + /// .entry(&token("+")) /// .entry(&space()) - /// .entry(&text("b")) + /// .entry(&token("b")) /// .finish() /// })])?; /// @@ -83,11 +83,11 @@ impl<'buf, Context> Formatter<'buf, Context> { /// /// # fn main() -> FormatResult<()> { /// let formatted = format!(SimpleFormatContext::default(), [format_with(|f| { - /// f.join_with(&format_args!(text(","), space())) - /// .entry(&text("1")) - /// .entry(&text("2")) - /// .entry(&text("3")) - /// .entry(&text("4")) + /// f.join_with(&format_args!(token(","), space())) + /// .entry(&token("1")) + /// .entry(&token("2")) + /// .entry(&token("3")) + /// .entry(&token("4")) /// .finish() /// })])?; /// @@ -121,10 +121,10 @@ impl<'buf, Context> Formatter<'buf, Context> { /// # fn main() -> FormatResult<()> { /// let formatted = format!(SimpleFormatContext::default(), [format_with(|f| { /// f.fill() - /// .entry(&soft_line_break_or_space(), &text("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) - /// .entry(&soft_line_break_or_space(), &text("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) - /// .entry(&soft_line_break_or_space(), &text("cccccccccccccccccccccccccccccc")) - /// .entry(&soft_line_break_or_space(), &text("dddddddddddddddddddddddddddddd")) + /// .entry(&soft_line_break_or_space(), &token("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")) + /// .entry(&soft_line_break_or_space(), &token("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")) + /// .entry(&soft_line_break_or_space(), &token("cccccccccccccccccccccccccccccc")) + /// .entry(&soft_line_break_or_space(), &token("dddddddddddddddddddddddddddddd")) /// .finish() /// })])?; /// @@ -142,10 +142,10 @@ impl<'buf, Context> Formatter<'buf, Context> { /// /// # fn main() -> FormatResult<()> { /// let entries = vec![ - /// text("Important: "), - /// text("Please do not commit memory bugs such as segfaults, buffer overflows, etc. otherwise you "), - /// text("will"), - /// text(" be reprimanded") + /// token("Important: "), + /// token("Please do not commit memory bugs such as segfaults, buffer overflows, etc. otherwise you "), + /// token("will"), + /// token(" be reprimanded") /// ]; /// /// let formatted = format!(SimpleFormatContext::default(), [format_with(|f| { diff --git a/crates/ruff_formatter/src/lib.rs b/crates/ruff_formatter/src/lib.rs index 41a3b43f1e775..a90788e1e4be0 100644 --- a/crates/ruff_formatter/src/lib.rs +++ b/crates/ruff_formatter/src/lib.rs @@ -455,7 +455,7 @@ pub type FormatResult = Result; /// fn fmt(&self, f: &mut Formatter) -> FormatResult<()> { /// write!(f, [ /// hard_line_break(), -/// dynamic_text(&self.0, None), +/// text(&self.0, None), /// hard_line_break(), /// ]) /// } @@ -704,7 +704,7 @@ where /// let mut state = FormatState::new(SimpleFormatContext::default()); /// let mut buffer = VecBuffer::new(&mut state); /// -/// write!(&mut buffer, [format_args!(text("Hello World"))])?; +/// write!(&mut buffer, [format_args!(token("Hello World"))])?; /// /// let formatted = Formatted::new(Document::from(buffer.into_vec()), SimpleFormatContext::default()); /// @@ -723,7 +723,7 @@ where /// let mut state = FormatState::new(SimpleFormatContext::default()); /// let mut buffer = VecBuffer::new(&mut state); /// -/// write!(&mut buffer, [text("Hello World")])?; +/// write!(&mut buffer, [token("Hello World")])?; /// /// let formatted = Formatted::new(Document::from(buffer.into_vec()), SimpleFormatContext::default()); /// @@ -754,7 +754,7 @@ pub fn write( /// use ruff_formatter::{format, format_args}; /// /// # fn main() -> FormatResult<()> { -/// let formatted = format!(SimpleFormatContext::default(), [&format_args!(text("test"))])?; +/// let formatted = format!(SimpleFormatContext::default(), [&format_args!(token("test"))])?; /// assert_eq!("test", formatted.print()?.as_code()); /// # Ok(()) /// # } @@ -767,7 +767,7 @@ pub fn write( /// use ruff_formatter::{format}; /// /// # fn main() -> FormatResult<()> { -/// let formatted = format!(SimpleFormatContext::default(), [text("test")])?; +/// let formatted = format!(SimpleFormatContext::default(), [token("test")])?; /// assert_eq!("test", formatted.print()?.as_code()); /// # Ok(()) /// # } diff --git a/crates/ruff_formatter/src/macros.rs b/crates/ruff_formatter/src/macros.rs index 2e6b28bf23545..d3745d1d3c582 100644 --- a/crates/ruff_formatter/src/macros.rs +++ b/crates/ruff_formatter/src/macros.rs @@ -16,7 +16,7 @@ /// /// # fn main() -> FormatResult<()> { /// let formatted = format!(SimpleFormatContext::default(), [ -/// format_args!(text("Hello World")) +/// format_args!(token("Hello World")) /// ])?; /// /// assert_eq!("Hello World", formatted.print()?.as_code()); @@ -52,15 +52,15 @@ macro_rules! format_args { /// # fn main() -> FormatResult<()> { /// let mut state = FormatState::new(SimpleFormatContext::default()); /// let mut buffer = VecBuffer::new(&mut state); -/// write!(&mut buffer, [text("Hello"), space()])?; -/// write!(&mut buffer, [text("World")])?; +/// write!(&mut buffer, [token("Hello"), space()])?; +/// write!(&mut buffer, [token("World")])?; /// /// assert_eq!( /// buffer.into_vec(), /// vec![ -/// FormatElement::StaticText { text: "Hello" }, +/// FormatElement::Token { text: "Hello" }, /// FormatElement::Space, -/// FormatElement::StaticText { text: "World" }, +/// FormatElement::Token { text: "World" }, /// ] /// ); /// # Ok(()) @@ -86,10 +86,10 @@ macro_rules! write { /// let mut state = FormatState::new(SimpleFormatContext::default()); /// let mut buffer = VecBuffer::new(&mut state); /// -/// dbg_write!(buffer, [text("Hello")])?; +/// dbg_write!(buffer, [token("Hello")])?; /// // ^-- prints: [src/main.rs:7][0] = StaticToken("Hello") /// -/// assert_eq!(buffer.into_vec(), vec![FormatElement::StaticText { text: "Hello" }]); +/// assert_eq!(buffer.into_vec(), vec![FormatElement::Token { text: "Hello" }]); /// # Ok(()) /// # } /// ``` @@ -126,14 +126,14 @@ macro_rules! dbg_write { /// use ruff_formatter::prelude::*; /// use ruff_formatter::format; /// -/// let formatted = format!(SimpleFormatContext::default(), [text("("), text("a"), text(")")]).unwrap(); +/// let formatted = format!(SimpleFormatContext::default(), [token("("), token("a"), token(")")]).unwrap(); /// /// assert_eq!( /// formatted.into_document(), /// Document::from(vec![ -/// FormatElement::StaticText { text: "(" }, -/// FormatElement::StaticText { text: "a" }, -/// FormatElement::StaticText { text: ")" }, +/// FormatElement::Token { text: "(" }, +/// FormatElement::Token { text: "a" }, +/// FormatElement::Token { text: ")" }, /// ]) /// ); /// ``` @@ -160,49 +160,49 @@ macro_rules! format { /// let formatted = format!( /// SimpleFormatContext::default(), /// [ -/// text("aVeryLongIdentifier"), +/// token("aVeryLongIdentifier"), /// best_fitting!( /// // Everything fits on a single line /// format_args!( -/// text("("), +/// token("("), /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("1,"), +/// token("1,"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), +/// token("3"), /// ]), -/// text("]") +/// token("]") /// ]), -/// text(")") +/// token(")") /// ), /// /// // Breaks after `[`, but prints all elements on a single line /// format_args!( -/// text("("), -/// text("["), -/// block_indent(&text("1, 2, 3")), -/// text("]"), -/// text(")"), +/// token("("), +/// token("["), +/// block_indent(&token("1, 2, 3")), +/// token("]"), +/// token(")"), /// ), /// /// // Breaks after `[` and prints each element on a single line /// format_args!( -/// text("("), +/// token("("), /// block_indent(&format_args![ -/// text("["), +/// token("["), /// block_indent(&format_args![ -/// text("1,"), +/// token("1,"), /// hard_line_break(), -/// text("2,"), +/// token("2,"), /// hard_line_break(), -/// text("3"), +/// token("3"), /// ]), -/// text("]"), +/// token("]"), /// ]), -/// text(")") +/// token(")") /// ) /// ) /// ] @@ -251,38 +251,38 @@ macro_rules! format { /// best_fitting!( /// // Prints the method call on the line but breaks the array. /// format_args!( -/// text("expect(a).toMatch("), +/// token("expect(a).toMatch("), /// group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("1,"), +/// token("1,"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), +/// token("3"), /// ]), -/// text("]") +/// token("]") /// ]).should_expand(true), -/// text(")") +/// token(")") /// ), /// /// // Breaks after `(` /// format_args!( -/// text("expect(a).toMatch("), +/// token("expect(a).toMatch("), /// group(&soft_block_indent( /// &group(&format_args![ -/// text("["), +/// token("["), /// soft_block_indent(&format_args![ -/// text("1,"), +/// token("1,"), /// soft_line_break_or_space(), -/// text("2,"), +/// token("2,"), /// soft_line_break_or_space(), -/// text("3"), +/// token("3"), /// ]), -/// text("]") +/// token("]") /// ]).should_expand(true), /// )).should_expand(true), -/// text(")") +/// token(")") /// ), /// ) /// ] @@ -345,7 +345,7 @@ mod tests { impl Format<()> for TestFormat { fn fmt(&self, f: &mut Formatter<()>) -> FormatResult<()> { - write!(f, [text("test")]) + write!(f, [token("test")]) } } @@ -358,7 +358,7 @@ mod tests { assert_eq!( buffer.into_vec(), - vec![FormatElement::StaticText { text: "test" }] + vec![FormatElement::Token { text: "test" }] ); } @@ -369,18 +369,18 @@ mod tests { write![ &mut buffer, - [text("a"), space(), text("simple"), space(), TestFormat] + [token("a"), space(), token("simple"), space(), TestFormat] ] .unwrap(); assert_eq!( buffer.into_vec(), vec![ - FormatElement::StaticText { text: "a" }, + FormatElement::Token { text: "a" }, FormatElement::Space, - FormatElement::StaticText { text: "simple" }, + FormatElement::Token { text: "simple" }, FormatElement::Space, - FormatElement::StaticText { text: "test" } + FormatElement::Token { text: "test" } ] ); } @@ -394,41 +394,41 @@ mod tests { let formatted_best_fitting = format!( SimpleFormatContext::default(), [ - text("aVeryLongIdentifier"), + token("aVeryLongIdentifier"), soft_line_break_or_space(), best_fitting![ - format_args![text( + format_args![token( "Something that will not fit on a line with 30 character print width." )], format_args![group(&format_args![ - text("Start"), + token("Start"), soft_line_break(), group(&soft_block_indent(&format_args![ - text("1,"), + token("1,"), soft_line_break_or_space(), - text("2,"), + token("2,"), soft_line_break_or_space(), - text("3"), + token("3"), ])), soft_line_break_or_space(), soft_block_indent(&format_args![ - text("1,"), + token("1,"), soft_line_break_or_space(), - text("2,"), + token("2,"), soft_line_break_or_space(), group(&format_args!( - text("A,"), + token("A,"), soft_line_break_or_space(), - text("B") + token("B") )), soft_line_break_or_space(), - text("3") + token("3") ]), soft_line_break_or_space(), - text("End") + token("End") ]) .should_expand(true)], - format_args!(text("Most"), hard_line_break(), text("Expanded")) + format_args!(token("Most"), hard_line_break(), token("Expanded")) ] ] ) @@ -439,34 +439,34 @@ mod tests { let formatted_normal_list = format!( SimpleFormatContext::default(), [ - text("aVeryLongIdentifier"), + token("aVeryLongIdentifier"), soft_line_break_or_space(), format_args![ - text("Start"), + token("Start"), soft_line_break(), &group(&soft_block_indent(&format_args![ - text("1,"), + token("1,"), soft_line_break_or_space(), - text("2,"), + token("2,"), soft_line_break_or_space(), - text("3"), + token("3"), ])), soft_line_break_or_space(), &soft_block_indent(&format_args![ - text("1,"), + token("1,"), soft_line_break_or_space(), - text("2,"), + token("2,"), soft_line_break_or_space(), group(&format_args!( - text("A,"), + token("A,"), soft_line_break_or_space(), - text("B") + token("B") )), soft_line_break_or_space(), - text("3") + token("3") ]), soft_line_break_or_space(), - text("End") + token("End") ], ] ) diff --git a/crates/ruff_formatter/src/printer/mod.rs b/crates/ruff_formatter/src/printer/mod.rs index f1cbe097c3226..c732c125e8a9e 100644 --- a/crates/ruff_formatter/src/printer/mod.rs +++ b/crates/ruff_formatter/src/printer/mod.rs @@ -95,31 +95,31 @@ impl<'a> Printer<'a> { let args = stack.top(); match element { - FormatElement::Space => self.print_text(" ", None), - FormatElement::StaticText { text } => self.print_text(text, None), - FormatElement::DynamicText { text } => self.print_text(text, None), + FormatElement::Space => self.print_text(Text::Token(" "), None), + FormatElement::Token { text } => self.print_text(Text::Token(text), None), + FormatElement::Text { text } => self.print_text(Text::Text(text), None), FormatElement::SourceCodeSlice { slice, .. } => { let text = slice.text(self.source_code); - self.print_text(text, Some(slice.range())); + self.print_text(Text::Text(text), Some(slice.range())); } FormatElement::Line(line_mode) => { if args.mode().is_flat() && matches!(line_mode, LineMode::Soft | LineMode::SoftOrSpace) { if line_mode == &LineMode::SoftOrSpace { - self.print_text(" ", None); + self.print_text(Text::Token(" "), None); } } else if self.state.line_suffixes.has_pending() { self.flush_line_suffixes(queue, stack, Some(element)); } else { // Only print a newline if the current line isn't already empty if self.state.line_width > 0 { - self.print_str("\n"); + self.print_char('\n'); } // Print a second line break if this is an empty line if line_mode == &LineMode::Empty { - self.print_str("\n"); + self.print_char('\n'); } self.state.pending_indent = args.indention(); @@ -352,7 +352,7 @@ impl<'a> Printer<'a> { Ok(print_mode) } - fn print_text(&mut self, text: &str, source_range: Option) { + fn print_text(&mut self, text: Text, source_range: Option) { if !self.state.pending_indent.is_empty() { let (indent_char, repeat_count) = match self.options.indent_style() { IndentStyle::Tab => ('\t', 1), @@ -390,7 +390,18 @@ impl<'a> Printer<'a> { self.push_marker(); - self.print_str(text); + match text { + #[allow(clippy::cast_possible_truncation)] + Text::Token(token) => { + self.state.buffer.push_str(token); + self.state.line_width += token.len() as u32; + } + Text::Text(text) => { + for char in text.chars() { + self.print_char(char); + } + } + } if let Some(range) = source_range { self.state.source_position = range.end(); @@ -718,12 +729,6 @@ impl<'a> Printer<'a> { invalid_end_tag(TagKind::Entry, stack.top_kind()) } - fn print_str(&mut self, content: &str) { - for char in content.chars() { - self.print_char(char); - } - } - fn print_char(&mut self, char: char) { if char == '\n' { self.state @@ -1047,12 +1052,12 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> { let args = self.stack.top(); match element { - FormatElement::Space => return Ok(self.fits_text(" ", args)), + FormatElement::Space => return Ok(self.fits_text(Text::Token(" "), args)), FormatElement::Line(line_mode) => { match args.mode() { PrintMode::Flat => match line_mode { - LineMode::SoftOrSpace => return Ok(self.fits_text(" ", args)), + LineMode::SoftOrSpace => return Ok(self.fits_text(Text::Token(" "), args)), LineMode::Soft => {} LineMode::Hard | LineMode::Empty => { return Ok(if self.must_be_flat { @@ -1081,11 +1086,11 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> { } } - FormatElement::StaticText { text } => return Ok(self.fits_text(text, args)), - FormatElement::DynamicText { text, .. } => return Ok(self.fits_text(text, args)), + FormatElement::Token { text } => return Ok(self.fits_text(Text::Token(text), args)), + FormatElement::Text { text, .. } => return Ok(self.fits_text(Text::Text(text), args)), FormatElement::SourceCodeSlice { slice, .. } => { let text = slice.text(self.printer.source_code); - return Ok(self.fits_text(text, args)); + return Ok(self.fits_text(Text::Text(text), args)); } FormatElement::LineSuffixBoundary => { if self.state.has_line_suffix { @@ -1293,31 +1298,39 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> { Fits::Maybe } - fn fits_text(&mut self, text: &str, args: PrintElementArgs) -> Fits { + fn fits_text(&mut self, text: Text, args: PrintElementArgs) -> Fits { let indent = std::mem::take(&mut self.state.pending_indent); self.state.line_width += u32::from(indent.level()) * self.options().indent_width() + u32::from(indent.align()); - for c in text.chars() { - let char_width = match c { - '\t' => self.options().tab_width.value(), - '\n' => { - if self.must_be_flat { - return Fits::No; - } - match args.measure_mode() { - MeasureMode::FirstLine => return Fits::Yes, - MeasureMode::AllLines => { - self.state.line_width = 0; - continue; + match text { + #[allow(clippy::cast_possible_truncation)] + Text::Token(token) => { + self.state.line_width += token.len() as u32; + } + Text::Text(text) => { + for c in text.chars() { + let char_width = match c { + '\t' => self.options().tab_width.value(), + '\n' => { + if self.must_be_flat { + return Fits::No; + } + match args.measure_mode() { + MeasureMode::FirstLine => return Fits::Yes, + MeasureMode::AllLines => { + self.state.line_width = 0; + continue; + } + } } - } + // SAFETY: A u32 is sufficient to format files <= 4GB + #[allow(clippy::cast_possible_truncation)] + c => c.width().unwrap_or(0) as u32, + }; + self.state.line_width += char_width; } - // SAFETY: A u32 is sufficient to format files <= 4GB - #[allow(clippy::cast_possible_truncation)] - c => c.width().unwrap_or(0) as u32, - }; - self.state.line_width += char_width; + } } if self.state.line_width > self.options().line_width.into() { @@ -1434,6 +1447,14 @@ impl From for MeasureMode { } } +#[derive(Copy, Clone, Debug)] +enum Text<'a> { + /// ASCII only text that contains no line breaks or tab characters. + Token(&'a str), + /// Arbitrary text. May contain `\n` line breaks, tab characters, or unicode characters. + Text(&'a str), +} + #[cfg(test)] mod tests { use crate::prelude::*; @@ -1469,10 +1490,10 @@ mod tests { fn it_prints_a_group_on_a_single_line_if_it_fits() { let result = format(&FormatArrayElements { items: vec![ - &text("\"a\""), - &text("\"b\""), - &text("\"c\""), - &text("\"d\""), + &token("\"a\""), + &token("\"b\""), + &token("\"c\""), + &token("\"d\""), ], }); @@ -1482,17 +1503,17 @@ mod tests { #[test] fn it_tracks_the_indent_for_each_token() { let formatted = format(&format_args!( - text("a"), + token("a"), soft_block_indent(&format_args!( - text("b"), + token("b"), soft_block_indent(&format_args!( - text("c"), - soft_block_indent(&format_args!(text("d"), soft_line_break(), text("d"),)), - text("c"), + token("c"), + soft_block_indent(&format_args!(token("d"), soft_line_break(), token("d"),)), + token("c"), )), - text("b"), + token("b"), )), - text("a") + token("a") )); assert_eq!( @@ -1517,9 +1538,9 @@ a"#, let result = format_with_options( &format_args![ - text("function main() {"), - block_indent(&text("let x = `This is a multiline\nstring`;")), - text("}"), + token("function main() {"), + block_indent(&text("let x = `This is a multiline\nstring`;", None)), + token("}"), hard_line_break() ], options, @@ -1535,8 +1556,8 @@ a"#, fn it_breaks_a_group_if_a_string_contains_a_newline() { let result = format(&FormatArrayElements { items: vec![ - &text("`This is a string spanning\ntwo lines`"), - &text("\"b\""), + &text("`This is a string spanning\ntwo lines`", None), + &token("\"b\""), ], }); @@ -1551,7 +1572,7 @@ two lines`, } #[test] fn it_breaks_a_group_if_it_contains_a_hard_line_break() { - let result = format(&group(&format_args![text("a"), block_indent(&text("b"))])); + let result = format(&group(&format_args![token("a"), block_indent(&token("b"))])); assert_eq!("a\n b\n", result.as_code()); } @@ -1560,17 +1581,17 @@ two lines`, fn it_breaks_parent_groups_if_they_dont_fit_on_a_single_line() { let result = format(&FormatArrayElements { items: vec![ - &text("\"a\""), - &text("\"b\""), - &text("\"c\""), - &text("\"d\""), + &token("\"a\""), + &token("\"b\""), + &token("\"c\""), + &token("\"d\""), &FormatArrayElements { items: vec![ - &text("\"0123456789\""), - &text("\"0123456789\""), - &text("\"0123456789\""), - &text("\"0123456789\""), - &text("\"0123456789\""), + &token("\"0123456789\""), + &token("\"0123456789\""), + &token("\"0123456789\""), + &token("\"0123456789\""), + &token("\"0123456789\""), ], }, ], @@ -1599,7 +1620,7 @@ two lines`, let result = format_with_options( &FormatArrayElements { - items: vec![&text("'a'"), &text("'b'"), &text("'c'"), &text("'d'")], + items: vec![&token("'a'"), &token("'b'"), &token("'c'"), &token("'d'")], }, options, ); @@ -1610,11 +1631,11 @@ two lines`, #[test] fn it_prints_consecutive_hard_lines_as_one() { let result = format(&format_args![ - text("a"), + token("a"), hard_line_break(), hard_line_break(), hard_line_break(), - text("b"), + token("b"), ]); assert_eq!("a\nb", result.as_code()); @@ -1623,11 +1644,11 @@ two lines`, #[test] fn it_prints_consecutive_empty_lines_as_many() { let result = format(&format_args![ - text("a"), + token("a"), empty_line(), empty_line(), empty_line(), - text("b"), + token("b"), ]); assert_eq!("a\n\n\n\nb", result.as_code()); @@ -1636,12 +1657,12 @@ two lines`, #[test] fn it_prints_consecutive_mixed_lines_as_many() { let result = format(&format_args![ - text("a"), + token("a"), empty_line(), hard_line_break(), empty_line(), hard_line_break(), - text("b"), + token("b"), ]); assert_eq!("a\n\n\nb", result.as_code()); @@ -1658,37 +1679,37 @@ two lines`, // These all fit on the same line together .entry( &soft_line_break_or_space(), - &format_args!(text("1"), text(",")), + &format_args!(token("1"), token(",")), ) .entry( &soft_line_break_or_space(), - &format_args!(text("2"), text(",")), + &format_args!(token("2"), token(",")), ) .entry( &soft_line_break_or_space(), - &format_args!(text("3"), text(",")), + &format_args!(token("3"), token(",")), ) // This one fits on a line by itself, .entry( &soft_line_break_or_space(), - &format_args!(text("723493294"), text(",")), + &format_args!(token("723493294"), token(",")), ) // fits without breaking .entry( &soft_line_break_or_space(), &group(&format_args!( - text("["), - soft_block_indent(&text("5")), - text("],") + token("["), + soft_block_indent(&token("5")), + token("],") )), ) // this one must be printed in expanded mode to fit .entry( &soft_line_break_or_space(), &group(&format_args!( - text("["), - soft_block_indent(&text("123456789")), - text("]"), + token("["), + soft_block_indent(&token("123456789")), + token("]"), )), ) .finish() @@ -1713,27 +1734,27 @@ two lines`, fn line_suffix_printed_at_end() { let printed = format(&format_args![ group(&format_args![ - text("["), + token("["), soft_block_indent(&format_with(|f| { f.fill() .entry( &soft_line_break_or_space(), - &format_args!(text("1"), text(",")), + &format_args!(token("1"), token(",")), ) .entry( &soft_line_break_or_space(), - &format_args!(text("2"), text(",")), + &format_args!(token("2"), token(",")), ) .entry( &soft_line_break_or_space(), - &format_args!(text("3"), if_group_breaks(&text(","))), + &format_args!(token("3"), if_group_breaks(&token(","))), ) .finish() })), - text("]") + token("]") ]), - text(";"), - line_suffix(&format_args![space(), text("// trailing")], 0) + token(";"), + line_suffix(&format_args![space(), token("// trailing")], 0) ]); assert_eq!(printed.as_code(), "[1, 2, 3]; // trailing"); @@ -1743,27 +1764,27 @@ two lines`, fn line_suffix_with_reserved_width() { let printed = format(&format_args![ group(&format_args![ - text("["), + token("["), soft_block_indent(&format_with(|f| { f.fill() .entry( &soft_line_break_or_space(), - &format_args!(text("1"), text(",")), + &format_args!(token("1"), token(",")), ) .entry( &soft_line_break_or_space(), - &format_args!(text("2"), text(",")), + &format_args!(token("2"), token(",")), ) .entry( &soft_line_break_or_space(), - &format_args!(text("3"), if_group_breaks(&text(","))), + &format_args!(token("3"), if_group_breaks(&token(","))), ) .finish() })), - text("]") + token("]") ]), - text(";"), - line_suffix(&format_args![space(), text("// Using reserved width causes this content to not fit even though it's a line suffix element")], 93) + token(";"), + line_suffix(&format_args![space(), token("// Using reserved width causes this content to not fit even though it's a line suffix element")], 93) ]); assert_eq!(printed.as_code(), "[\n 1, 2, 3\n]; // Using reserved width causes this content to not fit even though it's a line suffix element"); @@ -1777,15 +1798,15 @@ two lines`, f, [ group(&format_args![ - text("The referenced group breaks."), + token("The referenced group breaks."), hard_line_break() ]) .with_group_id(Some(group_id)), group(&format_args![ - text("This group breaks because:"), + token("This group breaks because:"), soft_line_break_or_space(), - if_group_fits_on_line(&text("This content fits but should not be printed.")).with_group_id(Some(group_id)), - if_group_breaks(&text("It measures with the 'if_group_breaks' variant because the referenced group breaks and that's just way too much text.")).with_group_id(Some(group_id)), + if_group_fits_on_line(&token("This content fits but should not be printed.")).with_group_id(Some(group_id)), + if_group_breaks(&token("It measures with the 'if_group_breaks' variant because the referenced group breaks and that's just way too much text.")).with_group_id(Some(group_id)), ]) ] ) @@ -1805,7 +1826,7 @@ two lines`, write!( f, [ - group(&text("Group with id-2")).with_group_id(Some(id_2)), + group(&token("Group with id-2")).with_group_id(Some(id_2)), hard_line_break() ] )?; @@ -1813,7 +1834,7 @@ two lines`, write!( f, [ - group(&text("Group with id-1 does not fit on the line because it exceeds the line width of 80 characters by")).with_group_id(Some(id_1)), + group(&token("Group with id-1 does not fit on the line because it exceeds the line width of 80 characters by")).with_group_id(Some(id_1)), hard_line_break() ] )?; @@ -1821,9 +1842,9 @@ two lines`, write!( f, [ - if_group_fits_on_line(&text("Group 2 fits")).with_group_id(Some(id_2)), + if_group_fits_on_line(&token("Group 2 fits")).with_group_id(Some(id_2)), hard_line_break(), - if_group_breaks(&text("Group 1 breaks")).with_group_id(Some(id_1)) + if_group_breaks(&token("Group 1 breaks")).with_group_id(Some(id_1)) ] ) }); @@ -1848,15 +1869,15 @@ Group 1 breaks"# write!( f, [group(&format_args!( - text("["), + token("["), soft_block_indent(&format_args!( format_with(|f| f - .join_with(format_args!(text(","), soft_line_break_or_space())) + .join_with(format_args!(token(","), soft_line_break_or_space())) .entries(&self.items) .finish()), - if_group_breaks(&text(",")), + if_group_breaks(&token(",")), )), - text("]") + token("]") ))] ) } diff --git a/crates/ruff_python_formatter/src/builders.rs b/crates/ruff_python_formatter/src/builders.rs index 40327ce7da87f..fa4e30ea79e1b 100644 --- a/crates/ruff_python_formatter/src/builders.rs +++ b/crates/ruff_python_formatter/src/builders.rs @@ -27,9 +27,9 @@ impl<'ast> Format> for ParenthesizeIfExpands<'_, 'ast> { write!( f, [group(&format_args![ - if_group_breaks(&text("(")), + if_group_breaks(&token("(")), soft_block_indent(&Arguments::from(&self.inner)), - if_group_breaks(&text(")")), + if_group_breaks(&token(")")), ])] ) } @@ -152,7 +152,7 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> { { self.result = self.result.and_then(|_| { if self.entries.is_one_or_more() { - write!(self.fmt, [text(","), separator])?; + write!(self.fmt, [token(","), separator])?; } self.entries = self.entries.next(node.end()); @@ -204,7 +204,7 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> { || self.trailing_comma == TrailingComma::OneOrMore || self.entries.is_more_than_one() { - if_group_breaks(&text(",")).fmt(self.fmt)?; + if_group_breaks(&token(",")).fmt(self.fmt)?; } if magic_trailing_comma { diff --git a/crates/ruff_python_formatter/src/comments/format.rs b/crates/ruff_python_formatter/src/comments/format.rs index 6a6cb33768e16..376590e0197ab 100644 --- a/crates/ruff_python_formatter/src/comments/format.rs +++ b/crates/ruff_python_formatter/src/comments/format.rs @@ -434,7 +434,7 @@ impl Format> for FormatNormalizedComment<'_> { write!( f, [ - dynamic_text(owned, Some(self.range.start())), + text(owned, Some(self.range.start())), source_position(self.range.end()) ] ) diff --git a/crates/ruff_python_formatter/src/expression/expr_attribute.rs b/crates/ruff_python_formatter/src/expression/expr_attribute.rs index b2566242cb508..047e88d980282 100644 --- a/crates/ruff_python_formatter/src/expression/expr_attribute.rs +++ b/crates/ruff_python_formatter/src/expression/expr_attribute.rs @@ -113,7 +113,7 @@ impl FormatNodeRule for FormatExprAttribute { f, [ dangling_comments(before_dot), - text("."), + token("."), dangling_comments(after_dot), attr.format() ] diff --git a/crates/ruff_python_formatter/src/expression/expr_await.rs b/crates/ruff_python_formatter/src/expression/expr_await.rs index 25fd244e758a3..665fec248972d 100644 --- a/crates/ruff_python_formatter/src/expression/expr_await.rs +++ b/crates/ruff_python_formatter/src/expression/expr_await.rs @@ -16,7 +16,7 @@ impl FormatNodeRule for FormatExprAwait { write!( f, [ - text("await"), + token("await"), space(), maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks) ] diff --git a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs index 2241d014cb302..e19566e3c2031 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs @@ -260,7 +260,7 @@ impl FormatRule> for FormatOperator { Operator::FloorDiv => "//", }; - text(operator).fmt(f) + token(operator).fmt(f) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs index 8b2f9feeda655..ee96ddd369c23 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs @@ -132,6 +132,6 @@ impl FormatRule> for FormatBoolOp { BoolOp::Or => "or", }; - text(operator).fmt(f) + token(operator).fmt(f) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_compare.rs b/crates/ruff_python_formatter/src/expression/expr_compare.rs index 2dd82e497bfbc..61e60a0cc863f 100644 --- a/crates/ruff_python_formatter/src/expression/expr_compare.rs +++ b/crates/ruff_python_formatter/src/expression/expr_compare.rs @@ -113,6 +113,6 @@ impl FormatRule> for FormatCmpOp { CmpOp::NotIn => "not in", }; - text(operator).fmt(f) + token(operator).fmt(f) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_constant.rs b/crates/ruff_python_formatter/src/expression/expr_constant.rs index 8f3de76916a80..4877ad30aa32f 100644 --- a/crates/ruff_python_formatter/src/expression/expr_constant.rs +++ b/crates/ruff_python_formatter/src/expression/expr_constant.rs @@ -42,11 +42,11 @@ impl FormatNodeRule for FormatExprConstant { } = item; match value { - Constant::Ellipsis => text("...").fmt(f), - Constant::None => text("None").fmt(f), + Constant::Ellipsis => token("...").fmt(f), + Constant::None => token("None").fmt(f), Constant::Bool(value) => match value { - true => text("True").fmt(f), - false => text("False").fmt(f), + true => token("True").fmt(f), + false => token("False").fmt(f), }, Constant::Int(_) => FormatInt::new(item).fmt(f), Constant::Float(_) => FormatFloat::new(item).fmt(f), diff --git a/crates/ruff_python_formatter/src/expression/expr_dict.rs b/crates/ruff_python_formatter/src/expression/expr_dict.rs index d53e6b6b5faa4..66630dac7c4c6 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict.rs @@ -34,7 +34,7 @@ impl Format> for KeyValuePair<'_> { f, [group(&format_args![ key.format(), - text(":"), + token(":"), space(), self.value.format() ])] @@ -49,7 +49,7 @@ impl Format> for KeyValuePair<'_> { [ // make sure the leading comments are hoisted past the `**` leading_comments(leading_value_comments), - group(&format_args![text("**"), self.value.format()]) + group(&format_args![token("**"), self.value.format()]) ] ) } diff --git a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs index 9b615d0461afe..4fcbbe35e9b6c 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs @@ -1,5 +1,5 @@ use ruff_formatter::prelude::{ - format_args, format_with, group, soft_line_break_or_space, space, text, + format_args, format_with, group, soft_line_break_or_space, space, token, }; use ruff_formatter::write; use ruff_python_ast::node::AnyNodeRef; @@ -35,7 +35,7 @@ impl FormatNodeRule for FormatExprDictComp { "{", &group(&format_args!( group(&key.format()), - text(":"), + token(":"), space(), value.format(), soft_line_break_or_space(), diff --git a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs index 07236000c1958..1b22fb568c199 100644 --- a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs @@ -62,12 +62,12 @@ impl FormatNodeRule for FormatExprIfExp { body.format(), in_parentheses_only_soft_line_break_or_space(), leading_comments(comments.leading(test.as_ref())), - text("if"), + token("if"), space(), test.format(), in_parentheses_only_soft_line_break_or_space(), leading_comments(comments.leading(orelse.as_ref())), - text("else"), + token("else"), space(), ] )?; diff --git a/crates/ruff_python_formatter/src/expression/expr_lambda.rs b/crates/ruff_python_formatter/src/expression/expr_lambda.rs index 3498325222d3a..54cd619b1307a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_lambda.rs +++ b/crates/ruff_python_formatter/src/expression/expr_lambda.rs @@ -21,7 +21,7 @@ impl FormatNodeRule for FormatExprLambda { let comments = f.context().comments().clone(); let dangling = comments.dangling(item); - write!(f, [text("lambda")])?; + write!(f, [token("lambda")])?; if let Some(parameters) = parameters { write!( @@ -35,7 +35,7 @@ impl FormatNodeRule for FormatExprLambda { )?; } - write!(f, [text(":")])?; + write!(f, [token(":")])?; if dangling.is_empty() { write!(f, [space()])?; diff --git a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs index 01371565eb281..2ec759ab5fae6 100644 --- a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs +++ b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs @@ -25,7 +25,7 @@ impl FormatNodeRule for FormatExprNamedExpr { f, [ group(&format_args!(target.format(), soft_line_break_or_space())), - text(":=") + token(":=") ] )?; diff --git a/crates/ruff_python_formatter/src/expression/expr_slice.rs b/crates/ruff_python_formatter/src/expression/expr_slice.rs index 8d1dfc627c40b..c8177e9f8019a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_slice.rs +++ b/crates/ruff_python_formatter/src/expression/expr_slice.rs @@ -91,7 +91,7 @@ impl FormatNodeRule for FormatExprSlice { if !all_simple && lower.is_some() { space().fmt(f)?; } - text(":").fmt(f)?; + token(":").fmt(f)?; // No upper node, no need for a space, e.g. `x[a() :]` if !all_simple && upper.is_some() { space().fmt(f)?; @@ -125,7 +125,7 @@ impl FormatNodeRule for FormatExprSlice { if !all_simple && (upper.is_some() || step.is_none()) { space().fmt(f)?; } - text(":").fmt(f)?; + token(":").fmt(f)?; // No step node, no need for a space if !all_simple && step.is_some() { space().fmt(f)?; diff --git a/crates/ruff_python_formatter/src/expression/expr_starred.rs b/crates/ruff_python_formatter/src/expression/expr_starred.rs index 9bea9e6e878dd..da3e5e208caec 100644 --- a/crates/ruff_python_formatter/src/expression/expr_starred.rs +++ b/crates/ruff_python_formatter/src/expression/expr_starred.rs @@ -21,7 +21,7 @@ impl FormatNodeRule for FormatExprStarred { let comments = f.context().comments().clone(); let dangling = comments.dangling(item); - write!(f, [text("*"), dangling_comments(dangling), value.format()]) + write!(f, [token("*"), dangling_comments(dangling), value.format()]) } fn fmt_dangling_comments( diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index 5eadfeb4d07d9..a7f0fa306b51e 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -70,10 +70,10 @@ impl FormatNodeRule for FormatExprSubscript { write!( f, [group(&format_args![ - text("["), + token("["), trailing_comments(dangling_comments), soft_block_indent(&format_slice), - text("]") + token("]") ])] ) } diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index f62d40f138c90..cb3f848f2dbe1 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -140,12 +140,12 @@ impl FormatNodeRule for FormatExprTuple { TupleParentheses::Preserve if !is_tuple_parenthesized(item, f.context().source()) => { - write!(f, [single.format(), text(",")]) + write!(f, [single.format(), token(",")]) } _ => // A single element tuple always needs parentheses and a trailing comma, except when inside of a subscript { - parenthesized("(", &format_args![single.format(), text(",")], ")") + parenthesized("(", &format_args![single.format(), token(",")], ")") .with_dangling_comments(dangling) .fmt(f) } @@ -166,7 +166,7 @@ impl FormatNodeRule for FormatExprTuple { _ => match self.parentheses { TupleParentheses::Never => { let separator = - format_with(|f| group(&format_args![text(","), space()]).fmt(f)); + format_with(|f| group(&format_args![token(","), space()]).fmt(f)); f.join_with(separator) .entries(elts.iter().formatted()) .finish() diff --git a/crates/ruff_python_formatter/src/expression/expr_unary_op.rs b/crates/ruff_python_formatter/src/expression/expr_unary_op.rs index f83ac33a4c637..2a75c243a476a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_unary_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_unary_op.rs @@ -26,7 +26,7 @@ impl FormatNodeRule for FormatExprUnaryOp { UnaryOp::USub => "-", }; - text(operator).fmt(f)?; + token(operator).fmt(f)?; let comments = f.context().comments().clone(); diff --git a/crates/ruff_python_formatter/src/expression/expr_yield.rs b/crates/ruff_python_formatter/src/expression/expr_yield.rs index 4d8334c64f525..a0d9526dc6d45 100644 --- a/crates/ruff_python_formatter/src/expression/expr_yield.rs +++ b/crates/ruff_python_formatter/src/expression/expr_yield.rs @@ -87,14 +87,14 @@ impl Format> for AnyExpressionYield<'_> { write!( f, [ - text(keyword), + token(keyword), space(), maybe_parenthesize_expression(val, self, Parenthesize::Optional) ] )?; } else { // ExprYieldFrom always has Some(value) so we should never get a bare `yield from` - write!(f, [&text(keyword)])?; + write!(f, [&token(keyword)])?; } Ok(()) } diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 9594ab08f6f0e..91e2e54065e2a 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -248,9 +248,9 @@ impl Format> for MaybeParenthesizeExpression<'_> { // The group here is necessary because `format_expression` may contain IR elements // that refer to the group id group(&format_args![ - text("("), + token("("), soft_block_indent(&format_expression), - text(")") + token(")") ]) .with_group_id(Some(group_id)) .fmt(f) @@ -267,9 +267,9 @@ impl Format> for MaybeParenthesizeExpression<'_> { // Variant 2: // Try to fit the expression by adding parentheses and indenting the expression. group(&format_args![ - text("("), + token("("), soft_block_indent(&format_expression), - text(")") + token(")") ]) .with_group_id(Some(group_id)) .should_expand(true), diff --git a/crates/ruff_python_formatter/src/expression/number.rs b/crates/ruff_python_formatter/src/expression/number.rs index bef3b476e073a..8bf6d266a8b32 100644 --- a/crates/ruff_python_formatter/src/expression/number.rs +++ b/crates/ruff_python_formatter/src/expression/number.rs @@ -25,7 +25,7 @@ impl Format> for FormatInt<'_> { match normalized { Cow::Borrowed(_) => source_text_slice(range, ContainsNewlines::No).fmt(f), - Cow::Owned(normalized) => dynamic_text(&normalized, Some(range.start())).fmt(f), + Cow::Owned(normalized) => text(&normalized, Some(range.start())).fmt(f), } } } @@ -50,7 +50,7 @@ impl Format> for FormatFloat<'_> { match normalized { Cow::Borrowed(_) => source_text_slice(range, ContainsNewlines::No).fmt(f), - Cow::Owned(normalized) => dynamic_text(&normalized, Some(range.start())).fmt(f), + Cow::Owned(normalized) => text(&normalized, Some(range.start())).fmt(f), } } } @@ -78,11 +78,11 @@ impl Format> for FormatComplex<'_> { source_text_slice(range.sub_end(TextSize::from(1)), ContainsNewlines::No).fmt(f)?; } Cow::Owned(normalized) => { - dynamic_text(&normalized, Some(range.start())).fmt(f)?; + text(&normalized, Some(range.start())).fmt(f)?; } } - text("j").fmt(f) + token("j").fmt(f) } } diff --git a/crates/ruff_python_formatter/src/expression/parentheses.rs b/crates/ruff_python_formatter/src/expression/parentheses.rs index 0a0b85a864d5e..b43395c5705b1 100644 --- a/crates/ruff_python_formatter/src/expression/parentheses.rs +++ b/crates/ruff_python_formatter/src/expression/parentheses.rs @@ -172,10 +172,10 @@ impl<'ast> Format> for FormatParenthesized<'_, 'ast> { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { let inner = format_with(|f| { group(&format_args![ - text(self.left), + token(self.left), dangling_open_parenthesis_comments(self.comments), soft_block_indent(&Arguments::from(&self.content)), - text(self.right) + token(self.right) ]) .fmt(f) }); @@ -232,13 +232,13 @@ impl<'ast> Format> for FormatOptionalParentheses<'_, 'ast> write!( f, [group(&format_args![ - if_group_breaks(&text("(")), + if_group_breaks(&token("(")), indent_if_group_breaks( &format_args![soft_line_break(), Arguments::from(&self.content)], parens_id ), soft_line_break(), - if_group_breaks(&text(")")) + if_group_breaks(&token(")")) ]) .with_group_id(Some(parens_id))] ) @@ -375,7 +375,7 @@ impl Format> for FormatEmptyParenthesized<'_> { write!( f, [group(&format_args![ - text(self.left), + token(self.left), // end-of-line comments trailing_comments(&self.comments[..end_of_line_split]), // Avoid unstable formatting with @@ -390,7 +390,7 @@ impl Format> for FormatEmptyParenthesized<'_> { (!self.comments[..end_of_line_split].is_empty()).then_some(hard_line_break()), // own line comments, which need to be indented soft_block_indent(&dangling_comments(&self.comments[end_of_line_split..])), - text(self.right) + token(self.right) ])] ) } diff --git a/crates/ruff_python_formatter/src/expression/string.rs b/crates/ruff_python_formatter/src/expression/string.rs index c4e53a106473a..e6165942ba9f4 100644 --- a/crates/ruff_python_formatter/src/expression/string.rs +++ b/crates/ruff_python_formatter/src/expression/string.rs @@ -326,7 +326,7 @@ impl Format> for FormatStringPart { source_text_slice(self.range(), contains_newlines).fmt(f)?; } Cow::Owned(normalized) => { - dynamic_text(&normalized, Some(self.start())).fmt(f)?; + text(&normalized, Some(self.start())).fmt(f)?; } } self.preferred_quotes.fmt(f) @@ -386,17 +386,17 @@ impl Format> for StringPrefix { // Retain the casing for the raw prefix: // https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#r-strings-and-r-strings if self.contains(StringPrefix::RAW) { - text("r").fmt(f)?; + token("r").fmt(f)?; } else if self.contains(StringPrefix::RAW_UPPER) { - text("R").fmt(f)?; + token("R").fmt(f)?; } if self.contains(StringPrefix::BYTE) { - text("b").fmt(f)?; + token("b").fmt(f)?; } if self.contains(StringPrefix::F_STRING) { - text("f").fmt(f)?; + token("f").fmt(f)?; } // Remove the unicode prefix `u` if any because it is meaningless in Python 3+. @@ -596,7 +596,7 @@ impl Format> for StringQuotes { (QuoteStyle::Double, true) => "\"\"\"", }; - text(quotes).fmt(f) + token(quotes).fmt(f) } } @@ -839,7 +839,7 @@ fn format_docstring(string_part: &FormatStringPart, f: &mut PyFormatter) -> Form if already_normalized { source_text_slice(trimmed_line_range, ContainsNewlines::No).fmt(f)?; } else { - dynamic_text(trim_both, Some(trimmed_line_range.start())).fmt(f)?; + text(trim_both, Some(trimmed_line_range.start())).fmt(f)?; } } offset += first.text_len(); @@ -947,7 +947,7 @@ fn format_docstring_line( let indent_len = count_indentation_like_black(trim_end, f.options().tab_width()) - stripped_indentation; let in_docstring_indent = " ".repeat(indent_len.to_usize()) + trim_end.trim_start(); - dynamic_text(&in_docstring_indent, Some(offset)).fmt(f)?; + text(&in_docstring_indent, Some(offset)).fmt(f)?; } else { // Take the string with the trailing whitespace removed, then also skip the leading // whitespace @@ -957,7 +957,7 @@ fn format_docstring_line( source_text_slice(trimmed_line_range, ContainsNewlines::No).fmt(f)?; } else { // All indents are ascii spaces, so the slicing is correct - dynamic_text( + text( &trim_end[stripped_indentation.to_usize()..], Some(trimmed_line_range.start()), ) diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index 17193b8444b2c..98ab2a19d8e62 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -276,16 +276,16 @@ for converter in connection.ops.get_db_converters( f: &mut ruff_formatter::formatter::Formatter, ) -> FormatResult<()> { let format_str = format_with(|f| { - write!(f, [text("\"")])?; + write!(f, [token("\"")])?; let mut words = self.0.split_whitespace().peekable(); let mut fill = f.fill(); let separator = format_with(|f| { group(&format_args![ - if_group_breaks(&text("\"")), + if_group_breaks(&token("\"")), soft_line_break_or_space(), - if_group_breaks(&text("\" ")) + if_group_breaks(&token("\" ")) ]) .fmt(f) }); @@ -293,10 +293,10 @@ for converter in connection.ops.get_db_converters( while let Some(word) = words.next() { let is_last = words.peek().is_none(); let format_word = format_with(|f| { - write!(f, [dynamic_text(word, None)])?; + write!(f, [text(word, None)])?; if is_last { - write!(f, [text("\"")])?; + write!(f, [token("\"")])?; } Ok(()) @@ -311,9 +311,9 @@ for converter in connection.ops.get_db_converters( write!( f, [group(&format_args![ - if_group_breaks(&text("(")), + if_group_breaks(&token("(")), soft_block_indent(&format_str), - if_group_breaks(&text(")")) + if_group_breaks(&token(")")) ])] ) } diff --git a/crates/ruff_python_formatter/src/other/alias.rs b/crates/ruff_python_formatter/src/other/alias.rs index 020c0d77fde7f..0020c872cc454 100644 --- a/crates/ruff_python_formatter/src/other/alias.rs +++ b/crates/ruff_python_formatter/src/other/alias.rs @@ -15,7 +15,7 @@ impl FormatNodeRule for FormatAlias { } = item; name.format().fmt(f)?; if let Some(asname) = asname { - write!(f, [space(), text("as"), space(), asname.format()])?; + write!(f, [space(), token("as"), space(), asname.format()])?; } Ok(()) } diff --git a/crates/ruff_python_formatter/src/other/comprehension.rs b/crates/ruff_python_formatter/src/other/comprehension.rs index 23224f56c8d25..d5f439a676b3e 100644 --- a/crates/ruff_python_formatter/src/other/comprehension.rs +++ b/crates/ruff_python_formatter/src/other/comprehension.rs @@ -32,7 +32,7 @@ impl FormatNodeRule for FormatComprehension { } = item; if *is_async { - write!(f, [text("async"), space()])?; + write!(f, [token("async"), space()])?; } let comments = f.context().comments().clone(); @@ -54,14 +54,14 @@ impl FormatNodeRule for FormatComprehension { write!( f, [ - text("for"), + token("for"), trailing_comments(before_target_comments), group(&format_args!( Spacer(target), ExprTupleWithoutParentheses(target), in_spacer, leading_comments(before_in_comments), - text("in"), + token("in"), trailing_comments(trailing_in_comments), Spacer(iter), iter.format(), @@ -81,7 +81,7 @@ impl FormatNodeRule for FormatComprehension { ); joiner.entry(&group(&format_args!( leading_comments(own_line_if_comments), - text("if"), + token("if"), trailing_comments(end_of_line_if_comments), Spacer(if_case), if_case.format(), diff --git a/crates/ruff_python_formatter/src/other/decorator.rs b/crates/ruff_python_formatter/src/other/decorator.rs index 241afcd125e4c..9754ac4b4aef1 100644 --- a/crates/ruff_python_formatter/src/other/decorator.rs +++ b/crates/ruff_python_formatter/src/other/decorator.rs @@ -19,7 +19,7 @@ impl FormatNodeRule for FormatDecorator { write!( f, [ - text("@"), + token("@"), maybe_parenthesize_expression(expression, item, Parenthesize::Optional) ] ) diff --git a/crates/ruff_python_formatter/src/other/except_handler_except_handler.rs b/crates/ruff_python_formatter/src/other/except_handler_except_handler.rs index 73d4a08195569..387628af1d368 100644 --- a/crates/ruff_python_formatter/src/other/except_handler_except_handler.rs +++ b/crates/ruff_python_formatter/src/other/except_handler_except_handler.rs @@ -57,10 +57,10 @@ impl FormatNodeRule for FormatExceptHandlerExceptHan write!( f, [ - text("except"), + token("except"), match self.except_handler_kind { ExceptHandlerKind::Regular => None, - ExceptHandlerKind::Starred => Some(text("*")), + ExceptHandlerKind::Starred => Some(token("*")), } ] )?; @@ -78,7 +78,7 @@ impl FormatNodeRule for FormatExceptHandlerExceptHan ] )?; if let Some(name) = name { - write!(f, [space(), text("as"), space(), name.format()])?; + write!(f, [space(), token("as"), space(), name.format()])?; } } diff --git a/crates/ruff_python_formatter/src/other/keyword.rs b/crates/ruff_python_formatter/src/other/keyword.rs index 914a467ef8f9c..6c568832423fb 100644 --- a/crates/ruff_python_formatter/src/other/keyword.rs +++ b/crates/ruff_python_formatter/src/other/keyword.rs @@ -15,9 +15,9 @@ impl FormatNodeRule for FormatKeyword { } = item; // Comments after the `=` or `**` are reassigned as leading comments on the value. if let Some(arg) = arg { - write!(f, [arg.format(), text("="), value.format()]) + write!(f, [arg.format(), token("="), value.format()]) } else { - write!(f, [text("**"), value.format()]) + write!(f, [token("**"), value.format()]) } } } diff --git a/crates/ruff_python_formatter/src/other/match_case.rs b/crates/ruff_python_formatter/src/other/match_case.rs index 10b60921ca6ad..0b965583d5e3d 100644 --- a/crates/ruff_python_formatter/src/other/match_case.rs +++ b/crates/ruff_python_formatter/src/other/match_case.rs @@ -30,7 +30,7 @@ impl FormatNodeRule for FormatMatchCase { ClauseHeader::MatchCase(item), dangling_item_comments, &format_with(|f| { - write!(f, [text("case"), space()])?; + write!(f, [token("case"), space()])?; let has_comments = comments.has_leading(pattern) || comments.has_trailing_own_line(pattern); @@ -58,7 +58,7 @@ impl FormatNodeRule for FormatMatchCase { } if let Some(guard) = guard { - write!(f, [space(), text("if"), space(), guard.format()])?; + write!(f, [space(), token("if"), space(), guard.format()])?; } Ok(()) diff --git a/crates/ruff_python_formatter/src/other/parameter.rs b/crates/ruff_python_formatter/src/other/parameter.rs index a204e3e8a4cf3..fb0e84fbcd46d 100644 --- a/crates/ruff_python_formatter/src/other/parameter.rs +++ b/crates/ruff_python_formatter/src/other/parameter.rs @@ -17,7 +17,7 @@ impl FormatNodeRule for FormatParameter { name.format().fmt(f)?; if let Some(annotation) = annotation { - write!(f, [text(":"), space(), annotation.format()])?; + write!(f, [token(":"), space(), annotation.format()])?; } Ok(()) diff --git a/crates/ruff_python_formatter/src/other/parameter_with_default.rs b/crates/ruff_python_formatter/src/other/parameter_with_default.rs index 40e7508d8d4e0..7afcb1b31a959 100644 --- a/crates/ruff_python_formatter/src/other/parameter_with_default.rs +++ b/crates/ruff_python_formatter/src/other/parameter_with_default.rs @@ -18,7 +18,7 @@ impl FormatNodeRule for FormatParameterWithDefault { if let Some(default) = default { let space = parameter.annotation.is_some().then_some(space()); - write!(f, [space, text("="), space, group(&default.format())])?; + write!(f, [space, token("="), space, group(&default.format())])?; } Ok(()) diff --git a/crates/ruff_python_formatter/src/other/parameters.rs b/crates/ruff_python_formatter/src/other/parameters.rs index cb5666fbed25c..2194a579f4d26 100644 --- a/crates/ruff_python_formatter/src/other/parameters.rs +++ b/crates/ruff_python_formatter/src/other/parameters.rs @@ -102,7 +102,7 @@ impl FormatNodeRule for FormatParameters { dangling.split_at(parenthesis_comments_end); let format_inner = format_with(|f: &mut PyFormatter| { - let separator = format_with(|f| write!(f, [text(","), soft_line_break_or_space()])); + let separator = format_with(|f| write!(f, [token(","), soft_line_break_or_space()])); let mut joiner = f.join_with(separator); let mut last_node: Option = None; @@ -156,7 +156,7 @@ impl FormatNodeRule for FormatParameters { if let Some(vararg) = vararg { joiner.entry(&format_args![ leading_node_comments(vararg.as_ref()), - text("*"), + token("*"), vararg.format() ]); last_node = Some(vararg.as_any_node_ref()); @@ -192,7 +192,7 @@ impl FormatNodeRule for FormatParameters { if let Some(kwarg) = kwarg { joiner.entry(&format_args![ leading_node_comments(kwarg.as_ref()), - text("**"), + token("**"), kwarg.format() ]); last_node = Some(kwarg.as_any_node_ref()); @@ -216,10 +216,10 @@ impl FormatNodeRule for FormatParameters { // For lambdas (no parentheses), preserve the trailing comma. It doesn't // behave like a magic trailing comma, it's just preserved if has_trailing_comma(item, last_node, f.context().source()) { - write!(f, [text(",")])?; + write!(f, [token(",")])?; } } else { - write!(f, [if_group_breaks(&text(","))])?; + write!(f, [if_group_breaks(&token(","))])?; if f.options().magic_trailing_comma().is_respect() && has_trailing_comma(item, last_node, f.context().source()) @@ -252,10 +252,10 @@ impl FormatNodeRule for FormatParameters { write!( f, [ - text("("), + token("("), dangling_open_parenthesis_comments(parenthesis_dangling), soft_block_indent(&group(&format_inner)), - text(")") + token(")") ] ) } @@ -279,7 +279,7 @@ struct CommentsAroundText<'a> { impl Format> for CommentsAroundText<'_> { fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> { if self.comments.is_empty() { - text(self.text).fmt(f) + token(self.text).fmt(f) } else { // There might be own line comments in trailing, but those are weird and we can kinda // ignore them @@ -301,7 +301,7 @@ impl Format> for CommentsAroundText<'_> { f, [ leading_comments(leading), - text(self.text), + token(self.text), trailing_comments(trailing) ] ) diff --git a/crates/ruff_python_formatter/src/other/with_item.rs b/crates/ruff_python_formatter/src/other/with_item.rs index fd91501c0649f..0559e4a993d9a 100644 --- a/crates/ruff_python_formatter/src/other/with_item.rs +++ b/crates/ruff_python_formatter/src/other/with_item.rs @@ -30,7 +30,7 @@ impl FormatNodeRule for FormatWithItem { )?; if let Some(optional_vars) = optional_vars { - write!(f, [space(), text("as"), space()])?; + write!(f, [space(), token("as"), space()])?; if trailing_as_comments.is_empty() { write!(f, [optional_vars.format()])?; diff --git a/crates/ruff_python_formatter/src/pattern/pattern_keyword.rs b/crates/ruff_python_formatter/src/pattern/pattern_keyword.rs index 35bae9ddf49a7..974e6534743cd 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_keyword.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_keyword.rs @@ -12,6 +12,6 @@ impl FormatNodeRule for FormatPatternKeyword { attr, pattern, } = item; - write!(f, [attr.format(), text("="), pattern.format()]) + write!(f, [attr.format(), token("="), pattern.format()]) } } diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_as.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_as.rs index d16f4884d3ef1..921e548ac0b07 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_as.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_as.rs @@ -29,7 +29,7 @@ impl FormatNodeRule for FormatPatternMatchAs { write!(f, [space()])?; } - write!(f, [text("as")])?; + write!(f, [token("as")])?; let trailing_as_comments = comments.dangling(item); if trailing_as_comments.is_empty() { @@ -45,7 +45,7 @@ impl FormatNodeRule for FormatPatternMatchAs { name.format().fmt(f) } else { debug_assert!(pattern.is_none()); - text("_").fmt(f) + token("_").fmt(f) } } diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_mapping.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_mapping.rs index f6584be7a792e..1145f64bdd532 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_mapping.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_mapping.rs @@ -130,7 +130,7 @@ impl Format> for RestPattern<'_> { f, [ leading_comments(self.comments), - text("**"), + token("**"), self.identifier.format() ] ) @@ -156,7 +156,7 @@ impl Format> for KeyPatternPair<'_> { f, [group(&format_args![ self.key.format(), - text(":"), + token(":"), space(), self.pattern.format() ])] diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_or.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_or.rs index 8d84a240e9337..4de2af6c3ebe9 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_or.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_or.rs @@ -35,7 +35,7 @@ impl FormatNodeRule for FormatPatternMatchOr { [hard_line_break(), leading_comments(leading_value_comments)] )?; } - write!(f, [text("|"), space(), pattern.format()])?; + write!(f, [token("|"), space(), pattern.format()])?; } Ok(()) diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_singleton.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_singleton.rs index 56b0c223daac0..f1aabfddc33f4 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_singleton.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_singleton.rs @@ -10,9 +10,9 @@ pub struct FormatPatternMatchSingleton; impl FormatNodeRule for FormatPatternMatchSingleton { fn fmt_fields(&self, item: &PatternMatchSingleton, f: &mut PyFormatter) -> FormatResult<()> { match item.value { - Constant::None => text("None").fmt(f), - Constant::Bool(true) => text("True").fmt(f), - Constant::Bool(false) => text("False").fmt(f), + Constant::None => token("None").fmt(f), + Constant::Bool(true) => token("True").fmt(f), + Constant::Bool(false) => token("False").fmt(f), _ => unreachable!(), } } diff --git a/crates/ruff_python_formatter/src/pattern/pattern_match_star.rs b/crates/ruff_python_formatter/src/pattern/pattern_match_star.rs index 8b93d2b81cdf7..c8a88fe77353e 100644 --- a/crates/ruff_python_formatter/src/pattern/pattern_match_star.rs +++ b/crates/ruff_python_formatter/src/pattern/pattern_match_star.rs @@ -16,11 +16,11 @@ impl FormatNodeRule for FormatPatternMatchStar { let comments = f.context().comments().clone(); let dangling = comments.dangling(item); - write!(f, [text("*"), dangling_comments(dangling)])?; + write!(f, [token("*"), dangling_comments(dangling)])?; match name { Some(name) => write!(f, [name.format()]), - None => write!(f, [text("_")]), + None => write!(f, [token("_")]), } } diff --git a/crates/ruff_python_formatter/src/statement/clause.rs b/crates/ruff_python_formatter/src/statement/clause.rs index 348de0e4fa14b..449b64e299644 100644 --- a/crates/ruff_python_formatter/src/statement/clause.rs +++ b/crates/ruff_python_formatter/src/statement/clause.rs @@ -349,7 +349,7 @@ impl<'ast> Format> for FormatClauseHeader<'_, 'ast> { write_suppressed_clause_header(self.header, f)?; } else { f.write_fmt(Arguments::from(&self.formatter))?; - text(":").fmt(f)?; + token(":").fmt(f)?; } trailing_comments(self.trailing_colon_comment).fmt(f) diff --git a/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs b/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs index 6edc0a1320c14..a29798d4c3316 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs @@ -23,7 +23,7 @@ impl FormatNodeRule for FormatStmtAnnAssign { f, [ target.format(), - text(":"), + token(":"), space(), maybe_parenthesize_expression(annotation, item, Parenthesize::IfBreaks) ] @@ -34,7 +34,7 @@ impl FormatNodeRule for FormatStmtAnnAssign { f, [ space(), - text("="), + token("="), space(), maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks) ] diff --git a/crates/ruff_python_formatter/src/statement/stmt_assert.rs b/crates/ruff_python_formatter/src/statement/stmt_assert.rs index ae4781328319d..7b3930d252d74 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_assert.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_assert.rs @@ -1,4 +1,4 @@ -use ruff_formatter::prelude::{space, text}; +use ruff_formatter::prelude::{space, token}; use ruff_formatter::write; use ruff_python_ast::StmtAssert; @@ -22,7 +22,7 @@ impl FormatNodeRule for FormatStmtAssert { write!( f, [ - text("assert"), + token("assert"), space(), maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks) ] @@ -32,7 +32,7 @@ impl FormatNodeRule for FormatStmtAssert { write!( f, [ - text(","), + token(","), space(), maybe_parenthesize_expression(msg, item, Parenthesize::IfBreaks), ] diff --git a/crates/ruff_python_formatter/src/statement/stmt_assign.rs b/crates/ruff_python_formatter/src/statement/stmt_assign.rs index 3f9b36aa0aaf1..475a21a58b63e 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_assign.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_assign.rs @@ -27,7 +27,7 @@ impl FormatNodeRule for FormatStmtAssign { [ first.format(), space(), - text("="), + token("="), space(), FormatTargets { targets: rest } ] @@ -89,9 +89,9 @@ impl Format> for FormatTargets<'_> { write!( f, [ - if_group_breaks(&text("(")), + if_group_breaks(&token("(")), soft_block_indent(&first.format().with_options(Parentheses::Never)), - if_group_breaks(&text(")")) + if_group_breaks(&token(")")) ] ) } @@ -103,7 +103,7 @@ impl Format> for FormatTargets<'_> { [group(&format_args![ format_first, space(), - text("="), + token("="), space(), FormatTargets { targets: rest } ]) diff --git a/crates/ruff_python_formatter/src/statement/stmt_aug_assign.rs b/crates/ruff_python_formatter/src/statement/stmt_aug_assign.rs index 652247db46606..df15aaa12173b 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_aug_assign.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_aug_assign.rs @@ -24,7 +24,7 @@ impl FormatNodeRule for FormatStmtAugAssign { target.format(), space(), op.format(), - text("="), + token("="), space(), maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks) ] diff --git a/crates/ruff_python_formatter/src/statement/stmt_break.rs b/crates/ruff_python_formatter/src/statement/stmt_break.rs index 394cd03f74f10..67926e0a0e9ed 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_break.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_break.rs @@ -8,7 +8,7 @@ pub struct FormatStmtBreak; impl FormatNodeRule for FormatStmtBreak { fn fmt_fields(&self, _item: &StmtBreak, f: &mut PyFormatter) -> FormatResult<()> { - text("break").fmt(f) + token("break").fmt(f) } fn is_suppressed( diff --git a/crates/ruff_python_formatter/src/statement/stmt_class_def.rs b/crates/ruff_python_formatter/src/statement/stmt_class_def.rs index f3c170902a13a..fa27970b9056d 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_class_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_class_def.rs @@ -44,7 +44,7 @@ impl FormatNodeRule for FormatStmtClassDef { ClauseHeader::Class(item), trailing_definition_comments, &format_with(|f| { - write!(f, [text("class"), space(), name.format()])?; + write!(f, [token("class"), space(), name.format()])?; if let Some(type_params) = type_params.as_deref() { write!(f, [type_params.format()])?; diff --git a/crates/ruff_python_formatter/src/statement/stmt_continue.rs b/crates/ruff_python_formatter/src/statement/stmt_continue.rs index 625f2d8fcee3b..af045d241814b 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_continue.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_continue.rs @@ -8,7 +8,7 @@ pub struct FormatStmtContinue; impl FormatNodeRule for FormatStmtContinue { fn fmt_fields(&self, _item: &StmtContinue, f: &mut PyFormatter) -> FormatResult<()> { - text("continue").fmt(f) + token("continue").fmt(f) } fn is_suppressed( diff --git a/crates/ruff_python_formatter/src/statement/stmt_delete.rs b/crates/ruff_python_formatter/src/statement/stmt_delete.rs index 485a317ae3715..4af373c801258 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_delete.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_delete.rs @@ -15,7 +15,7 @@ impl FormatNodeRule for FormatStmtDelete { fn fmt_fields(&self, item: &StmtDelete, f: &mut PyFormatter) -> FormatResult<()> { let StmtDelete { range: _, targets } = item; - write!(f, [text("del"), space()])?; + write!(f, [token("del"), space()])?; match targets.as_slice() { [] => { @@ -27,9 +27,9 @@ impl FormatNodeRule for FormatStmtDelete { // del ( // # Dangling comment // ) - text("("), + token("("), block_indent(&dangling_node_comments(item)), - text(")"), + token(")"), ] ) } diff --git a/crates/ruff_python_formatter/src/statement/stmt_for.rs b/crates/ruff_python_formatter/src/statement/stmt_for.rs index a66cc1888b195..f70ca9269d752 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_for.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_for.rs @@ -54,12 +54,12 @@ impl FormatNodeRule for FormatStmtFor { ClauseHeader::For(item), trailing_condition_comments, &format_args![ - is_async.then_some(format_args![text("async"), space()]), - text("for"), + is_async.then_some(format_args![token("async"), space()]), + token("for"), space(), ExprTupleWithoutParentheses(target), space(), - text("in"), + token("in"), space(), maybe_parenthesize_expression(iter, item, Parenthesize::IfBreaks), ], @@ -83,7 +83,7 @@ impl FormatNodeRule for FormatStmtFor { clause_header( ClauseHeader::OrElse(ElseClause::For(item)), trailing, - &text("else"), + &token("else"), ) .with_leading_comments(leading, body.last()), clause_body(orelse, trailing), diff --git a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs index 2e6a17fed51da..0a7a462282c95 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs @@ -50,10 +50,10 @@ impl FormatNodeRule for FormatStmtFunctionDef { trailing_definition_comments, &format_with(|f| { if *is_async { - write!(f, [text("async"), space()])?; + write!(f, [token("async"), space()])?; } - write!(f, [text("def"), space(), name.format()])?; + write!(f, [token("def"), space(), name.format()])?; if let Some(type_params) = type_params.as_ref() { write!(f, [type_params.format()])?; @@ -63,7 +63,7 @@ impl FormatNodeRule for FormatStmtFunctionDef { write!(f, [parameters.format()])?; if let Some(return_annotation) = returns.as_ref() { - write!(f, [space(), text("->"), space()])?; + write!(f, [space(), token("->"), space()])?; if return_annotation.is_tuple_expr() { let parentheses = diff --git a/crates/ruff_python_formatter/src/statement/stmt_global.rs b/crates/ruff_python_formatter/src/statement/stmt_global.rs index c837daff08a2c..731630a3d8e00 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_global.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_global.rs @@ -15,18 +15,18 @@ impl FormatNodeRule for FormatStmtGlobal { // move the comment "off" of the `global` statement. if f.context().comments().has_trailing(item.as_any_node_ref()) { let joined = format_with(|f| { - f.join_with(format_args![text(","), space()]) + f.join_with(format_args![token(","), space()]) .entries(item.names.iter().formatted()) .finish() }); - write!(f, [text("global"), space(), &joined]) + write!(f, [token("global"), space(), &joined]) } else { let joined = format_with(|f| { f.join_with(&format_args![ - text(","), + token(","), space(), - if_group_breaks(&text("\\")), + if_group_breaks(&token("\\")), soft_line_break(), ]) .entries(item.names.iter().formatted()) @@ -36,10 +36,10 @@ impl FormatNodeRule for FormatStmtGlobal { write!( f, [ - text("global"), + token("global"), space(), group(&format_args!( - if_group_breaks(&text("\\")), + if_group_breaks(&token("\\")), soft_line_break(), soft_block_indent(&joined) )) diff --git a/crates/ruff_python_formatter/src/statement/stmt_if.rs b/crates/ruff_python_formatter/src/statement/stmt_if.rs index a842ddf53f494..e5852db8d0dc5 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_if.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_if.rs @@ -30,7 +30,7 @@ impl FormatNodeRule for FormatStmtIf { ClauseHeader::If(item), trailing_colon_comment, &format_args![ - text("if"), + token("if"), space(), maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks), ], @@ -86,13 +86,13 @@ pub(crate) fn format_elif_else_clause( write!( f, [ - text("elif"), + token("elif"), space(), maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks), ] ) } else { - text("else").fmt(f) + token("else").fmt(f) } }), ) diff --git a/crates/ruff_python_formatter/src/statement/stmt_import.rs b/crates/ruff_python_formatter/src/statement/stmt_import.rs index d4c10af939814..97dc4fe830f08 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_import.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_import.rs @@ -11,11 +11,11 @@ impl FormatNodeRule for FormatStmtImport { fn fmt_fields(&self, item: &StmtImport, f: &mut PyFormatter) -> FormatResult<()> { let StmtImport { names, range: _ } = item; let names = format_with(|f| { - f.join_with(&format_args![text(","), space()]) + f.join_with(&format_args![token(","), space()]) .entries(names.iter().formatted()) .finish() }); - write!(f, [text("import"), space(), names]) + write!(f, [token("import"), space(), names]) } fn is_suppressed( diff --git a/crates/ruff_python_formatter/src/statement/stmt_import_from.rs b/crates/ruff_python_formatter/src/statement/stmt_import_from.rs index 5881d6e4125cb..6f6c87fb8a3ca 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_import_from.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_import_from.rs @@ -27,12 +27,12 @@ impl FormatNodeRule for FormatStmtImportFrom { write!( f, [ - text("from"), + token("from"), space(), - dynamic_text(&level_str, None), + text(&level_str, None), module.as_ref().map(AsFormat::format), space(), - text("import"), + token("import"), space(), ] )?; @@ -40,7 +40,7 @@ impl FormatNodeRule for FormatStmtImportFrom { if let [name] = names.as_slice() { // star can't be surrounded by parentheses if name.name.as_str() == "*" { - return text("*").fmt(f); + return token("*").fmt(f); } } diff --git a/crates/ruff_python_formatter/src/statement/stmt_match.rs b/crates/ruff_python_formatter/src/statement/stmt_match.rs index b89c6018a3c44..672cf7bce788f 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_match.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_match.rs @@ -29,7 +29,7 @@ impl FormatNodeRule for FormatStmtMatch { ClauseHeader::Match(item), dangling_item_comments, &format_args![ - text("match"), + token("match"), space(), maybe_parenthesize_expression(subject, item, Parenthesize::IfBreaks), ], diff --git a/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs b/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs index 53884c6f406d0..5be0c7c9eb17d 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_nonlocal.rs @@ -15,18 +15,18 @@ impl FormatNodeRule for FormatStmtNonlocal { // move the comment "off" of the `nonlocal` statement. if f.context().comments().has_trailing(item.as_any_node_ref()) { let joined = format_with(|f| { - f.join_with(format_args![text(","), space()]) + f.join_with(format_args![token(","), space()]) .entries(item.names.iter().formatted()) .finish() }); - write!(f, [text("nonlocal"), space(), &joined]) + write!(f, [token("nonlocal"), space(), &joined]) } else { let joined = format_with(|f| { f.join_with(&format_args![ - text(","), + token(","), space(), - if_group_breaks(&text("\\")), + if_group_breaks(&token("\\")), soft_line_break(), ]) .entries(item.names.iter().formatted()) @@ -36,10 +36,10 @@ impl FormatNodeRule for FormatStmtNonlocal { write!( f, [ - text("nonlocal"), + token("nonlocal"), space(), group(&format_args!( - if_group_breaks(&text("\\")), + if_group_breaks(&token("\\")), soft_line_break(), soft_block_indent(&joined) )) diff --git a/crates/ruff_python_formatter/src/statement/stmt_pass.rs b/crates/ruff_python_formatter/src/statement/stmt_pass.rs index 9a5c5249e2d46..d6a1e74564c63 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_pass.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_pass.rs @@ -8,7 +8,7 @@ pub struct FormatStmtPass; impl FormatNodeRule for FormatStmtPass { fn fmt_fields(&self, _item: &StmtPass, f: &mut PyFormatter) -> FormatResult<()> { - text("pass").fmt(f) + token("pass").fmt(f) } fn is_suppressed( diff --git a/crates/ruff_python_formatter/src/statement/stmt_raise.rs b/crates/ruff_python_formatter/src/statement/stmt_raise.rs index e8c11228deb0c..18c72e7672cb7 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_raise.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_raise.rs @@ -17,7 +17,7 @@ impl FormatNodeRule for FormatStmtRaise { cause, } = item; - text("raise").fmt(f)?; + token("raise").fmt(f)?; if let Some(value) = exc { write!( @@ -34,7 +34,7 @@ impl FormatNodeRule for FormatStmtRaise { f, [ space(), - text("from"), + token("from"), space(), maybe_parenthesize_expression(value, item, Parenthesize::Optional) ] diff --git a/crates/ruff_python_formatter/src/statement/stmt_return.rs b/crates/ruff_python_formatter/src/statement/stmt_return.rs index c6379b5a79da5..be63db2e73f2b 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_return.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_return.rs @@ -14,7 +14,7 @@ impl FormatNodeRule for FormatStmtReturn { fn fmt_fields(&self, item: &StmtReturn, f: &mut PyFormatter) -> FormatResult<()> { let StmtReturn { range: _, value } = item; - text("return").fmt(f)?; + token("return").fmt(f)?; match value.as_deref() { Some(Expr::Tuple(tuple)) if !f.context().comments().has_leading(tuple) => { diff --git a/crates/ruff_python_formatter/src/statement/stmt_try.rs b/crates/ruff_python_formatter/src/statement/stmt_try.rs index 3cbf7ff7f8587..4b40ab45492fa 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_try.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_try.rs @@ -136,7 +136,7 @@ fn format_case<'a>( write!( f, [ - clause_header(header, trailing_case_comments, &text(kind.keyword())) + clause_header(header, trailing_case_comments, &token(kind.keyword())) .with_leading_comments(leading_case_comments, previous_node), clause_body(body, trailing_case_comments), ] diff --git a/crates/ruff_python_formatter/src/statement/stmt_type_alias.rs b/crates/ruff_python_formatter/src/statement/stmt_type_alias.rs index ddb82ebfe7894..c2daaf8528d20 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_type_alias.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_type_alias.rs @@ -18,7 +18,7 @@ impl FormatNodeRule for FormatStmtTypeAlias { range: _, } = item; - write!(f, [text("type"), space(), name.as_ref().format()])?; + write!(f, [token("type"), space(), name.as_ref().format()])?; if let Some(type_params) = type_params { write!(f, [type_params.format()])?; @@ -28,7 +28,7 @@ impl FormatNodeRule for FormatStmtTypeAlias { f, [ space(), - text("="), + token("="), space(), maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks) ] diff --git a/crates/ruff_python_formatter/src/statement/stmt_while.rs b/crates/ruff_python_formatter/src/statement/stmt_while.rs index af667a6cf5fc8..88a9af9831fdc 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_while.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_while.rs @@ -38,7 +38,7 @@ impl FormatNodeRule for FormatStmtWhile { ClauseHeader::While(item), trailing_condition_comments, &format_args![ - text("while"), + token("while"), space(), maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks), ] @@ -60,7 +60,7 @@ impl FormatNodeRule for FormatStmtWhile { clause_header( ClauseHeader::OrElse(ElseClause::While(item)), trailing, - &text("else") + &token("else") ) .with_leading_comments(leading, body.last()), clause_body(orelse, trailing), diff --git a/crates/ruff_python_formatter/src/statement/stmt_with.rs b/crates/ruff_python_formatter/src/statement/stmt_with.rs index 481d3925ae928..9aa5e54bda32e 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_with.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_with.rs @@ -52,8 +52,8 @@ impl FormatNodeRule for FormatStmtWith { f, [ item.is_async - .then_some(format_args![text("async"), space()]), - text("with"), + .then_some(format_args![token("async"), space()]), + token("with"), space() ] )?; @@ -92,7 +92,7 @@ impl FormatNodeRule for FormatStmtWith { item.format().fmt(f)?; } } else { - f.join_with(format_args![text(","), space()]) + f.join_with(format_args![token(","), space()]) .entries(item.items.iter().formatted()) .finish()?; } diff --git a/crates/ruff_python_formatter/src/type_param/type_param_param_spec.rs b/crates/ruff_python_formatter/src/type_param/type_param_param_spec.rs index f9a3a7ed7309f..c64545ce846c7 100644 --- a/crates/ruff_python_formatter/src/type_param/type_param_param_spec.rs +++ b/crates/ruff_python_formatter/src/type_param/type_param_param_spec.rs @@ -9,6 +9,6 @@ pub struct FormatTypeParamParamSpec; impl FormatNodeRule for FormatTypeParamParamSpec { fn fmt_fields(&self, item: &TypeParamParamSpec, f: &mut PyFormatter) -> FormatResult<()> { let TypeParamParamSpec { range: _, name } = item; - write!(f, [text("**"), name.format()]) + write!(f, [token("**"), name.format()]) } } diff --git a/crates/ruff_python_formatter/src/type_param/type_param_type_var.rs b/crates/ruff_python_formatter/src/type_param/type_param_type_var.rs index 8a86f905929f4..fbd3e7877538d 100644 --- a/crates/ruff_python_formatter/src/type_param/type_param_type_var.rs +++ b/crates/ruff_python_formatter/src/type_param/type_param_type_var.rs @@ -15,7 +15,7 @@ impl FormatNodeRule for FormatTypeParamTypeVar { } = item; name.format().fmt(f)?; if let Some(bound) = bound { - write!(f, [text(":"), space(), bound.format()])?; + write!(f, [token(":"), space(), bound.format()])?; } Ok(()) } diff --git a/crates/ruff_python_formatter/src/type_param/type_param_type_var_tuple.rs b/crates/ruff_python_formatter/src/type_param/type_param_type_var_tuple.rs index 66d1a5307e539..4f7ae7ca9dcc8 100644 --- a/crates/ruff_python_formatter/src/type_param/type_param_type_var_tuple.rs +++ b/crates/ruff_python_formatter/src/type_param/type_param_type_var_tuple.rs @@ -9,6 +9,6 @@ pub struct FormatTypeParamTypeVarTuple; impl FormatNodeRule for FormatTypeParamTypeVarTuple { fn fmt_fields(&self, item: &TypeParamTypeVarTuple, f: &mut PyFormatter) -> FormatResult<()> { let TypeParamTypeVarTuple { range: _, name } = item; - write!(f, [text("*"), name.format()]) + write!(f, [token("*"), name.format()]) } } diff --git a/crates/ruff_python_formatter/src/verbatim.rs b/crates/ruff_python_formatter/src/verbatim.rs index f50aac90a90e0..f40723de99ee7 100644 --- a/crates/ruff_python_formatter/src/verbatim.rs +++ b/crates/ruff_python_formatter/src/verbatim.rs @@ -871,7 +871,7 @@ impl Format> for VerbatimText { write!( f, [ - dynamic_text(&cleaned, Some(self.verbatim_range.start())), + text(&cleaned, Some(self.verbatim_range.start())), source_position(self.verbatim_range.end()) ] )?;