Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
1e70c99
baseline test cases
ntBre Nov 20, 2025
8cc8844
keep lambda parameters on a single line
ntBre Nov 20, 2025
1b58643
wip: parenthesize long lambda bodies
ntBre Nov 20, 2025
74093bd
add a poorly formatted case from the ecosystem report
ntBre Nov 20, 2025
a5e13cf
add an unstable test case from ecosystem report
ntBre Nov 20, 2025
62c968c
rough draft of ExprLambdaLayout::Assignment
ntBre Nov 20, 2025
d68a03a
add another bad case from the ecosystem check
ntBre Nov 21, 2025
07bcf41
fix binary expression in lambda in return
ntBre Dec 1, 2025
6b47664
add another bad test case from the ecosystem report
ntBre Dec 1, 2025
f634bb5
propagate lambda layout for annotated assignments
ntBre Dec 1, 2025
ad31477
another bad test for long bodies with their own parens
ntBre Dec 1, 2025
9db5d43
possibly bad test for triple-quoted f-strings
ntBre Dec 1, 2025
24e15bf
exclude call and subscript expressions from has_own_parentheses
ntBre Dec 2, 2025
0a41abe
avoid lambda special-casing in maybe_parenthesize_expression
ntBre Dec 2, 2025
e9f9507
add some assignment tests with parentheses and comments
ntBre Dec 2, 2025
89dc1ad
add a couple more test cases
ntBre Dec 2, 2025
18a3d59
use write!
ntBre Dec 2, 2025
972129c
create id only in indented case, update group name
ntBre Dec 2, 2025
2e95002
avoid nesting groups
ntBre Dec 2, 2025
9785066
add too-eagerly parenthesized case from ecosystem
ntBre Dec 2, 2025
efa372b
apply Micha's patch, fixing everything?
ntBre Dec 3, 2025
6f6c09c
fix snapshot changes for cases with comments
ntBre Dec 3, 2025
9ef9d03
fix another ecosystem call expansion
ntBre Dec 3, 2025
a3400a0
use parenthesize_if_expands for fluent call chains
ntBre Dec 3, 2025
258b1fd
add wrapping case from the ecosystem check
ntBre Dec 3, 2025
04963a6
expand parent if the lambda body breaks
ntBre Dec 3, 2025
7dddcc8
remove comment
ntBre Dec 3, 2025
afb01ce
combine preview checks
ntBre Dec 4, 2025
2e84402
add comments and some supporting tests
ntBre Dec 4, 2025
3a20c6f
copy mapper test case from can_omit_optional_parentheses
ntBre Dec 4, 2025
bdd5ba5
gate optional_parentheses branches behind stable
ntBre Dec 4, 2025
1a3e385
Merge branch 'main' into brent/indent-lambda-params
ntBre Dec 4, 2025
08b1da3
mirror comment handling from `maybe_parenthesize_expression`
ntBre Dec 4, 2025
dfd3460
add some more tests
ntBre Dec 4, 2025
219bbd1
check comment case first
ntBre Dec 5, 2025
a4b4a82
add another dangling eol case
ntBre Dec 5, 2025
43b53ed
improve dangling header comment placement
ntBre Dec 5, 2025
dc240a1
clippy
ntBre Dec 5, 2025
5605387
add another dangling case between lambda and parameters
ntBre Dec 5, 2025
25d70b4
more tests
ntBre Dec 5, 2025
c766642
block indent and trailing comments
ntBre Dec 5, 2025
fe255d1
leading_comments in block
ntBre Dec 5, 2025
8cbe03b
add more cases without parameters
ntBre Dec 5, 2025
c62390d
try parenthesizing the body without parameters
ntBre Dec 5, 2025
4585a0b
Revert "try parenthesizing the body without parameters"
ntBre Dec 5, 2025
3c481ba
factor out preview variable
ntBre Dec 5, 2025
4d76cd7
factor out parameters_have_comments
ntBre Dec 5, 2025
80852c1
pairing
ntBre Dec 5, 2025
86406c0
wip
ntBre Dec 5, 2025
1531c94
revert the last two commits, back to a stable formatting
ntBre Dec 5, 2025
0710e0b
fix assignment instability with dangling comments
ntBre Dec 5, 2025
df42aa2
Reapply "try parenthesizing the body without parameters"
ntBre Dec 5, 2025
f20f3e0
fix assignment instability without parameters too
ntBre Dec 5, 2025
8ede14a
move comments within lambda parameters to dangling lambda comments
ntBre Dec 8, 2025
e8540d9
format new dangling comments
ntBre Dec 8, 2025
c7b1089
apply patch
ntBre Dec 9, 2025
ea30464
clippy
ntBre Dec 9, 2025
21b442a
accept snapshots
ntBre Dec 9, 2025
711dd6e
pass preview to handle_lambda_comment
ntBre Dec 9, 2025
4ffbd49
Merge branch 'main' into brent/indent-lambda-params
ntBre Dec 9, 2025
90f43bd
add broken test cases
ntBre Dec 9, 2025
b0a8298
avoid breaking when the first parameter has leading comments
ntBre Dec 9, 2025
65c9435
Merge branch 'brent/fix-kwargs' into brent/indent-lambda-params
ntBre Dec 9, 2025
fd34cd6
update snaps
ntBre Dec 9, 2025
b823866
revert preview and comment placement changes
ntBre Dec 9, 2025
2bd6409
revert FormatParameters changes
ntBre Dec 9, 2025
d722155
make comments leading on parameter_s_
ntBre Dec 9, 2025
f639389
simplify check with parameters.start() == parameter.start()
ntBre Dec 9, 2025
9d683da
simplify a bit further
ntBre Dec 9, 2025
2da4798
update comments
ntBre Dec 9, 2025
acc49ac
remove redundant parameters check
ntBre Dec 9, 2025
2e0ee2e
update lambda comment
ntBre Dec 9, 2025
829f105
Merge branch 'brent/fix-kwargs' into brent/indent-lambda-params
ntBre Dec 9, 2025
54c7689
clean up deref
ntBre Dec 9, 2025
05ff8f5
Merge branch 'main' into brent/indent-lambda-params
ntBre Dec 9, 2025
bb053f8
fix lambda formatting in assignments
ntBre Dec 10, 2025
6e9e42d
factor out maybe_parenthesize_lambda
ntBre Dec 10, 2025
33fcca9
re-apply 'pass preview to handle_lambda_comment'
ntBre Dec 10, 2025
9f9b76b
move preview comment handling mostly into placement.rs
ntBre Dec 10, 2025
a8bebaa
add preview function taking a PreviewMode
ntBre Dec 10, 2025
f999da0
move tokenizer checks into the stable branch
ntBre Dec 10, 2025
7c94e10
update lambda placement docs
ntBre Dec 10, 2025
8022343
maybe_parenthesize_lambda -> maybe_parenthesize_value
ntBre Dec 10, 2025
b96cf96
add a few more call tests with comments
ntBre Dec 10, 2025
553b45e
move dangling comment handling back out of placement.rs
ntBre Dec 10, 2025
9d7d94c
factor out FormatBody
ntBre Dec 10, 2025
2cb98d4
working for Micha's exact example
ntBre Dec 11, 2025
8711b1a
Revert leading comment changes
ntBre Dec 11, 2025
0f1ea90
reposition dangling comments
ntBre Dec 11, 2025
5fee753
save f.context.comments() to comments
ntBre Dec 11, 2025
d1b79f5
share common code
ntBre Dec 11, 2025
854062f
inline format_body
ntBre Dec 11, 2025
6f25547
delete outdated preview comments
ntBre Dec 11, 2025
0074515
consolidate preview checks
ntBre Dec 11, 2025
3dfda9f
unwrap format_body
ntBre Dec 11, 2025
a48dc8e
inline current FormatBody
ntBre Dec 11, 2025
b80fdfa
move all body formatting to FormatBody
ntBre Dec 11, 2025
4a66edf
inline parameters_have_comments
ntBre Dec 12, 2025
93a958a
share dangling comment logic
ntBre Dec 12, 2025
17a1065
consolidate layout and preview checks
ntBre Dec 12, 2025
d24895d
comment on leading_body_comments
ntBre Dec 12, 2025
3011190
improve dangling name and docs
ntBre Dec 12, 2025
95301b3
improve after_parameters_end_of_line name and docs
ntBre Dec 12, 2025
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

Large diffs are not rendered by default.

37 changes: 22 additions & 15 deletions crates/ruff_python_formatter/src/builders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ruff_formatter::{Argument, Arguments, write};
use ruff_formatter::{Argument, Arguments, format_args, write};
use ruff_text_size::{Ranged, TextRange, TextSize};

use crate::context::{NodeLevel, WithNodeLevel};
Expand Down Expand Up @@ -33,20 +33,27 @@ impl<'ast> Format<PyFormatContext<'ast>> for ParenthesizeIfExpands<'_, 'ast> {
{
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);

write!(
f,
[group(&format_with(|f| {
if_group_breaks(&token("(")).fmt(f)?;

if self.indent {
soft_block_indent(&Arguments::from(&self.inner)).fmt(f)?;
} else {
Arguments::from(&self.inner).fmt(f)?;
}

if_group_breaks(&token(")")).fmt(f)
}))]
)
if self.indent {
let parens_id = f.group_id("indented_parenthesize_if_expands");
group(&format_args![
if_group_breaks(&token("(")),
indent_if_group_breaks(
&format_args![soft_line_break(), &Arguments::from(&self.inner)],
parens_id
),
soft_line_break(),
if_group_breaks(&token(")"))
])
.with_id(Some(parens_id))
.fmt(&mut f)
} else {
group(&format_args![
if_group_breaks(&token("(")),
Arguments::from(&self.inner),
if_group_breaks(&token(")")),
])
.fmt(&mut f)
}
}
}
}
Expand Down
Loading