Skip to content

Commit

Permalink
Join implicit concatenated strings when they fit on a line (#13663)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Oct 24, 2024
1 parent e402e27 commit 73ee72b
Show file tree
Hide file tree
Showing 50 changed files with 3,910 additions and 366 deletions.
2 changes: 1 addition & 1 deletion crates/ruff_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,7 @@ impl<Context> std::fmt::Debug for Group<'_, Context> {
/// layout doesn't exceed the line width too, in which case it falls back to the flat layout.
///
/// This IR is identical to the following [`best_fitting`] layout but is implemented as custom IR for
/// best performance.
/// better performance.
///
/// ```rust
/// # use ruff_formatter::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_formatter/src/format_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ where
/// # Ok(())
/// # }
/// ```
pub fn inspect(&mut self, f: &mut Formatter<Context>) -> FormatResult<&[FormatElement]> {
pub fn inspect(&self, f: &mut Formatter<Context>) -> FormatResult<&[FormatElement]> {
let result = self.memory.get_or_init(|| f.intern(&self.inner));

match result.as_ref() {
Expand Down
34 changes: 33 additions & 1 deletion crates/ruff_python_ast/src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ pub enum StringLikePart<'a> {
FString(&'a ast::FString),
}

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

pub const fn is_string_literal(self) -> bool {
matches!(self, Self::String(_))
}

pub const fn as_string_literal(self) -> Option<&'a ast::StringLiteral> {
match self {
StringLikePart::String(value) => Some(value),
_ => None,
}
}

pub const fn is_fstring(self) -> bool {
matches!(self, Self::FString(_))
}
Expand Down Expand Up @@ -571,6 +582,7 @@ impl Ranged for StringLikePart<'_> {
/// An iterator over all the [`StringLikePart`] of a string-like expression.
///
/// This is created by the [`StringLike::parts`] method.
#[derive(Clone)]
pub enum StringLikePartIter<'a> {
String(std::slice::Iter<'a, ast::StringLiteral>),
Bytes(std::slice::Iter<'a, ast::BytesLiteral>),
Expand Down Expand Up @@ -607,5 +619,25 @@ impl<'a> Iterator for StringLikePartIter<'a> {
}
}

impl DoubleEndedIterator for StringLikePartIter<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
let part = match self {
StringLikePartIter::String(inner) => StringLikePart::String(inner.next_back()?),
StringLikePartIter::Bytes(inner) => StringLikePart::Bytes(inner.next_back()?),
StringLikePartIter::FString(inner) => {
let part = inner.next_back()?;
match part {
ast::FStringPart::Literal(string_literal) => {
StringLikePart::String(string_literal)
}
ast::FStringPart::FString(f_string) => StringLikePart::FString(f_string),
}
}
};

Some(part)
}
}

impl FusedIterator for StringLikePartIter<'_> {}
impl ExactSizeIterator for StringLikePartIter<'_> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"preview": "enabled"
}
]
Loading

0 comments on commit 73ee72b

Please sign in to comment.