Skip to content

Commit

Permalink
Handle lambda Arguments dangling comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cnpryer committed Jul 19, 2023
1 parent f66df7c commit dcb5cea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
18 changes: 16 additions & 2 deletions crates/ruff_python_formatter/src/expression/expr_lambda.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::comments::dangling_comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::other::arguments::ArgumentsParentheses;
use crate::AsFormat;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{space, text};
use ruff_formatter::{write, Buffer, FormatResult};
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use rustpython_parser::ast::ExprLambda;

#[derive(Default)]
Expand All @@ -19,7 +20,11 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
body,
} = item;

let comments = f.context().comments().clone();
let dangling = comments.dangling_comments(args.as_any_node_ref());

This comment has been minimized.

Copy link
@cnpryer

cnpryer Jul 19, 2023

Author Contributor

Let me add some comments here to explain what's going on.


write!(f, [text("lambda")])?;

if !args.args.is_empty() {
write!(
f,
Expand All @@ -30,7 +35,16 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
]
)?;
}
write!(f, [text(":"), space(), body.format()])

write!(
f,
[
text(":"),
space(),
body.format(),
dangling_comments(dangling)
]
)
}
}

Expand Down
4 changes: 0 additions & 4 deletions crates/ruff_python_formatter/src/other/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,6 @@ impl FormatNodeRule<Arguments> for FormatArguments {
+ usize::from(kwarg.is_some());

if self.parentheses == ArgumentsParentheses::SkipInsideLambda {
debug_assert!(
dangling.is_empty(),
"This node's parentheses cannot be skipped with dangling comments"
);
group(&format_inner).fmt(f)?;
} else if num_arguments == 0 {
// No arguments, format any dangling comments between `()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ lambda x: lambda y: lambda z: (
# Trailing
a = lambda: 1 # Dangling
a = (
lambda: 1 # Dangling
)
```


Expand Down

0 comments on commit dcb5cea

Please sign in to comment.