@@ -1881,14 +1881,13 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
18811881 rewrite_assign_rhs_with ( context, lhs, ex, shape, RhsTactics :: Default )
18821882}
18831883
1884- pub ( crate ) fn rewrite_assign_rhs_with < S : Into < String > , R : Rewrite > (
1884+ pub ( crate ) fn rewrite_assign_rhs_expr < R : Rewrite > (
18851885 context : & RewriteContext < ' _ > ,
1886- lhs : S ,
1886+ lhs : & str ,
18871887 ex : & R ,
18881888 shape : Shape ,
18891889 rhs_tactics : RhsTactics ,
18901890) -> Option < String > {
1891- let lhs = lhs. into ( ) ;
18921891 let last_line_width = last_line_width ( & lhs) . saturating_sub ( if lhs. contains ( '\n' ) {
18931892 shape. indent . width ( )
18941893 } else {
@@ -1900,13 +1899,31 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19001899 offset : shape. offset + last_line_width + 1 ,
19011900 ..shape
19021901 } ) ;
1903- let rhs = choose_rhs (
1902+ let has_rhs_comment = if let Some ( offset) = lhs. find_last_uncommented ( "=" ) {
1903+ lhs. trim_end ( ) . len ( ) > offset + 1
1904+ } else {
1905+ false
1906+ } ;
1907+
1908+ choose_rhs (
19041909 context,
19051910 ex,
19061911 orig_shape,
19071912 ex. rewrite ( context, orig_shape) ,
19081913 rhs_tactics,
1909- ) ?;
1914+ has_rhs_comment,
1915+ )
1916+ }
1917+
1918+ pub ( crate ) fn rewrite_assign_rhs_with < S : Into < String > , R : Rewrite > (
1919+ context : & RewriteContext < ' _ > ,
1920+ lhs : S ,
1921+ ex : & R ,
1922+ shape : Shape ,
1923+ rhs_tactics : RhsTactics ,
1924+ ) -> Option < String > {
1925+ let lhs = lhs. into ( ) ;
1926+ let rhs = rewrite_assign_rhs_expr ( context, & lhs, ex, shape, rhs_tactics) ?;
19101927 Some ( lhs + & rhs)
19111928}
19121929
@@ -1916,6 +1933,7 @@ fn choose_rhs<R: Rewrite>(
19161933 shape : Shape ,
19171934 orig_rhs : Option < String > ,
19181935 rhs_tactics : RhsTactics ,
1936+ has_rhs_comment : bool ,
19191937) -> Option < String > {
19201938 match orig_rhs {
19211939 Some ( ref new_str)
@@ -1932,13 +1950,14 @@ fn choose_rhs<R: Rewrite>(
19321950 . indent
19331951 . block_indent ( context. config )
19341952 . to_string_with_newline ( context. config ) ;
1953+ let before_space_str = if has_rhs_comment { "" } else { " " } ;
19351954
19361955 match ( orig_rhs, new_rhs) {
19371956 ( Some ( ref orig_rhs) , Some ( ref new_rhs) )
19381957 if wrap_str ( new_rhs. clone ( ) , context. config . max_width ( ) , new_shape)
19391958 . is_none ( ) =>
19401959 {
1941- Some ( format ! ( " {}" , orig_rhs) )
1960+ Some ( format ! ( "{}{}" , before_space_str , orig_rhs) )
19421961 }
19431962 ( Some ( ref orig_rhs) , Some ( ref new_rhs) )
19441963 if prefer_next_line ( orig_rhs, new_rhs, rhs_tactics) =>
@@ -1948,10 +1967,11 @@ fn choose_rhs<R: Rewrite>(
19481967 ( None , Some ( ref new_rhs) ) => Some ( format ! ( "{}{}" , new_indent_str, new_rhs) ) ,
19491968 ( None , None ) if rhs_tactics == RhsTactics :: AllowOverflow => {
19501969 let shape = shape. infinite_width ( ) ;
1951- expr. rewrite ( context, shape) . map ( |s| format ! ( " {}" , s) )
1970+ expr. rewrite ( context, shape)
1971+ . map ( |s| format ! ( "{}{}" , before_space_str, s) )
19521972 }
19531973 ( None , None ) => None ,
1954- ( Some ( orig_rhs) , _) => Some ( format ! ( " {}" , orig_rhs) ) ,
1974+ ( Some ( orig_rhs) , _) => Some ( format ! ( "{}{}" , before_space_str , orig_rhs) ) ,
19551975 }
19561976 }
19571977 }
0 commit comments