Skip to content

Commit

Permalink
Indent lambda parameters if parameters wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Nov 3, 2023
1 parent 9f30ccc commit 01c6438
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ def f(
y:
z
)

lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
12 changes: 0 additions & 12 deletions crates/ruff_python_formatter/src/comments/placement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,18 +1805,6 @@ fn handle_lambda_comment<'a>(
locator: &Locator,
) -> CommentPlacement<'a> {
if let Some(parameters) = lambda.parameters.as_deref() {
// Comments between the `lambda` and the parameters are dangling on the lambda:
// ```python
// (
// lambda # comment
// x:
// y
// )
// ```
if comment.start() < parameters.start() {
return CommentPlacement::dangling(comment.enclosing_node(), comment);
}

// Comments between the parameters and the body are dangling on the lambda:
// ```python
// (
Expand Down
48 changes: 33 additions & 15 deletions crates/ruff_python_formatter/src/expression/expr_lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprLambda;
use ruff_text_size::Ranged;

use crate::builders::parenthesize_if_expands;
use crate::comments::{dangling_comments, SourceComment};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::expression::has_own_parentheses;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
use crate::other::parameters::ParametersParentheses;
use crate::prelude::*;

Expand All @@ -25,7 +27,7 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
write!(f, [token("lambda")])?;

if let Some(parameters) = parameters {
// In this context, a dangling comment can either be a comment between the `lambda` the
// In this context, a dangling comment can either be a comment between the `lambda` and the
// parameters, or a comment between the parameters and the body.
let (dangling_before_parameters, dangling_after_parameters) = dangling
.split_at(dangling.partition_point(|comment| comment.end() < parameters.start()));
Expand All @@ -36,20 +38,30 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
write!(f, [dangling_comments(dangling_before_parameters)])?;
}

write!(
f,
[parameters
.format()
.with_options(ParametersParentheses::Never)]
)?;
group(&format_with(|f: &mut PyFormatter| {
if f.context().node_level().is_parenthesized() {
soft_block_indent(
&parameters
.format()
.with_options(ParametersParentheses::Never),
)
.fmt(f)
} else {
parameters
.format()
.with_options(ParametersParentheses::Never)
.fmt(f)
}?;

write!(f, [token(":")])?;
token(":").fmt(f)?;

if dangling_after_parameters.is_empty() {
write!(f, [space()])?;
} else {
write!(f, [dangling_comments(dangling_after_parameters)])?;
}
if dangling_after_parameters.is_empty() {
space().fmt(f)
} else {
dangling_comments(dangling_after_parameters).fmt(f)
}
}))
.fmt(f)?;
} else {
write!(f, [token(":")])?;

Expand All @@ -61,7 +73,13 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
}
}

write!(f, [body.format()])
if has_own_parentheses(body, f.context()).is_some()
|| body.needs_parentheses(item.into(), f.context()) == OptionalParentheses::Always
{
body.format().fmt(f)
} else {
parenthesize_if_expands(&body.format().with_options(Parentheses::Never)).fmt(f)
}
}

fn fmt_dangling_comments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl NeedsParentheses for ExprNamedExpr {
|| parent.is_stmt_delete()
|| parent.is_stmt_for()
|| parent.is_stmt_function_def()
|| parent.is_expr_lambda()
{
OptionalParentheses::Always
} else {
Expand Down
9 changes: 1 addition & 8 deletions crates/ruff_python_formatter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,7 @@ if True:
#[test]
fn quick_test() {
let source = r#"
def main() -> None:
if True:
some_very_long_variable_name_abcdefghijk = Foo()
some_very_long_variable_name_abcdefghijk = some_very_long_variable_name_abcdefghijk[
some_very_long_variable_name_abcdefghijk.some_very_long_attribute_name
== "This is a very long string abcdefghijk"
]
lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
"#;
let source_type = PySourceType::Python;
let (tokens, comment_ranges) = tokens_and_ranges(source, source_type).unwrap();
Expand Down
17 changes: 13 additions & 4 deletions crates/ruff_python_formatter/src/other/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,15 @@ impl FormatNodeRule<Parameters> for FormatParameters {
dangling.split_at(parenthesis_comments_end);

let format_inner = format_with(|f: &mut PyFormatter| {
let separator = format_with(|f| write!(f, [token(","), soft_line_break_or_space()]));
let separator = format_with(|f: &mut PyFormatter| {
token(",").fmt(f)?;

if f.context().node_level().is_parenthesized() {
soft_line_break_or_space().fmt(f)
} else {
space().fmt(f)
}
});
let mut joiner = f.join_with(separator);
let mut last_node: Option<AnyNodeRef> = None;

Expand Down Expand Up @@ -232,20 +240,21 @@ impl FormatNodeRule<Parameters> for FormatParameters {
Ok(())
});

let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);

let num_parameters = posonlyargs.len()
+ args.len()
+ usize::from(vararg.is_some())
+ kwonlyargs.len()
+ usize::from(kwarg.is_some());

if self.parentheses == ParametersParentheses::Never {
write!(f, [group(&format_inner), dangling_comments(dangling)])
write!(f, [format_inner, dangling_comments(dangling)])
} else if num_parameters == 0 {
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);
// No parameters, format any dangling comments between `()`
write!(f, [empty_parenthesized("(", dangling, ")")])
} else {
let mut f = WithNodeLevel::new(NodeLevel::ParenthesizedExpression, f);

// Intentionally avoid `parenthesized`, which groups the entire formatted contents.
// We want parameters to be grouped alongside return types, one level up, so we
// format them "inline" here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ lambda: ( # comment
y:
z
)
lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d
```

## Output
Expand Down Expand Up @@ -242,30 +244,10 @@ lambda x: lambda y: lambda z: (x, y, z) # Trailing
# Trailing
# Leading
lambda x: lambda y: lambda z: (
x,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
y,
z,
lambda x: (
lambda y: (
lambda z: (x, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, z)
)
) # Trailing
# Trailing
Expand All @@ -275,8 +257,10 @@ a = (
)
a = (
lambda x, # Dangling
y: 1
lambda
x, # Dangling
y
: 1
)
# Regression test: lambda empty arguments ranges were too long, leading to unstable
Expand All @@ -289,7 +273,9 @@ a = (
# lambda arguments don't have parentheses, so we never add a magic trailing comma ...
def f(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = lambda x: y,
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = lambda
x
: y,
):
pass
Expand Down Expand Up @@ -336,36 +322,42 @@ lambda a, /, c: a
)
(
lambda
# comment
*x: x
lambda
# comment
*x
: x
)
(
lambda
# comment 1
# comment 2
*x:
lambda
# comment 1
# comment 2
*x
:
# comment 3
x
)
(
lambda # comment 1
# comment 2
*x: # comment 3
lambda
# comment 1
# comment 2
*x
: # comment 3
x
)
lambda *x: x
(
lambda
# comment
*x: x
lambda
# comment
*x
: x
)
lambda: ( # comment
lambda: (
# comment
x
)
Expand Down Expand Up @@ -393,26 +385,35 @@ lambda: ( # comment
(
lambda: # comment
( # comment
(
# comment
x
)
)
(
lambda # 1
# 2
x: # 3
lambda
# 1
# 2
x
: # 3
# 4
# 5
# 6
x
)
(
lambda x,
# comment
y: z
lambda
x,
# comment
y
: z
)
lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
*args, **kwargs
), e=1, f=2, g=2: d
```


Expand Down

0 comments on commit 01c6438

Please sign in to comment.