| 345 | |
| 346 | #[test] |
| 347 | fn string_processing() { |
| 348 | use crate::prelude::*; |
| 349 | use ruff_formatter::{format, format_args, write}; |
| 350 | |
| 351 | struct FormatString<'a>(&'a str); |
| 352 | |
| 353 | impl Format<SimpleFormatContext> for FormatString<'_> { |
| 354 | fn fmt(&self, f: &mut Formatter<SimpleFormatContext>) -> FormatResult<()> { |
| 355 | let format_str = format_with(|f| { |
| 356 | write!(f, [token("\"")])?; |
| 357 | |
| 358 | let mut words = self.0.split_whitespace().peekable(); |
| 359 | let mut fill = f.fill(); |
| 360 | |
| 361 | let separator = format_with(|f| { |
| 362 | group(&format_args![ |
| 363 | if_group_breaks(&token("\"")), |
| 364 | soft_line_break_or_space(), |
| 365 | if_group_breaks(&token("\" ")) |
| 366 | ]) |
| 367 | .fmt(f) |
| 368 | }); |
| 369 | |
| 370 | while let Some(word) = words.next() { |
| 371 | let is_last = words.peek().is_none(); |
| 372 | let format_word = format_with(|f| { |
| 373 | write!(f, [text(word)])?; |
| 374 | |
| 375 | if is_last { |
| 376 | write!(f, [token("\"")])?; |
| 377 | } |
| 378 | |
| 379 | Ok(()) |
| 380 | }); |
| 381 | |
| 382 | fill.entry(&separator, &format_word); |
| 383 | } |
| 384 | |
| 385 | fill.finish() |
| 386 | }); |
| 387 | |
| 388 | write!( |
| 389 | f, |
| 390 | [group(&format_args![ |
| 391 | if_group_breaks(&token("(")), |
| 392 | soft_block_indent(&format_str), |
| 393 | if_group_breaks(&token(")")) |
| 394 | ])] |
| 395 | ) |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // 77 after g group (leading quote) |
| 400 | let fits = |
| 401 | r"aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg h"; |
| 402 | let breaks = |
| 403 | r"aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hh"; |
| 404 | |