diff --git a/crates/ruff_python_formatter/src/lib.rs b/crates/ruff_python_formatter/src/lib.rs index 5a7d357eb1..552f839a33 100644 --- a/crates/ruff_python_formatter/src/lib.rs +++ b/crates/ruff_python_formatter/src/lib.rs @@ -64,9 +64,11 @@ mod tests { use std::path::Path; use anyhow::Result; - use similar::TextDiff; + use insta::assert_snapshot; use ruff_testing_macros::fixture; + use ruff_text_size::TextSize; + use similar::TextDiff; use crate::fmt; @@ -176,6 +178,77 @@ mod tests { ); } + #[test] + fn string_processing() { + use ruff_formatter::prelude::*; + use ruff_formatter::{format, format_args, write}; + + struct FormatString<'a>(&'a str); + + impl Format for FormatString<'_> { + fn fmt( + &self, + f: &mut ruff_formatter::formatter::Formatter, + ) -> FormatResult<()> { + let format_str = format_with(|f| { + write!(f, [text("\"")])?; + + 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("\"")), + soft_line_break_or_space(), + if_group_breaks(&text("\" ")) + ]) + .fmt(f) + }); + + while let Some(word) = words.next() { + let is_last = words.peek().is_none(); + let format_word = format_with(|f| { + write!(f, [dynamic_text(word, TextSize::default())])?; + + if is_last { + write!(f, [text("\"")])?; + } + + Ok(()) + }); + + fill.entry(&separator, &format_word); + } + + fill.finish() + }); + + write!( + f, + [group(&format_args![ + if_group_breaks(&text("(")), + soft_block_indent(&format_str), + if_group_breaks(&text(")")) + ])] + ) + } + } + + // 77 after g group (leading quote) + let fits = + r#"aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg h"#; + let breaks = + r#"aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hh"#; + + let output = format!( + SimpleFormatContext::default(), + [FormatString(fits), hard_line_break(), FormatString(breaks)] + ) + .expect("Formatting to succeed"); + + assert_snapshot!(output.print().expect("Printing to succeed").as_code()); + } + struct Header<'a> { title: &'a str, } diff --git a/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__string_processing.snap b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__string_processing.snap new file mode 100644 index 0000000000..fca7f8344e --- /dev/null +++ b/crates/ruff_python_formatter/src/snapshots/ruff_python_formatter__tests__string_processing.snap @@ -0,0 +1,9 @@ +--- +source: crates/ruff_python_formatter/src/lib.rs +expression: "output.print().expect(\"Printing to succeed\").as_code()" +--- +"aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg h" +( + "aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg" + " hh" +)