@@ -8,6 +8,8 @@ use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Pare
88use crate :: expression:: { has_own_parentheses, maybe_parenthesize_expression} ;
99use crate :: other:: parameters:: ParametersParentheses ;
1010use crate :: prelude:: * ;
11+ use crate :: preview:: is_indent_lambda_parameters_enabled;
12+ use crate :: preview:: is_parenthesize_lambda_bodies_enabled;
1113
1214#[ derive( Default ) ]
1315pub 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}
0 commit comments