@@ -253,29 +253,8 @@ fn format_expression_internal(
253253
254254 // If the context is for a prefix, we should always keep the parentheses, as they are always required
255255 if use_internal_expression && !keep_parentheses {
256- // Get the leading and trailing comments from contained span and append them onto the expression
257- let ( start_parens, end_parens) = contained. tokens ( ) ;
258- let leading_comments = start_parens
259- . leading_trivia ( )
260- . filter ( |token| trivia_util:: trivia_is_comment ( token) )
261- . flat_map ( |x| {
262- vec ! [
263- create_indent_trivia( ctx, shape) ,
264- x. to_owned( ) ,
265- create_newline_trivia( ctx) ,
266- ]
267- } )
268- // .chain(std::iter::once(create_indent_trivia(ctx, shape)))
269- . collect ( ) ;
270-
271- let trailing_comments = end_parens
272- . trailing_trivia ( )
273- . filter ( |token| trivia_util:: trivia_is_comment ( token) )
274- . flat_map ( |x| {
275- // Prepend a single space beforehand
276- vec ! [ Token :: new( TokenType :: spaces( 1 ) ) , x. to_owned( ) ]
277- } )
278- . collect ( ) ;
256+ let ( leading_comments, trailing_comments) =
257+ contained_span_comments ( ctx, contained, shape) ;
279258
280259 format_expression ( ctx, expression, shape)
281260 . update_leading_trivia ( FormatTriviaType :: Append ( leading_comments) )
@@ -358,6 +337,37 @@ fn format_expression_internal(
358337 }
359338}
360339
340+ fn contained_span_comments (
341+ ctx : & Context ,
342+ contained_span : & ContainedSpan ,
343+ shape : Shape ,
344+ ) -> ( Vec < Token > , Vec < Token > ) {
345+ // Get the leading and trailing comments from contained span and append them onto the expression
346+ let ( start_parens, end_parens) = contained_span. tokens ( ) ;
347+ let leading_comments = start_parens
348+ . leading_trivia ( )
349+ . filter ( |token| trivia_util:: trivia_is_comment ( token) )
350+ . flat_map ( |x| {
351+ vec ! [
352+ create_indent_trivia( ctx, shape) ,
353+ x. to_owned( ) ,
354+ create_newline_trivia( ctx) ,
355+ ]
356+ } )
357+ // .chain(std::iter::once(create_indent_trivia(ctx, shape)))
358+ . collect ( ) ;
359+
360+ let trailing_comments = end_parens
361+ . trailing_trivia ( )
362+ . filter ( |token| trivia_util:: trivia_is_comment ( token) )
363+ . flat_map ( |x| {
364+ // Prepend a single space beforehand
365+ vec ! [ Token :: new( TokenType :: spaces( 1 ) ) , x. to_owned( ) ]
366+ } )
367+ . collect ( ) ;
368+ ( leading_comments, trailing_comments)
369+ }
370+
361371/// Determines whether the provided [`Expression`] is a brackets string, i.e. `[[string]]`
362372/// We care about this because `[ [[string] ]` is invalid syntax if we remove the whitespace
363373pub fn is_brackets_string ( expression : & Expression ) -> bool {
@@ -1352,13 +1362,17 @@ fn format_hanging_expression_(
13521362
13531363 // If the context is for a prefix, we should always keep the parentheses, as they are always required
13541364 if use_internal_expression && !keep_parentheses {
1365+ let ( leading_comments, trailing_comments) =
1366+ contained_span_comments ( ctx, contained, shape) ;
13551367 format_hanging_expression_ (
13561368 ctx,
13571369 expression,
13581370 lhs_shape,
13591371 expression_context,
13601372 lhs_range,
13611373 )
1374+ . update_leading_trivia ( FormatTriviaType :: Append ( leading_comments) )
1375+ . update_trailing_trivia ( FormatTriviaType :: Append ( trailing_comments) )
13621376 } else {
13631377 let contained = format_contained_span ( ctx, contained, lhs_shape) ;
13641378
0 commit comments