diff --git a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/join_implicit_concatenated_string.py b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/join_implicit_concatenated_string.py index 6711e84be66817..89451cad1a59ba 100644 --- a/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/join_implicit_concatenated_string.py +++ b/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/join_implicit_concatenated_string.py @@ -9,6 +9,7 @@ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) # too long to join + "diffent '" 'quote "are fine"' # join @@ -23,6 +24,22 @@ b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" b"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) # too long to join + +# Skip joining if there is a trailing comment +( + "fffffffffffff" + "bbbbbbbbbbbbb" # comment + "cccccccccccccc" +) + +# Skip joining if there is a leading comment +( + "fffffffffffff" + # comment + "bbbbbbbbbbbbb" + "cccccccccccccc" +) + ############################################################################## # F-strings ############################################################################## diff --git a/crates/ruff_python_formatter/src/string/implicit.rs b/crates/ruff_python_formatter/src/string/implicit.rs index da48fe59a93a18..257147c5744370 100644 --- a/crates/ruff_python_formatter/src/string/implicit.rs +++ b/crates/ruff_python_formatter/src/string/implicit.rs @@ -1,5 +1,6 @@ use std::borrow::Cow; +use ruff_formatter::{format_args, write}; use ruff_python_ast::str::Quote; use ruff_python_ast::str_prefix::{ AnyStringPrefix, ByteStringPrefix, FStringPrefix, StringLiteralPrefix, @@ -37,13 +38,9 @@ impl Format> for FormatImplicitConcatenatedString<'_> { // if it fits on the line. Otherwise, parenthesize the string parts and format each part on its // own line. if let Some(flat) = FormatImplicitConcatenatedStringFlat::new(self.string, f.context()) { - ruff_formatter::write!( + write!( f, - [ - // TODO: strings in expression statements aren't joined correctly because they aren't wrap in a group :( - if_group_fits_on_line(&flat), - if_group_breaks(&expanded) - ] + [if_group_fits_on_line(&flat), if_group_breaks(&expanded)] ) } else { expanded.fmt(f) @@ -87,7 +84,7 @@ impl Format> for FormatImplicitConcatenatedStringExpanded<'_ }); let part_comments = comments.leading_dangling_trailing(&part); - joiner.entry(&ruff_formatter::format_args![ + joiner.entry(&format_args![ line_suffix_boundary(), leading_comments(part_comments.leading), format_part, @@ -190,7 +187,7 @@ impl Format> for FormatImplicitConcatenatedStringFlat<'_> { // Merges all string parts into a single string. let quotes = StringQuotes::from(self.flags); - ruff_formatter::write!(f, [self.flags.prefix(), quotes])?; + write!(f, [self.flags.prefix(), quotes])?; // TODO: FStrings when the f-string preview style is enabled??? diff --git a/crates/ruff_python_formatter/tests/snapshots/format@expression__join_implicit_concatenated_string.py.snap b/crates/ruff_python_formatter/tests/snapshots/format@expression__join_implicit_concatenated_string.py.snap index 54b87aefd0a342..c46805e07930a0 100644 --- a/crates/ruff_python_formatter/tests/snapshots/format@expression__join_implicit_concatenated_string.py.snap +++ b/crates/ruff_python_formatter/tests/snapshots/format@expression__join_implicit_concatenated_string.py.snap @@ -15,6 +15,7 @@ input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/expression "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) # too long to join + "diffent '" 'quote "are fine"' # join @@ -29,6 +30,22 @@ b"aaaaaaaaa" b"bbbbbbbbbbbbbbbbbbbb" # Join b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" b"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) # too long to join + +# Skip joining if there is a trailing comment +( + "fffffffffffff" + "bbbbbbbbbbbbb" # comment + "cccccccccccccc" +) + +# Skip joining if there is a leading comment +( + "fffffffffffff" + # comment + "bbbbbbbbbbbbb" + "cccccccccccccc" +) + ############################################################################## # F-strings ############################################################################## @@ -218,6 +235,7 @@ source_type = Python "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) # too long to join + 'diffent \'quote "are fine"' # join @@ -231,6 +249,22 @@ b"aaaaaaaaabbbbbbbbbbbbbbbbbbbb" # Join b"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ) # too long to join + +# Skip joining if there is a trailing comment +( + "fffffffffffff" + "bbbbbbbbbbbbb" # comment + "cccccccccccccc" +) + +# Skip joining if there is a leading comment +( + "fffffffffffff" + # comment + "bbbbbbbbbbbbb" + "cccccccccccccc" +) + ############################################################################## # F-strings ##############################################################################