@@ -2351,23 +2351,34 @@ fn call_chain_elements_are_user_multilined<'src>(
23512351 | prism:: Node :: ParenthesesNode { .. }
23522352 ) ;
23532353
2354- // _However_, don't ignore this if there are comments in the call chain though; this check may
2355- // cause it to single-lined, which breaks comment rendering. Specifically, we're checking
2356- // for comments in between the receiver expression and the following message, e.g.
2357- // ```ruby
2358- // [stuff]
2359- // # spooky comment
2360- // .freeze
2361- // ```
2362- // For cases without the comment, we'd usually put this all on one line, but if we force
2363- // it all on one line, this will break the comment insertion logic, and given the comment's
2364- // placement, the user probably intended to break this onto multiple lines anyways.
2365- let has_comment = ps. has_comment_in_offset_span (
2366- call_chain_elements[ 0 ] . location ( ) . end_offset ( ) ,
2367- start_loc_for_call_node_in_chain ( & call_chain_elements[ 1 ] . as_call_node ( ) . unwrap ( ) ) ,
2368- ) ;
2369- if is_literal_expression && !has_comment {
2370- call_chain_elements = & call_chain_elements[ 1 ..] ;
2354+ if is_literal_expression {
2355+ // _However_, don't ignore this if there are comments in the call chain though; this check may
2356+ // cause it to single-lined, which breaks comment rendering. Specifically, we're checking
2357+ // for comments in between the receiver expression and the following message, e.g.
2358+ // ```ruby
2359+ // [stuff]
2360+ // # spooky comment
2361+ // .freeze
2362+ // ```
2363+ // For cases without the comment, we'd usually put this all on one line, but if we force
2364+ // it all on one line, this will break the comment insertion logic, and given the comment's
2365+ // placement, the user probably intended to break this onto multiple lines anyways.
2366+ let first_call_start_line = ps. get_line_number_for_offset (
2367+ start_loc_for_call_node_in_chain ( & call_chain_elements[ 1 ] . as_call_node ( ) . unwrap ( ) ) ,
2368+ ) ;
2369+ let leading_expr_end_line =
2370+ ps. get_line_number_for_offset ( call_chain_elements[ 0 ] . location ( ) . end_offset ( ) ) ;
2371+
2372+ // Note: We check from `leading_expr_end_line + 1` because comments on the same line as
2373+ // the closing brace will be rendered into the breakable during formatting, so they don't
2374+ // affect whether we should multiline the call chain. We check `first_call_start_line + 1`
2375+ // because the range checked by `has_comments_in_line` is non-inclusive.
2376+ let has_comment_between_expression_and_call =
2377+ ps. has_comments_in_line ( leading_expr_end_line + 1 , first_call_start_line + 1 ) ;
2378+
2379+ if !has_comment_between_expression_and_call {
2380+ call_chain_elements = & call_chain_elements[ 1 ..] ;
2381+ }
23712382 }
23722383 }
23732384
0 commit comments