@@ -284,13 +284,6 @@ enum BinaryLeftOrRightSide<'a, 'b> {
284284 parent : BinaryLikeExpression < ' a , ' b > ,
285285 /// Is the parent the condition of a `if` / `while` / `do-while` / `for` statement?
286286 inside_condition : bool ,
287-
288- /// Indicates if the comments of the parent should be printed or not.
289- /// Must be true if `parent` isn't the root `BinaryLikeExpression` for which `format` is called.
290- print_parent_comments : bool ,
291-
292- /// Indicates if the parent has the same kind as the current binary expression.
293- parent_has_same_kind : bool ,
294287 } ,
295288}
296289
@@ -301,8 +294,6 @@ impl<'a> Format<'a> for BinaryLeftOrRightSide<'a, '_> {
301294 Self :: Right {
302295 parent : binary_like_expression,
303296 inside_condition : inside_parenthesis,
304- print_parent_comments,
305- parent_has_same_kind,
306297 } => {
307298 // // It's only possible to suppress the formatting of the whole binary expression formatting OR
308299 // // the formatting of the right hand side value but not of a nested binary expression.
@@ -319,52 +310,37 @@ impl<'a> Format<'a> for BinaryLeftOrRightSide<'a, '_> {
319310 write ! ( f, [ soft_line_break_or_space( ) ] ) ?;
320311 }
321312
322- write ! ( f, right) ?;
323-
324- Ok ( ( ) )
313+ write ! ( f, right)
325314 } ) ;
326315
316+ let parent = binary_like_expression. parent ( ) ;
317+
327318 // Doesn't match prettier that only distinguishes between logical and binary
328- let left_has_same_kind = is_same_binary_expression_kind (
329- binary_like_expression,
330- binary_like_expression. left ( ) ,
331- ) ;
332-
333- let right_has_same_kind =
334- is_same_binary_expression_kind ( binary_like_expression, right) ;
335-
336- // let should_break = f
337- // .context()
338- // .comments()
339- // .trailing_comments(binary_like_expression.left()?.syntax())
340- // .iter()
341- // .any(|comment| comment.kind().is_line());
342- let should_break = false ;
343-
344- let should_group = !( * parent_has_same_kind
345- || left_has_same_kind
346- || right_has_same_kind
347- || ( * inside_parenthesis
348- && matches ! (
319+ let should_group =
320+ !( is_same_binary_expression_kind ( binary_like_expression, parent)
321+ || is_same_binary_expression_kind (
349322 binary_like_expression,
350- BinaryLikeExpression :: LogicalExpression ( _)
351- ) ) ) ;
352-
353- // if *print_parent_comments {
354- // write!(f, binary_like_expression)?;
355- // }
323+ binary_like_expression. left ( ) . as_ast_nodes ( ) ,
324+ )
325+ || is_same_binary_expression_kind (
326+ binary_like_expression,
327+ right. as_ast_nodes ( ) ,
328+ )
329+ || ( * inside_parenthesis
330+ && matches ! (
331+ binary_like_expression,
332+ BinaryLikeExpression :: LogicalExpression ( _)
333+ ) ) ) ;
356334
357335 if should_group {
358- write ! ( f, [ group( & operator_and_right_expression) . should_expand( should_break) ] ) ?;
336+ let should_break = f. context ( ) . comments ( ) . has_trailing_line_comments (
337+ binary_like_expression. left ( ) . span ( ) . end ,
338+ right. span ( ) . start ,
339+ ) ;
340+ write ! ( f, [ group( & operator_and_right_expression) . should_expand( should_break) ] )
359341 } else {
360- write ! ( f, [ operator_and_right_expression] ) ? ;
342+ write ! ( f, [ operator_and_right_expression] )
361343 }
362-
363- // if *print_parent_comments {
364- // write!(f, [format_trailing_comments(binary_like_expression.syntax())])?;
365- // }
366-
367- Ok ( ( ) )
368344 }
369345 }
370346 }
@@ -403,7 +379,6 @@ fn split_into_left_and_right_sides<'a, 'b>(
403379 fn split_into_left_and_right_sides_inner < ' a , ' b > (
404380 binary : BinaryLikeExpression < ' a , ' b > ,
405381 inside_condition : bool ,
406- parent_has_same_kind : bool ,
407382 items : & mut Vec < BinaryLeftOrRightSide < ' a , ' b > > ,
408383 ) {
409384 let left = binary. left ( ) ;
@@ -416,28 +391,20 @@ fn split_into_left_and_right_sides<'a, 'b>(
416391 // SAFETY: `left` is guaranteed to be a valid binary like expression in `can_flatten()`.
417392 BinaryLikeExpression :: try_from ( left) . unwrap ( ) ,
418393 inside_condition,
419- is_same_binary_expression_kind ( & binary, left) ,
420394 items,
421395 ) ;
422396 } else {
423397 items. push ( BinaryLeftOrRightSide :: Left { parent : binary } ) ;
424398 }
425399
426- items. push ( BinaryLeftOrRightSide :: Right {
427- parent : binary,
428- inside_condition,
429- // TODO:
430- // print_parent_comments: expression.syntax() != root.syntax(),
431- print_parent_comments : false ,
432- parent_has_same_kind,
433- } ) ;
400+ items. push ( BinaryLeftOrRightSide :: Right { parent : binary, inside_condition } ) ;
434401 }
435402
436403 // Stores the left and right parts of the binary expression in sequence (rather than nested as they
437404 // appear in the tree).
438405 let mut items = Vec :: new ( ) ;
439406
440- split_into_left_and_right_sides_inner ( root, inside_condition, false , & mut items) ;
407+ split_into_left_and_right_sides_inner ( root, inside_condition, & mut items) ;
441408
442409 items
443410}
@@ -464,14 +431,14 @@ fn should_indent_if_parent_inlines(parent: &AstNodes<'_>) -> bool {
464431
465432fn is_same_binary_expression_kind (
466433 binary : & BinaryLikeExpression < ' _ , ' _ > ,
467- other : & Expression < ' _ > ,
434+ other : & AstNodes < ' _ > ,
468435) -> bool {
469436 match binary {
470437 BinaryLikeExpression :: LogicalExpression ( _) => {
471- matches ! ( other, Expression :: LogicalExpression ( _) )
438+ matches ! ( other, AstNodes :: LogicalExpression ( _) )
472439 }
473440 BinaryLikeExpression :: BinaryExpression ( _) => {
474- matches ! ( other, Expression :: BinaryExpression ( _) )
441+ matches ! ( other, AstNodes :: BinaryExpression ( _) )
475442 }
476443 }
477444}
0 commit comments