@@ -9,8 +9,8 @@ use rustc_span::{BytePos, Span};
99use crate :: chains:: rewrite_chain;
1010use crate :: closures;
1111use crate :: comment:: {
12- combine_strs_with_missing_comments, comment_style , contains_comment, recover_comment_removed,
13- rewrite_comment , rewrite_missing_comment, CharClasses , FindUncommented ,
12+ combine_strs_with_missing_comments, contains_comment, recover_comment_removed, rewrite_comment ,
13+ rewrite_missing_comment, CharClasses , FindUncommented ,
1414} ;
1515use crate :: config:: lists:: * ;
1616use crate :: config:: { Config , ControlBraceStyle , IndentStyle , Version } ;
@@ -829,38 +829,16 @@ impl<'a> ControlFlow<'a> {
829829 let comments_lo = context
830830 . snippet_provider
831831 . span_after ( self . span , self . connector . trim ( ) ) ;
832- let missing_comments = if let Some ( comment) =
833- rewrite_missing_comment ( mk_sp ( comments_lo, expr. span . lo ( ) ) , cond_shape, context)
834- {
835- if !self . connector . is_empty ( ) && !comment. is_empty ( ) {
836- if comment_style ( & comment, false ) . is_line_comment ( ) || comment. contains ( "\n " ) {
837- let newline = & pat_shape
838- . indent
839- . block_indent ( context. config )
840- . to_string_with_newline ( context. config ) ;
841- // An extra space is added when the lhs and rhs are joined
842- // so we need to remove one space from the end to ensure
843- // the comment and rhs are aligned.
844- let mut suffix = newline. as_ref ( ) . to_string ( ) ;
845- if !suffix. is_empty ( ) {
846- suffix. truncate ( suffix. len ( ) - 1 ) ;
847- }
848- format ! ( "{}{}{}" , newline, comment, suffix)
849- } else {
850- format ! ( " {}" , comment)
851- }
852- } else {
853- comment
854- }
855- } else {
856- "" . to_owned ( )
857- } ;
858-
859- let result = format ! (
860- "{}{}{}{}" ,
861- matcher, pat_string, self . connector, missing_comments
832+ let comments_span = mk_sp ( comments_lo, expr. span . lo ( ) ) ;
833+ return rewrite_assign_rhs_with_comments (
834+ context,
835+ & format ! ( "{}{}{}" , matcher, pat_string, self . connector) ,
836+ expr,
837+ cond_shape,
838+ RhsTactics :: Default ,
839+ comments_span,
840+ true ,
862841 ) ;
863- return rewrite_assign_rhs ( context, result, expr, cond_shape) ;
864842 }
865843
866844 let expr_rw = expr. rewrite ( context, cond_shape) ;
@@ -1899,14 +1877,13 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
18991877 rewrite_assign_rhs_with ( context, lhs, ex, shape, RhsTactics :: Default )
19001878}
19011879
1902- pub ( crate ) fn rewrite_assign_rhs_with < S : Into < String > , R : Rewrite > (
1880+ pub ( crate ) fn rewrite_assign_rhs_expr < R : Rewrite > (
19031881 context : & RewriteContext < ' _ > ,
1904- lhs : S ,
1882+ lhs : & str ,
19051883 ex : & R ,
19061884 shape : Shape ,
19071885 rhs_tactics : RhsTactics ,
19081886) -> Option < String > {
1909- let lhs = lhs. into ( ) ;
19101887 let last_line_width = last_line_width ( & lhs) . saturating_sub ( if lhs. contains ( '\n' ) {
19111888 shape. indent . width ( )
19121889 } else {
@@ -1918,22 +1895,67 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19181895 offset : shape. offset + last_line_width + 1 ,
19191896 ..shape
19201897 } ) ;
1921- let rhs = choose_rhs (
1898+ let has_rhs_comment = if let Some ( offset) = lhs. find_last_uncommented ( "=" ) {
1899+ lhs. trim_end ( ) . len ( ) > offset + 1
1900+ } else {
1901+ false
1902+ } ;
1903+
1904+ choose_rhs (
19221905 context,
19231906 ex,
19241907 orig_shape,
19251908 ex. rewrite ( context, orig_shape) ,
19261909 rhs_tactics,
1927- ) ?;
1910+ has_rhs_comment,
1911+ )
1912+ }
1913+
1914+ pub ( crate ) fn rewrite_assign_rhs_with < S : Into < String > , R : Rewrite > (
1915+ context : & RewriteContext < ' _ > ,
1916+ lhs : S ,
1917+ ex : & R ,
1918+ shape : Shape ,
1919+ rhs_tactics : RhsTactics ,
1920+ ) -> Option < String > {
1921+ let lhs = lhs. into ( ) ;
1922+ let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_tactics) ?;
19281923 Some ( lhs + & rhs)
19291924}
19301925
1926+ pub ( crate ) fn rewrite_assign_rhs_with_comments < S : Into < String > , R : Rewrite > (
1927+ context : & RewriteContext < ' _ > ,
1928+ lhs : S ,
1929+ ex : & R ,
1930+ shape : Shape ,
1931+ rhs_tactics : RhsTactics ,
1932+ between_span : Span ,
1933+ allow_extend : bool ,
1934+ ) -> Option < String > {
1935+ let lhs = lhs. into ( ) ;
1936+ let contains_comment = contains_comment ( context. snippet ( between_span) ) ;
1937+ let shape = if contains_comment {
1938+ shape. block_left ( context. config . tab_spaces ( ) ) ?
1939+ } else {
1940+ shape
1941+ } ;
1942+ let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_tactics) ?;
1943+
1944+ if contains_comment {
1945+ let rhs = rhs. trim_start ( ) ;
1946+ combine_strs_with_missing_comments ( context, & lhs, & rhs, between_span, shape, allow_extend)
1947+ } else {
1948+ Some ( lhs + & rhs)
1949+ }
1950+ }
1951+
19311952fn choose_rhs < R : Rewrite > (
19321953 context : & RewriteContext < ' _ > ,
19331954 expr : & R ,
19341955 shape : Shape ,
19351956 orig_rhs : Option < String > ,
19361957 rhs_tactics : RhsTactics ,
1958+ has_rhs_comment : bool ,
19371959) -> Option < String > {
19381960 match orig_rhs {
19391961 Some ( ref new_str)
@@ -1950,13 +1972,14 @@ fn choose_rhs<R: Rewrite>(
19501972 . indent
19511973 . block_indent ( context. config )
19521974 . to_string_with_newline ( context. config ) ;
1975+ let before_space_str = if has_rhs_comment { "" } else { " " } ;
19531976
19541977 match ( orig_rhs, new_rhs) {
19551978 ( Some ( ref orig_rhs) , Some ( ref new_rhs) )
19561979 if wrap_str ( new_rhs. clone ( ) , context. config . max_width ( ) , new_shape)
19571980 . is_none ( ) =>
19581981 {
1959- Some ( format ! ( " {}" , orig_rhs) )
1982+ Some ( format ! ( "{}{}" , before_space_str , orig_rhs) )
19601983 }
19611984 ( Some ( ref orig_rhs) , Some ( ref new_rhs) )
19621985 if prefer_next_line ( orig_rhs, new_rhs, rhs_tactics) =>
@@ -1966,10 +1989,11 @@ fn choose_rhs<R: Rewrite>(
19661989 ( None , Some ( ref new_rhs) ) => Some ( format ! ( "{}{}" , new_indent_str, new_rhs) ) ,
19671990 ( None , None ) if rhs_tactics == RhsTactics :: AllowOverflow => {
19681991 let shape = shape. infinite_width ( ) ;
1969- expr. rewrite ( context, shape) . map ( |s| format ! ( " {}" , s) )
1992+ expr. rewrite ( context, shape)
1993+ . map ( |s| format ! ( "{}{}" , before_space_str, s) )
19701994 }
19711995 ( None , None ) => None ,
1972- ( Some ( orig_rhs) , _) => Some ( format ! ( " {}" , orig_rhs) ) ,
1996+ ( Some ( orig_rhs) , _) => Some ( format ! ( "{}{}" , before_space_str , orig_rhs) ) ,
19731997 }
19741998 }
19751999 }
0 commit comments