diff --git a/crates/ruff_formatter/src/builders.rs b/crates/ruff_formatter/src/builders.rs index 1b6165c744e620..432f2b21345945 100644 --- a/crates/ruff_formatter/src/builders.rs +++ b/crates/ruff_formatter/src/builders.rs @@ -434,26 +434,56 @@ fn debug_assert_no_newlines(text: &str) { debug_assert!(!text.contains('\r'), "The content '{text}' contains an unsupported '\\r' line terminator character but text must only use line feeds '\\n' as line separator. Use '\\n' instead of '\\r' and '\\r\\n' to insert a line break in strings."); } -/// Pushes some content to the end of the current line. Provide a reserved width in -/// order to include the line suffix content during measurement. +/// Pushes some content to the end of the current line. /// /// ## Examples /// -/// ``` -/// use ruff_formatter::{format}; +/// ```rust +/// use ruff_formatter::format; /// use ruff_formatter::prelude::*; /// -/// fn main() -> FormatResult<()> { +/// # fn main() -> FormatResult<()> { /// let elements = format!(SimpleFormatContext::default(), [ /// text("a"), /// line_suffix(&text("c"), 0), /// text("b") /// ])?; /// -/// assert_eq!( -/// "abc", -/// elements.print()?.as_code() -/// ); +/// assert_eq!("abc", elements.print()?.as_code()); +/// # Ok(()) +/// # } +/// ``` +/// +/// Provide reserved width for the line suffix to include it during measurement. This +/// will push the line suffix to the next line if it exceeds the configured line width. +/// ```rust +/// use ruff_formatter::{format, format_args, LineWidth, SimpleFormatContext, SimpleFormatOptions}; +/// use ruff_formatter::prelude::*; +/// +/// # fn main() -> FormatResult<()> { +/// let context = SimpleFormatContext::new(SimpleFormatOptions { +/// line_width: LineWidth::try_from(2).unwrap(), +/// ..SimpleFormatOptions::default() +/// }); +/// +/// let elements = format!(context, [ +/// group(&format_args![ +/// &format_with(|f| { +/// f.fill() +/// .entry( +/// &soft_line_break(), +/// &format_args![text("a"), text("b")], +/// ) +/// .entry( +/// &soft_line_break(), +/// &line_suffix(&text("c"), 1), +/// ) +/// .finish() +/// }), +/// ]), +/// ])?; +/// +/// assert_eq!("ab\nc", elements.print()?.as_code()); /// # Ok(()) /// # } /// ``` diff --git a/crates/ruff_formatter/src/printer/mod.rs b/crates/ruff_formatter/src/printer/mod.rs index c2be4a30585cc8..42d07e727adf22 100644 --- a/crates/ruff_formatter/src/printer/mod.rs +++ b/crates/ruff_formatter/src/printer/mod.rs @@ -1731,6 +1731,36 @@ two lines`, assert_eq!(printed.as_code(), "[1, 2, 3]; // trailing"); } + #[test] + fn line_suffix_with_reserved_width() { + let printed = format(&format_args![ + group(&format_args![ + text("["), + soft_block_indent(&format_with(|f| { + f.fill() + .entry( + &soft_line_break_or_space(), + &format_args!(text("1"), text(",")), + ) + .entry( + &soft_line_break_or_space(), + &format_args!(text("2"), text(",")), + ) + .entry( + &soft_line_break_or_space(), + &format_args!(text("3"), if_group_breaks(&text(","))), + ) + .finish() + })), + text("]") + ]), + text(";"), + line_suffix(&format_args![space(), text("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")], 80) + ]); + + assert_eq!(printed.as_code(), "[\n 1, 2, 3\n]; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); + } + #[test] fn conditional_with_group_id_in_fits() { let content = format_with(|f| {