Skip to content

Commit 68e09d5

Browse files
committed
preview-gate both changes
1 parent 958f049 commit 68e09d5

4 files changed

Lines changed: 290 additions & 94 deletions

File tree

crates/ruff_python_formatter/src/context.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ impl<'a> PyFormatContext<'a> {
112112
pub(crate) const fn is_preview(&self) -> bool {
113113
self.options.preview().is_enabled()
114114
}
115-
116-
pub(crate) const fn is_stable(&self) -> bool {
117-
!self.is_preview()
118-
}
119115
}
120116

121117
impl FormatContext for PyFormatContext<'_> {

crates/ruff_python_formatter/src/expression/expr_lambda.rs

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Pare
88
use crate::expression::{has_own_parentheses, maybe_parenthesize_expression};
99
use crate::other::parameters::ParametersParentheses;
1010
use crate::prelude::*;
11+
use crate::preview::is_indent_lambda_parameters_enabled;
12+
use crate::preview::is_parenthesize_lambda_bodies_enabled;
1113

1214
#[derive(Default)]
1315
pub struct FormatExprLambda;
@@ -32,44 +34,67 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
3234
let (dangling_before_parameters, dangling_after_parameters) = dangling
3335
.split_at(dangling.partition_point(|comment| comment.end() < parameters.start()));
3436

35-
if dangling_before_parameters.is_empty() {
36-
write!(f, [space()])?;
37-
}
37+
if is_indent_lambda_parameters_enabled(f.context()) {
38+
if dangling_before_parameters.is_empty() {
39+
write!(f, [space()])?;
40+
}
3841

39-
group(&format_with(|f: &mut PyFormatter| {
40-
if f.context().node_level().is_parenthesized()
41-
&& (parameters.len() > 1 || !dangling_before_parameters.is_empty())
42-
{
43-
let end_of_line_start = dangling_before_parameters
44-
.partition_point(|comment| comment.line_position().is_end_of_line());
45-
let (same_line_comments, own_line_comments) =
46-
dangling_before_parameters.split_at(end_of_line_start);
42+
group(&format_with(|f: &mut PyFormatter| {
43+
if f.context().node_level().is_parenthesized()
44+
&& (parameters.len() > 1 || !dangling_before_parameters.is_empty())
45+
{
46+
let end_of_line_start = dangling_before_parameters
47+
.partition_point(|comment| comment.line_position().is_end_of_line());
48+
let (same_line_comments, own_line_comments) =
49+
dangling_before_parameters.split_at(end_of_line_start);
4750

48-
dangling_comments(same_line_comments).fmt(f)?;
51+
dangling_comments(same_line_comments).fmt(f)?;
4952

50-
soft_block_indent(&format_args![
51-
leading_comments(own_line_comments),
53+
soft_block_indent(&format_args![
54+
leading_comments(own_line_comments),
55+
parameters
56+
.format()
57+
.with_options(ParametersParentheses::Never),
58+
])
59+
.fmt(f)
60+
} else {
5261
parameters
5362
.format()
54-
.with_options(ParametersParentheses::Never),
55-
])
56-
.fmt(f)
63+
.with_options(ParametersParentheses::Never)
64+
.fmt(f)
65+
}?;
66+
67+
token(":").fmt(f)?;
68+
69+
if dangling_after_parameters.is_empty() {
70+
space().fmt(f)
71+
} else {
72+
dangling_comments(dangling_after_parameters).fmt(f)
73+
}
74+
}))
75+
.fmt(f)?;
76+
} else {
77+
if dangling_before_parameters.is_empty() {
78+
write!(f, [space()])?;
5779
} else {
58-
parameters
80+
write!(f, [dangling_comments(dangling_before_parameters)])?;
81+
}
82+
83+
write!(
84+
f,
85+
[parameters
5986
.format()
60-
.with_options(ParametersParentheses::Never)
61-
.fmt(f)
62-
}?;
87+
.with_options(ParametersParentheses::Never)]
88+
)?;
6389

64-
token(":").fmt(f)?;
90+
write!(f, [token(":")])?;
6591

6692
if dangling_after_parameters.is_empty() {
67-
space().fmt(f)
93+
write!(f, [space()])?;
6894
} else {
69-
dangling_comments(dangling_after_parameters).fmt(f)
95+
write!(f, [dangling_comments(dangling_after_parameters)])?;
7096
}
71-
}))
72-
.fmt(f)?;
97+
}
7398
} else {
7499
write!(f, [token(":")])?;
75100

@@ -82,11 +107,13 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
82107
}
83108

84109
// Avoid parenthesizing lists, dictionaries, etc.
85-
if f.context().is_stable() || has_own_parentheses(body, f.context()).is_some() {
86-
body.format().fmt(f)
87-
} else {
110+
if is_parenthesize_lambda_bodies_enabled(f.context())
111+
&& has_own_parentheses(body, f.context()).is_none()
112+
{
88113
maybe_parenthesize_expression(body, item, Parenthesize::IfBreaksParenthesizedNested)
89114
.fmt(f)
115+
} else {
116+
body.format().fmt(f)
90117
}
91118
}
92119
}

crates/ruff_python_formatter/src/preview.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,16 @@ pub(crate) const fn is_avoid_parens_for_long_as_captures_enabled(
5252
) -> bool {
5353
context.is_preview()
5454
}
55+
56+
/// Returns `true` if the [`indent_lambda_parameters`](https://github.com/astral-sh/ruff/pull/21385)
57+
/// preview style is enabled.
58+
pub(crate) const fn is_indent_lambda_parameters_enabled(context: &PyFormatContext) -> bool {
59+
context.is_preview()
60+
}
61+
62+
/// Returns `true` if the
63+
/// [`parenthesize_lambda_bodies`](https://github.com/astral-sh/ruff/pull/21385) preview style is
64+
/// enabled.
65+
pub(crate) const fn is_parenthesize_lambda_bodies_enabled(context: &PyFormatContext) -> bool {
66+
context.is_preview()
67+
}

0 commit comments

Comments
 (0)