Skip to content

Commit 73ee72b

Browse files
authored
Join implicit concatenated strings when they fit on a line (#13663)
1 parent e402e27 commit 73ee72b

File tree

50 files changed

+3910
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3910
-366
lines changed

crates/ruff_formatter/src/builders.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ impl<Context> std::fmt::Debug for Group<'_, Context> {
14541454
/// layout doesn't exceed the line width too, in which case it falls back to the flat layout.
14551455
///
14561456
/// This IR is identical to the following [`best_fitting`] layout but is implemented as custom IR for
1457-
/// best performance.
1457+
/// better performance.
14581458
///
14591459
/// ```rust
14601460
/// # use ruff_formatter::prelude::*;

crates/ruff_formatter/src/format_extensions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ where
139139
/// # Ok(())
140140
/// # }
141141
/// ```
142-
pub fn inspect(&mut self, f: &mut Formatter<Context>) -> FormatResult<&[FormatElement]> {
142+
pub fn inspect(&self, f: &mut Formatter<Context>) -> FormatResult<&[FormatElement]> {
143143
let result = self.memory.get_or_init(|| f.intern(&self.inner));
144144

145145
match result.as_ref() {

crates/ruff_python_ast/src/expression.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ pub enum StringLikePart<'a> {
506506
FString(&'a ast::FString),
507507
}
508508

509-
impl StringLikePart<'_> {
509+
impl<'a> StringLikePart<'a> {
510510
/// Returns the [`AnyStringFlags`] for the current string-like part.
511511
pub fn flags(&self) -> AnyStringFlags {
512512
match self {
@@ -525,6 +525,17 @@ impl StringLikePart<'_> {
525525
)
526526
}
527527

528+
pub const fn is_string_literal(self) -> bool {
529+
matches!(self, Self::String(_))
530+
}
531+
532+
pub const fn as_string_literal(self) -> Option<&'a ast::StringLiteral> {
533+
match self {
534+
StringLikePart::String(value) => Some(value),
535+
_ => None,
536+
}
537+
}
538+
528539
pub const fn is_fstring(self) -> bool {
529540
matches!(self, Self::FString(_))
530541
}
@@ -571,6 +582,7 @@ impl Ranged for StringLikePart<'_> {
571582
/// An iterator over all the [`StringLikePart`] of a string-like expression.
572583
///
573584
/// This is created by the [`StringLike::parts`] method.
585+
#[derive(Clone)]
574586
pub enum StringLikePartIter<'a> {
575587
String(std::slice::Iter<'a, ast::StringLiteral>),
576588
Bytes(std::slice::Iter<'a, ast::BytesLiteral>),
@@ -607,5 +619,25 @@ impl<'a> Iterator for StringLikePartIter<'a> {
607619
}
608620
}
609621

622+
impl DoubleEndedIterator for StringLikePartIter<'_> {
623+
fn next_back(&mut self) -> Option<Self::Item> {
624+
let part = match self {
625+
StringLikePartIter::String(inner) => StringLikePart::String(inner.next_back()?),
626+
StringLikePartIter::Bytes(inner) => StringLikePart::Bytes(inner.next_back()?),
627+
StringLikePartIter::FString(inner) => {
628+
let part = inner.next_back()?;
629+
match part {
630+
ast::FStringPart::Literal(string_literal) => {
631+
StringLikePart::String(string_literal)
632+
}
633+
ast::FStringPart::FString(f_string) => StringLikePart::FString(f_string),
634+
}
635+
}
636+
};
637+
638+
Some(part)
639+
}
640+
}
641+
610642
impl FusedIterator for StringLikePartIter<'_> {}
611643
impl ExactSizeIterator for StringLikePartIter<'_> {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{
3+
"preview": "enabled"
4+
}
5+
]

0 commit comments

Comments
 (0)