Skip to content

Commit

Permalink
refactor: Rename FormatStringContinuation to `FormatImplicitConcate…
Browse files Browse the repository at this point in the history
…natedString` (#13531)
  • Loading branch information
MichaReiser authored Sep 27, 2024
1 parent c046101 commit 253f5f2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
10 changes: 5 additions & 5 deletions crates/ruff_python_formatter/src/expression/binary_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::expression::parentheses::{
};
use crate::expression::OperatorPrecedence;
use crate::prelude::*;
use crate::string::{AnyString, FormatStringContinuation};
use crate::string::{AnyString, FormatImplicitConcatenatedString};

#[derive(Copy, Clone, Debug)]
pub(super) enum BinaryLike<'a> {
Expand Down Expand Up @@ -394,10 +394,10 @@ impl Format<PyFormatContext<'_>> for BinaryLike<'_> {
[
operand.leading_binary_comments().map(leading_comments),
leading_comments(comments.leading(string_constant)),
// Call `FormatStringContinuation` directly to avoid formatting
// Call `FormatImplicitConcatenatedString` directly to avoid formatting
// the implicitly concatenated string with the enclosing group
// because the group is added by the binary like formatting.
FormatStringContinuation::new(&string_constant),
FormatImplicitConcatenatedString::new(string_constant),
trailing_comments(comments.trailing(string_constant)),
operand.trailing_binary_comments().map(trailing_comments),
line_suffix_boundary(),
Expand All @@ -413,10 +413,10 @@ impl Format<PyFormatContext<'_>> for BinaryLike<'_> {
f,
[
leading_comments(comments.leading(string_constant)),
// Call `FormatStringContinuation` directly to avoid formatting
// Call `FormatImplicitConcatenatedString` directly to avoid formatting
// the implicitly concatenated string with the enclosing group
// because the group is added by the binary like formatting.
FormatStringContinuation::new(&string_constant),
FormatImplicitConcatenatedString::new(string_constant),
trailing_comments(comments.trailing(string_constant)),
]
)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::expression::parentheses::{
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
};
use crate::prelude::*;
use crate::string::{AnyString, FormatStringContinuation};
use crate::string::{AnyString, FormatImplicitConcatenatedString};

#[derive(Default)]
pub struct FormatExprBytesLiteral;
Expand All @@ -16,8 +16,7 @@ impl FormatNodeRule<ExprBytesLiteral> for FormatExprBytesLiteral {

match value.as_slice() {
[bytes_literal] => bytes_literal.format().fmt(f),
_ => in_parentheses_only_group(&FormatStringContinuation::new(&AnyString::Bytes(item)))
.fmt(f),
_ => in_parentheses_only_group(&FormatImplicitConcatenatedString::new(item)).fmt(f),
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions crates/ruff_python_formatter/src/expression/expr_f_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::expression::parentheses::{
};
use crate::other::f_string_part::FormatFStringPart;
use crate::prelude::*;
use crate::string::{AnyString, FormatStringContinuation, Quoting};
use crate::string::{AnyString, FormatImplicitConcatenatedString, Quoting};

#[derive(Default)]
pub struct FormatExprFString;
Expand All @@ -22,10 +22,7 @@ impl FormatNodeRule<ExprFString> for FormatExprFString {
f_string_quoting(item, &f.context().locator()),
)
.fmt(f),
_ => {
in_parentheses_only_group(&FormatStringContinuation::new(&AnyString::FString(item)))
.fmt(f)
}
_ => in_parentheses_only_group(&FormatImplicitConcatenatedString::new(item)).fmt(f),
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::expression::parentheses::{
};
use crate::other::string_literal::{FormatStringLiteral, StringLiteralKind};
use crate::prelude::*;
use crate::string::{AnyString, FormatStringContinuation};
use crate::string::{AnyString, FormatImplicitConcatenatedString};

#[derive(Default)]
pub struct FormatExprStringLiteral {
Expand Down Expand Up @@ -55,7 +55,7 @@ impl FormatNodeRule<ExprStringLiteral> for FormatExprStringLiteral {
// ensures that the docstring is a *single* string literal.
assert!(!self.kind.is_docstring());

in_parentheses_only_group(&FormatStringContinuation::new(&AnyString::String(item)))
in_parentheses_only_group(&FormatImplicitConcatenatedString::new(item))
}
.fmt(f),
}
Expand Down
18 changes: 18 additions & 0 deletions crates/ruff_python_formatter/src/string/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ impl<'a> From<&AnyString<'a>> for ExpressionRef<'a> {
}
}

impl<'a> From<&'a ExprBytesLiteral> for AnyString<'a> {
fn from(value: &'a ExprBytesLiteral) -> Self {
AnyString::Bytes(value)
}
}

impl<'a> From<&'a ExprStringLiteral> for AnyString<'a> {
fn from(value: &'a ExprStringLiteral) -> Self {
AnyString::String(value)
}
}

impl<'a> From<&'a ExprFString> for AnyString<'a> {
fn from(value: &'a ExprFString) -> Self {
AnyString::FString(value)
}
}

pub(super) enum AnyStringPartsIter<'a> {
String(std::slice::Iter<'a, StringLiteral>),
Bytes(std::slice::Iter<'a, ast::BytesLiteral>),
Expand Down
19 changes: 11 additions & 8 deletions crates/ruff_python_formatter/src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,32 @@ pub(crate) enum Quoting {

/// Formats any implicitly concatenated string. This could be any valid combination
/// of string, bytes or f-string literals.
pub(crate) struct FormatStringContinuation<'a> {
string: &'a AnyString<'a>,
pub(crate) struct FormatImplicitConcatenatedString<'a> {
string: AnyString<'a>,
}

impl<'a> FormatStringContinuation<'a> {
pub(crate) fn new(string: &'a AnyString<'a>) -> Self {
Self { string }
impl<'a> FormatImplicitConcatenatedString<'a> {
pub(crate) fn new(string: impl Into<AnyString<'a>>) -> Self {
Self {
string: string.into(),
}
}
}

impl Format<PyFormatContext<'_>> for FormatStringContinuation<'_> {
impl Format<PyFormatContext<'_>> for FormatImplicitConcatenatedString<'_> {
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
let comments = f.context().comments().clone();
let quoting = self.string.quoting(&f.context().locator());

let mut joiner = f.join_with(in_parentheses_only_soft_line_break_or_space());

for part in self.string.parts(quoting) {
let part_comments = comments.leading_dangling_trailing(&part);
joiner.entry(&format_args![
line_suffix_boundary(),
leading_comments(comments.leading(&part)),
leading_comments(part_comments.leading),
part,
trailing_comments(comments.trailing(&part))
trailing_comments(part_comments.trailing)
]);
}

Expand Down

0 comments on commit 253f5f2

Please sign in to comment.