Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Join implicit concatenated strings when they fit on a line #13663

Merged
merged 27 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
42d1b6d
All tests are passing
MichaReiser Oct 7, 2024
a688cbe
Fix assert formatting instability
MichaReiser Oct 16, 2024
c2ab97a
More preview gating
MichaReiser Oct 16, 2024
49b5d42
Add some docstrings
MichaReiser Oct 16, 2024
d7b0db7
In progress, tests for assignments
MichaReiser Oct 17, 2024
9ff7d73
Fix instability when implicit string is in the right hand side of a n…
MichaReiser Oct 17, 2024
3b84fdd
Add tests and fix assignment formatting
MichaReiser Oct 18, 2024
0d4750a
Review `maybe_parenthesize_expression` usages and add tests
MichaReiser Oct 18, 2024
072cbb8
Add tests for can_omit
MichaReiser Oct 18, 2024
56c30d4
Add tests for leading/trailing comments
MichaReiser Oct 18, 2024
52fdb83
Initial f-string formatting support
MichaReiser Oct 18, 2024
c216660
Handle comments and debug text in fstrings
MichaReiser Oct 19, 2024
dc6be21
Quote style preserve
MichaReiser Oct 20, 2024
68a360d
Fix instability when f-string formatting was disabled
MichaReiser Oct 20, 2024
c4bf7f6
Correctly handle implicit concatenated strings in docstrings positions
MichaReiser Oct 21, 2024
17642b4
Handle f-strings with quoted debug expressions and triple quoted lite…
MichaReiser Oct 22, 2024
4096009
Clippy
MichaReiser Oct 22, 2024
f3256b8
Fix bug with implicit concatenated string preceded by comment
MichaReiser Oct 23, 2024
e925583
Fix instability when switching between `BestFit` and `IfBreaksParenth…
MichaReiser Oct 23, 2024
ed5a9df
Fix chaperon space handling in docstrings
MichaReiser Oct 23, 2024
6cbcc30
Discard changes to crates/ruff_python_formatter/tests/snapshots/black…
MichaReiser Oct 23, 2024
ac84cfd
Discard changes to crates/ruff_python_formatter/tests/snapshots/black…
MichaReiser Oct 23, 2024
7e32fa7
Improve StmtAssign formattign doc, don't format f-strings if a debug …
MichaReiser Oct 23, 2024
d227d07
Gate `line_suffix_boundary` change in implicit formatting
MichaReiser Oct 23, 2024
c01a330
Fix implicit concatenated string with comments in docstring position
MichaReiser Oct 23, 2024
b67d3c6
Fix dropping comments when f-string contains magic trailing comma
MichaReiser Oct 24, 2024
f14f2ba
Fix docstring stable style regresssion
MichaReiser Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading