Skip to content

Commit

Permalink
Prototype Black's string joining/splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed May 16, 2023
1 parent 6049aab commit 14c7cd2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
72 changes: 72 additions & 0 deletions crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,16 @@ pub fn fmt(contents: &str) -> Result<Formatted<ASTFormatContext>> {
mod tests {
use std::fmt::{Formatter, Write};
use std::fs;
use std::hash::Hasher;
use std::path::Path;

use anyhow::Result;
use insta::assert_snapshot;
use ruff_formatter::prelude::{
format_once, format_with, group, if_group_breaks, soft_block_indent,
};
use ruff_formatter::{Format, FormatResult, SimpleFormatContext};
use ruff_text_size::TextSize;
use similar::TextDiff;

use ruff_testing_macros::fixture;
Expand Down Expand Up @@ -176,6 +183,71 @@ mod tests {
);
}

#[test]
fn string_processing() {
use ruff_formatter::prelude::*;
use ruff_formatter::{format, format_args, write};

// 77 after g group (leading quote)
let src =
r#"aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg hh"#;

struct FormatString<'a>(&'a str);

impl Format<SimpleFormatContext> for FormatString<'_> {
fn fmt(
&self,
f: &mut ruff_formatter::formatter::Formatter<SimpleFormatContext>,
) -> 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 format_word = format_once(|f| {
write!(f, [dynamic_text(word, TextSize::default())])?;

if words.peek().is_none() {
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(")"))
])]
)
}
}

let output = format!(SimpleFormatContext::default(), [FormatString(&src)])
.expect("Formatting to succeed");

assert_snapshot!(output.print().expect("Printing to succeed").as_code())
}

struct Header<'a> {
title: &'a str,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/ruff_python_formatter/src/lib.rs
expression: "output.print().expect(\"Printing to succeed\").as_code()"
---
(
"aaaaaaaaaa bbbbbbbbbb cccccccccc dddddddddd eeeeeeeeee ffffffffff gggggggggg"
"hh"
)
6 changes: 2 additions & 4 deletions crates/ruff_testing_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ fn find_highest_common_ancestor_module(module: &Module) -> &Module {
}
}

#[derive(Debug)]
struct Test {
path: PathBuf,
test_fn: ItemFn,
Expand All @@ -317,7 +316,7 @@ impl Test {
}
}

#[derive(Debug, Default)]
#[derive(Default)]
struct Module {
children: BTreeMap<String, Child>,
}
Expand All @@ -342,7 +341,6 @@ impl Module {
}
}

#[derive(Debug)]
enum Child {
Module(Module),
Test(Test),
Expand All @@ -357,7 +355,7 @@ impl Child {
}
}

#[derive(Debug, Default)]
#[derive(Default)]
struct Modules {
root: Module,
}
Expand Down

0 comments on commit 14c7cd2

Please sign in to comment.