@@ -334,7 +334,7 @@ impl<'a> Formatter<'a> {
334334 self . format_closure_expression ( * block_id, expr. span ) ;
335335 }
336336 Expr :: Subexpression ( block_id) => {
337- self . format_subexpression ( * block_id) ;
337+ self . format_subexpression ( * block_id, expr . span ) ;
338338 }
339339
340340 Expr :: List ( items) => self . format_list ( items) ,
@@ -705,15 +705,33 @@ impl<'a> Formatter<'a> {
705705 }
706706
707707 /// Format a subexpression
708- fn format_subexpression ( & mut self , block_id : nu_protocol:: BlockId ) {
708+ fn format_subexpression ( & mut self , block_id : nu_protocol:: BlockId , span : Span ) {
709709 let block = self . working_set . get_block ( block_id) ;
710+ // Check if the subexpression has explicit parentheses in the source
711+ let has_explicit_parens = self . source . get ( span. start ) == Some ( & b'(' )
712+ && self . source . get ( span. end - 1 ) == Some ( & b')' ) ;
713+ if !has_explicit_parens {
714+ self . format_block ( block) ;
715+ return ;
716+ }
710717 // Special case: subexpressions containing only a string interpolation don't need parentheses
711718 if block. pipelines . len ( ) == 1 && block. pipelines [ 0 ] . elements . len ( ) == 1 {
712719 if let Expr :: StringInterpolation ( _) = & block. pipelines [ 0 ] . elements [ 0 ] . expr . expr {
713720 self . format_block ( block) ;
714721 return ;
715722 }
716723 }
724+ // Special case: subexpressions containing a pipeline starting with $in don't need parentheses
725+ if block. pipelines . len ( ) == 1 && !block. pipelines [ 0 ] . elements . is_empty ( ) {
726+ let first_element = & block. pipelines [ 0 ] . elements [ 0 ] ;
727+ let element_text = String :: from_utf8_lossy (
728+ & self . source [ first_element. expr . span . start ..first_element. expr . span . end ] ,
729+ ) ;
730+ if element_text == "$in" {
731+ self . format_block ( block) ;
732+ return ;
733+ }
734+ }
717735
718736 self . write ( "(" ) ;
719737 let is_simple = block. pipelines . len ( ) == 1 && block. pipelines [ 0 ] . elements . len ( ) <= 3 ;
0 commit comments