@@ -136,18 +136,25 @@ impl Rewrite for ast::Expr {
136
136
Some ( ident) => format ! ( " {}" , ident. node) ,
137
137
None => String :: new ( ) ,
138
138
} ;
139
- wrap_str ( format ! ( "continue{}" , id_str) , context. config . max_width , width, offset)
139
+ wrap_str ( format ! ( "continue{}" , id_str) ,
140
+ context. config . max_width ,
141
+ width,
142
+ offset)
140
143
}
141
144
ast:: ExprKind :: Break ( ref opt_ident) => {
142
145
let id_str = match * opt_ident {
143
146
Some ( ident) => format ! ( " {}" , ident. node) ,
144
147
None => String :: new ( ) ,
145
148
} ;
146
- wrap_str ( format ! ( "break{}" , id_str) , context. config . max_width , width, offset)
149
+ wrap_str ( format ! ( "break{}" , id_str) ,
150
+ context. config . max_width ,
151
+ width,
152
+ offset)
147
153
}
148
154
ast:: ExprKind :: Closure ( capture, ref fn_decl, ref body, _) => {
149
155
rewrite_closure ( capture, fn_decl, body, self . span , context, width, offset)
150
156
}
157
+ ast:: ExprKind :: Try ( ..) |
151
158
ast:: ExprKind :: Field ( ..) |
152
159
ast:: ExprKind :: TupField ( ..) |
153
160
ast:: ExprKind :: MethodCall ( ..) => rewrite_chain ( self , context, width, offset) ,
@@ -199,21 +206,15 @@ impl Rewrite for ast::Expr {
199
206
rewrite_unary_prefix ( context, delim, & * * rhs, width, offset)
200
207
}
201
208
( Some ( ref lhs) , None ) => {
202
- Some ( format ! ( "{}{}" ,
203
- try_opt!( lhs. rewrite( context,
204
- try_opt!( width. checked_sub( delim. len( ) ) ) ,
205
- offset) ) ,
206
- delim) )
209
+ rewrite_unary_suffix ( context, delim, & * * lhs, width, offset)
207
210
}
208
211
( None , None ) => wrap_str ( delim. into ( ) , context. config . max_width , width, offset) ,
209
212
}
210
213
}
211
214
// We do not format these expressions yet, but they should still
212
215
// satisfy our width restrictions.
213
216
ast:: ExprKind :: InPlace ( ..) |
214
- ast:: ExprKind :: InlineAsm ( ..) |
215
- // TODO(#867): Handle try shorthand
216
- ast:: ExprKind :: Try ( _) => {
217
+ ast:: ExprKind :: InlineAsm ( ..) => {
217
218
wrap_str ( context. snippet ( self . span ) ,
218
219
context. config . max_width ,
219
220
width,
@@ -689,11 +690,8 @@ fn extract_comment(span: Span,
689
690
-> Option < String > {
690
691
let comment_str = context. snippet ( span) ;
691
692
if contains_comment ( & comment_str) {
692
- let comment = try_opt ! ( rewrite_comment( comment_str. trim( ) ,
693
- false ,
694
- width,
695
- offset,
696
- context. config) ) ;
693
+ let comment =
694
+ try_opt ! ( rewrite_comment( comment_str. trim( ) , false , width, offset, context. config) ) ;
697
695
Some ( format ! ( "\n {indent}{}\n {indent}" ,
698
696
comment,
699
697
indent = offset. to_string( context. config) ) )
@@ -793,14 +791,11 @@ fn rewrite_if_else(context: &RewriteContext,
793
791
}
794
792
} ;
795
793
796
- let between_if_else_block = mk_sp ( if_block. span . hi ,
797
- context. codemap . span_before ( mk_sp ( if_block. span . hi ,
798
- else_block. span . lo ) ,
799
- "else" ) ) ;
800
- let between_if_else_block_comment = extract_comment ( between_if_else_block,
801
- & context,
802
- offset,
803
- width) ;
794
+ let between_if_else_block =
795
+ mk_sp ( if_block. span . hi ,
796
+ context. codemap . span_before ( mk_sp ( if_block. span . hi , else_block. span . lo ) , "else" ) ) ;
797
+ let between_if_else_block_comment =
798
+ extract_comment ( between_if_else_block, & context, offset, width) ;
804
799
805
800
let after_else = mk_sp ( context. codemap
806
801
. span_after ( mk_sp ( if_block. span . hi , else_block. span . lo ) ,
@@ -927,11 +922,8 @@ fn rewrite_match_arm_comment(context: &RewriteContext,
927
922
}
928
923
let missed_str = missed_str[ first..] . trim ( ) ;
929
924
if !missed_str. is_empty ( ) {
930
- let comment = try_opt ! ( rewrite_comment( & missed_str,
931
- false ,
932
- width,
933
- arm_indent,
934
- context. config) ) ;
925
+ let comment =
926
+ try_opt ! ( rewrite_comment( & missed_str, false , width, arm_indent, context. config) ) ;
935
927
result. push ( '\n' ) ;
936
928
result. push_str ( arm_indent_str) ;
937
929
result. push_str ( & comment) ;
@@ -1155,10 +1147,9 @@ impl Rewrite for ast::Arm {
1155
1147
let body_budget = try_opt ! ( width. checked_sub( context. config. tab_spaces) ) ;
1156
1148
let indent = context. block_indent . block_indent ( context. config ) ;
1157
1149
let inner_context = & RewriteContext { block_indent : indent, ..* context } ;
1158
- let next_line_body = try_opt ! ( nop_block_collapse( body. rewrite( inner_context,
1159
- body_budget,
1160
- indent) ,
1161
- body_budget) ) ;
1150
+ let next_line_body =
1151
+ try_opt ! ( nop_block_collapse( body. rewrite( inner_context, body_budget, indent) ,
1152
+ body_budget) ) ;
1162
1153
let indent_str = offset. block_indent ( context. config ) . to_string ( context. config ) ;
1163
1154
let ( body_prefix, body_suffix) = if context. config . wrap_match_arms {
1164
1155
if context. config . match_block_trailing_comma {
@@ -1762,6 +1753,21 @@ pub fn rewrite_unary_prefix<R: Rewrite>(context: &RewriteContext,
1762
1753
. map ( |r| format ! ( "{}{}" , prefix, r) )
1763
1754
}
1764
1755
1756
+ // FIXME: this is probably not correct for multi-line Rewrites. we should
1757
+ // subtract suffix.len() from the last line budget, not the first!
1758
+ pub fn rewrite_unary_suffix < R : Rewrite > ( context : & RewriteContext ,
1759
+ suffix : & str ,
1760
+ rewrite : & R ,
1761
+ width : usize ,
1762
+ offset : Indent )
1763
+ -> Option < String > {
1764
+ rewrite. rewrite ( context, try_opt ! ( width. checked_sub( suffix. len( ) ) ) , offset)
1765
+ . map ( |mut r| {
1766
+ r. push_str ( suffix) ;
1767
+ r
1768
+ } )
1769
+ }
1770
+
1765
1771
fn rewrite_unary_op ( context : & RewriteContext ,
1766
1772
op : & ast:: UnOp ,
1767
1773
expr : & ast:: Expr ,
@@ -1817,24 +1823,42 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
1817
1823
let max_width = try_opt ! ( width. checked_sub( last_line_width + 1 ) ) ;
1818
1824
let rhs = ex. rewrite ( & context, max_width, offset + last_line_width + 1 ) ;
1819
1825
1826
+ fn count_line_breaks ( src : & str ) -> usize {
1827
+ src. chars ( ) . filter ( |& x| x == '\n' ) . count ( )
1828
+ }
1829
+
1820
1830
match rhs {
1821
- Some ( new_str) => {
1831
+ Some ( ref new_str) if count_line_breaks ( new_str ) < 2 => {
1822
1832
result. push ( ' ' ) ;
1823
- result. push_str ( & new_str)
1833
+ result. push_str ( new_str) ;
1824
1834
}
1825
- None => {
1826
- // Expression did not fit on the same line as the identifier. Retry
1827
- // on the next line.
1835
+ _ => {
1836
+ // Expression did not fit on the same line as the identifier or is
1837
+ // at least three lines big. Try splitting the line and see
1838
+ // if that works better.
1828
1839
let new_offset = offset. block_indent ( context. config ) ;
1829
- result. push_str ( & format ! ( "\n {}" , new_offset. to_string( context. config) ) ) ;
1830
-
1831
- // FIXME: we probably should related max_width to width instead of
1832
- // config.max_width where is the 1 coming from anyway?
1833
- let max_width = try_opt ! ( context. config. max_width. checked_sub( new_offset. width( ) + 1 ) ) ;
1840
+ let max_width = try_opt ! ( ( width + offset. width( ) ) . checked_sub( new_offset. width( ) ) ) ;
1834
1841
let inner_context = context. nested_context ( ) ;
1835
- let rhs = ex. rewrite ( & inner_context, max_width, new_offset) ;
1836
-
1837
- result. push_str ( & & try_opt ! ( rhs) ) ;
1842
+ let new_rhs = ex. rewrite ( & inner_context, max_width, new_offset) ;
1843
+
1844
+ // FIXME: DRY!
1845
+ match ( rhs, new_rhs) {
1846
+ ( Some ( ref orig_rhs) , Some ( ref replacement_rhs) )
1847
+ if count_line_breaks ( orig_rhs) >
1848
+ count_line_breaks ( replacement_rhs) + 1 => {
1849
+ result. push_str ( & format ! ( "\n {}" , new_offset. to_string( context. config) ) ) ;
1850
+ result. push_str ( replacement_rhs) ;
1851
+ }
1852
+ ( None , Some ( ref final_rhs) ) => {
1853
+ result. push_str ( & format ! ( "\n {}" , new_offset. to_string( context. config) ) ) ;
1854
+ result. push_str ( final_rhs) ;
1855
+ }
1856
+ ( None , None ) => return None ,
1857
+ ( Some ( ref orig_rhs) , _) => {
1858
+ result. push ( ' ' ) ;
1859
+ result. push_str ( orig_rhs) ;
1860
+ }
1861
+ }
1838
1862
}
1839
1863
}
1840
1864
0 commit comments