@@ -144,11 +144,32 @@ fn rewrite_macro_name(
144
144
}
145
145
146
146
// Use this on failing to format the macro call.
147
- fn return_original_snippet_with_failure_marked (
147
+ fn return_macro_parse_failure_fallback (
148
148
context : & RewriteContext ,
149
+ indent : Indent ,
149
150
span : Span ,
150
151
) -> Option < String > {
152
+ // Mark this as a failure however we format it
151
153
context. macro_rewrite_failure . replace ( true ) ;
154
+
155
+ // Heuristically determine whether the last line of the macro uses "Block" style
156
+ // rather than using "Visual" style, or another indentation style.
157
+ let is_like_block_indent_style = context
158
+ . snippet ( span)
159
+ . lines ( )
160
+ . last ( )
161
+ . map ( |closing_line| {
162
+ closing_line. trim ( ) . chars ( ) . all ( |ch| match ch {
163
+ '}' | ')' | ']' => true ,
164
+ _ => false ,
165
+ } )
166
+ } )
167
+ . unwrap_or ( false ) ;
168
+ if is_like_block_indent_style {
169
+ return trim_left_preserve_layout ( context. snippet ( span) , indent, & context. config ) ;
170
+ }
171
+
172
+ // Return the snippet unmodified if the macro is not block-like
152
173
Some ( context. snippet ( span) . to_owned ( ) )
153
174
}
154
175
@@ -239,7 +260,9 @@ pub fn rewrite_macro_inner(
239
260
loop {
240
261
match parse_macro_arg ( & mut parser) {
241
262
Some ( arg) => arg_vec. push ( arg) ,
242
- None => return return_original_snippet_with_failure_marked ( context, mac. span ) ,
263
+ None => {
264
+ return return_macro_parse_failure_fallback ( context, shape. indent , mac. span ) ;
265
+ }
243
266
}
244
267
245
268
match parser. token {
@@ -260,17 +283,19 @@ pub fn rewrite_macro_inner(
260
283
}
261
284
}
262
285
None => {
263
- return return_original_snippet_with_failure_marked (
264
- context, mac. span ,
265
- )
286
+ return return_macro_parse_failure_fallback (
287
+ context,
288
+ shape. indent ,
289
+ mac. span ,
290
+ ) ;
266
291
}
267
292
}
268
293
}
269
294
}
270
- return return_original_snippet_with_failure_marked ( context, mac. span ) ;
295
+ return return_macro_parse_failure_fallback ( context, shape . indent , mac. span ) ;
271
296
}
272
297
_ if arg_vec. last ( ) . map_or ( false , MacroArg :: is_item) => continue ,
273
- _ => return return_original_snippet_with_failure_marked ( context, mac. span ) ,
298
+ _ => return return_macro_parse_failure_fallback ( context, shape . indent , mac. span ) ,
274
299
}
275
300
276
301
parser. bump ( ) ;
@@ -376,7 +401,7 @@ pub fn rewrite_macro_inner(
376
401
}
377
402
DelimToken :: Brace => {
378
403
// Skip macro invocations with braces, for now.
379
- trim_left_preserve_layout ( context. snippet ( mac. span ) , & shape. indent , & context. config )
404
+ trim_left_preserve_layout ( context. snippet ( mac. span ) , shape. indent , & context. config )
380
405
}
381
406
_ => unreachable ! ( ) ,
382
407
}
0 commit comments