-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Improve comprehension line break beheavior #5680
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,25 @@ use crate::prelude::*; | |
use crate::AsFormat; | ||
use crate::{FormatNodeRule, PyFormatter}; | ||
use ruff_formatter::{format_args, write, Buffer, FormatResult}; | ||
use rustpython_parser::ast::{Comprehension, Ranged}; | ||
use rustpython_parser::ast::{Comprehension, Expr, Ranged}; | ||
|
||
#[derive(Default)] | ||
pub struct FormatComprehension; | ||
|
||
impl FormatNodeRule<Comprehension> for FormatComprehension { | ||
fn fmt_fields(&self, item: &Comprehension, f: &mut PyFormatter) -> FormatResult<()> { | ||
struct Spacer<'a>(&'a Expr); | ||
|
||
impl Format<PyFormatContext<'_>> for Spacer<'_> { | ||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> { | ||
if f.context().comments().has_leading_comments(self.0) { | ||
soft_line_break_or_space().fmt(f) | ||
} else { | ||
space().fmt(f) | ||
} | ||
} | ||
} | ||
|
||
let Comprehension { | ||
range: _, | ||
target, | ||
|
@@ -18,33 +30,40 @@ impl FormatNodeRule<Comprehension> for FormatComprehension { | |
is_async, | ||
} = item; | ||
|
||
let comments = f.context().comments().clone(); | ||
|
||
if *is_async { | ||
write!(f, [text("async"), space()])?; | ||
} | ||
|
||
let comments = f.context().comments().clone(); | ||
let dangling_item_comments = comments.dangling_comments(item); | ||
|
||
let (before_target_comments, before_in_comments) = dangling_item_comments.split_at( | ||
dangling_item_comments | ||
.partition_point(|comment| comment.slice().end() < target.range().start()), | ||
); | ||
|
||
let trailing_in_comments = comments.dangling_comments(iter); | ||
|
||
let in_spacer = format_with(|f| { | ||
if before_in_comments.is_empty() { | ||
space().fmt(f) | ||
} else { | ||
soft_line_break_or_space().fmt(f) | ||
} | ||
}); | ||
|
||
write!( | ||
f, | ||
[ | ||
text("for"), | ||
trailing_comments(before_target_comments), | ||
group(&format_args!( | ||
soft_line_break_or_space(), | ||
Spacer(target), | ||
target.format(), | ||
soft_line_break_or_space(), | ||
in_spacer, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would the macros rules allow inlining the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so because |
||
leading_comments(before_in_comments), | ||
text("in"), | ||
trailing_comments(trailing_in_comments), | ||
soft_line_break_or_space(), | ||
Spacer(iter), | ||
iter.format(), | ||
)), | ||
] | ||
|
@@ -64,7 +83,7 @@ impl FormatNodeRule<Comprehension> for FormatComprehension { | |
leading_comments(own_line_if_comments), | ||
text("if"), | ||
trailing_comments(end_of_line_if_comments), | ||
soft_line_break_or_space(), | ||
Spacer(if_case), | ||
if_case.format(), | ||
))); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ def make_arange(n): | |
```diff | ||
--- Black | ||
+++ Ruff | ||
@@ -2,29 +2,27 @@ | ||
@@ -2,14 +2,11 @@ | ||
|
||
|
||
def f(): | ||
|
@@ -59,17 +59,7 @@ def make_arange(n): | |
|
||
|
||
async def func(): | ||
if test: | ||
out_batched = [ | ||
i | ||
- async for i in aitertools._async_map( | ||
- self.async_inc, arange(8), batch_size=3 | ||
- ) | ||
+ async for | ||
+ i | ||
+ in | ||
+ aitertools._async_map(self.async_inc, arange(8), batch_size=3) | ||
] | ||
@@ -23,8 +20,8 @@ | ||
|
||
|
||
def awaited_generator_value(n): | ||
|
@@ -100,10 +90,9 @@ async def func(): | |
if test: | ||
out_batched = [ | ||
i | ||
async for | ||
i | ||
in | ||
aitertools._async_map(self.async_inc, arange(8), batch_size=3) | ||
async for i in aitertools._async_map( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that looks much nicer |
||
self.async_inc, arange(8), batch_size=3 | ||
) | ||
] | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No change other than that this gets all wrapped in an
in_parentheses_only_group