@@ -9,8 +9,8 @@ use rustc_span::{BytePos, Span};
9
9
use crate :: chains:: rewrite_chain;
10
10
use crate :: closures;
11
11
use 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 ,
14
14
} ;
15
15
use crate :: config:: lists:: * ;
16
16
use crate :: config:: { Config , ControlBraceStyle , IndentStyle , Version } ;
@@ -829,38 +829,16 @@ impl<'a> ControlFlow<'a> {
829
829
let comments_lo = context
830
830
. snippet_provider
831
831
. 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 ,
862
841
) ;
863
- return rewrite_assign_rhs ( context, result, expr, cond_shape) ;
864
842
}
865
843
866
844
let expr_rw = expr. rewrite ( context, cond_shape) ;
@@ -1899,14 +1877,13 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
1899
1877
rewrite_assign_rhs_with ( context, lhs, ex, shape, RhsTactics :: Default )
1900
1878
}
1901
1879
1902
- pub ( crate ) fn rewrite_assign_rhs_with < S : Into < String > , R : Rewrite > (
1880
+ pub ( crate ) fn rewrite_assign_rhs_expr < R : Rewrite > (
1903
1881
context : & RewriteContext < ' _ > ,
1904
- lhs : S ,
1882
+ lhs : & str ,
1905
1883
ex : & R ,
1906
1884
shape : Shape ,
1907
1885
rhs_tactics : RhsTactics ,
1908
1886
) -> Option < String > {
1909
- let lhs = lhs. into ( ) ;
1910
1887
let last_line_width = last_line_width ( & lhs) . saturating_sub ( if lhs. contains ( '\n' ) {
1911
1888
shape. indent . width ( )
1912
1889
} else {
@@ -1918,22 +1895,67 @@ pub(crate) fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
1918
1895
offset : shape. offset + last_line_width + 1 ,
1919
1896
..shape
1920
1897
} ) ;
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 (
1922
1905
context,
1923
1906
ex,
1924
1907
orig_shape,
1925
1908
ex. rewrite ( context, orig_shape) ,
1926
1909
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) ?;
1928
1923
Some ( lhs + & rhs)
1929
1924
}
1930
1925
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
+
1931
1952
fn choose_rhs < R : Rewrite > (
1932
1953
context : & RewriteContext < ' _ > ,
1933
1954
expr : & R ,
1934
1955
shape : Shape ,
1935
1956
orig_rhs : Option < String > ,
1936
1957
rhs_tactics : RhsTactics ,
1958
+ has_rhs_comment : bool ,
1937
1959
) -> Option < String > {
1938
1960
match orig_rhs {
1939
1961
Some ( ref new_str)
@@ -1950,13 +1972,14 @@ fn choose_rhs<R: Rewrite>(
1950
1972
. indent
1951
1973
. block_indent ( context. config )
1952
1974
. to_string_with_newline ( context. config ) ;
1975
+ let before_space_str = if has_rhs_comment { "" } else { " " } ;
1953
1976
1954
1977
match ( orig_rhs, new_rhs) {
1955
1978
( Some ( ref orig_rhs) , Some ( ref new_rhs) )
1956
1979
if wrap_str ( new_rhs. clone ( ) , context. config . max_width ( ) , new_shape)
1957
1980
. is_none ( ) =>
1958
1981
{
1959
- Some ( format ! ( " {}" , orig_rhs) )
1982
+ Some ( format ! ( "{}{}" , before_space_str , orig_rhs) )
1960
1983
}
1961
1984
( Some ( ref orig_rhs) , Some ( ref new_rhs) )
1962
1985
if prefer_next_line ( orig_rhs, new_rhs, rhs_tactics) =>
@@ -1966,10 +1989,11 @@ fn choose_rhs<R: Rewrite>(
1966
1989
( None , Some ( ref new_rhs) ) => Some ( format ! ( "{}{}" , new_indent_str, new_rhs) ) ,
1967
1990
( None , None ) if rhs_tactics == RhsTactics :: AllowOverflow => {
1968
1991
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) )
1970
1994
}
1971
1995
( None , None ) => None ,
1972
- ( Some ( orig_rhs) , _) => Some ( format ! ( " {}" , orig_rhs) ) ,
1996
+ ( Some ( orig_rhs) , _) => Some ( format ! ( "{}{}" , before_space_str , orig_rhs) ) ,
1973
1997
}
1974
1998
}
1975
1999
}
0 commit comments