Skip to content

Commit

Permalink
Add tests for leading/trailing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Oct 18, 2024
1 parent c7b565f commit 845d8ac
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
) # too long to join


"diffent '" 'quote "are fine"' # join


Expand All @@ -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
##############################################################################
Expand Down
13 changes: 5 additions & 8 deletions crates/ruff_python_formatter/src/string/implicit.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -37,13 +38,9 @@ impl Format<PyFormatContext<'_>> 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)
Expand Down Expand Up @@ -87,7 +84,7 @@ impl Format<PyFormatContext<'_>> 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,
Expand Down Expand Up @@ -190,7 +187,7 @@ impl Format<PyFormatContext<'_>> 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???

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
##############################################################################
Expand Down Expand Up @@ -218,6 +235,7 @@ source_type = Python
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
) # too long to join
'diffent \'quote "are fine"' # join
Expand All @@ -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
##############################################################################
Expand Down

0 comments on commit 845d8ac

Please sign in to comment.