mirror of https://github.com/astral-sh/ruff
keep lambda parameters on a single line
This commit is contained in:
parent
1e70c991a2
commit
8cc884428d
|
|
@ -1,4 +1,4 @@
|
|||
use ruff_formatter::write;
|
||||
use ruff_formatter::{RemoveSoftLinesBuffer, write};
|
||||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::ExprLambda;
|
||||
use ruff_text_size::Ranged;
|
||||
|
|
@ -7,6 +7,7 @@ use crate::comments::dangling_comments;
|
|||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::other::parameters::ParametersParentheses;
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_force_single_line_lambda_parameters_enabled;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprLambda;
|
||||
|
|
@ -37,12 +38,25 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
|
|||
write!(f, [dangling_comments(dangling_before_parameters)])?;
|
||||
}
|
||||
|
||||
// Try to keep the parameters on a single line, unless there are intervening comments.
|
||||
if is_force_single_line_lambda_parameters_enabled(f.context())
|
||||
&& !comments.contains_comments(parameters.as_ref().into())
|
||||
{
|
||||
let mut buffer = RemoveSoftLinesBuffer::new(f);
|
||||
write!(
|
||||
buffer,
|
||||
[parameters
|
||||
.format()
|
||||
.with_options(ParametersParentheses::Never)]
|
||||
)?;
|
||||
} else {
|
||||
write!(
|
||||
f,
|
||||
[parameters
|
||||
.format()
|
||||
.with_options(ParametersParentheses::Never)]
|
||||
)?;
|
||||
}
|
||||
|
||||
write!(f, [token(":")])?;
|
||||
|
||||
|
|
|
|||
|
|
@ -52,3 +52,12 @@ pub(crate) const fn is_avoid_parens_for_long_as_captures_enabled(
|
|||
) -> bool {
|
||||
context.is_preview()
|
||||
}
|
||||
|
||||
/// Returns `true` if the
|
||||
/// [`force_single_line_lambda_parameters`](https://github.com/astral-sh/ruff/pull/21385) preview
|
||||
/// style is enabled.
|
||||
pub(crate) const fn is_force_single_line_lambda_parameters_enabled(
|
||||
context: &PyFormatContext,
|
||||
) -> bool {
|
||||
context.is_preview()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -676,3 +676,65 @@ class C:
|
|||
else storage.Bucket(mock_service, destination_bucket_name)
|
||||
)
|
||||
```
|
||||
|
||||
|
||||
## Preview changes
|
||||
```diff
|
||||
--- Stable
|
||||
+++ Preview
|
||||
@@ -280,9 +280,7 @@
|
||||
] # Trailing
|
||||
# Trailing
|
||||
|
||||
-lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||
- *args, **kwargs
|
||||
-), e=1, f=2, g=2: d
|
||||
+lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/8179
|
||||
@@ -291,9 +289,9 @@
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
- f=lambda self,
|
||||
- *args,
|
||||
- **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs),
|
||||
+ f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||
+ *args, **kwargs
|
||||
+ ),
|
||||
)
|
||||
|
||||
|
||||
@@ -302,15 +300,7 @@
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
- f=lambda self,
|
||||
- araa,
|
||||
- kkkwargs,
|
||||
- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
|
||||
- args,
|
||||
- kwargs,
|
||||
- e=1,
|
||||
- f=2,
|
||||
- g=2: d,
|
||||
+ f=lambda self, araa, kkkwargs, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, args, kwargs, e=1, f=2, g=2: d,
|
||||
g=10,
|
||||
)
|
||||
|
||||
@@ -320,9 +310,10 @@
|
||||
c,
|
||||
d,
|
||||
e,
|
||||
- f=lambda self,
|
||||
- *args,
|
||||
- **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs) + 1,
|
||||
+ f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
|
||||
+ *args, **kwargs
|
||||
+ )
|
||||
+ + 1,
|
||||
)
|
||||
|
||||
|
||||
```
|
||||
|
|
|
|||
Loading…
Reference in New Issue