@@ -144,11 +144,32 @@ fn rewrite_macro_name(
144144}
145145
146146// Use this on failing to format the macro call.
147- fn return_original_snippet_with_failure_marked (
147+ fn return_macro_parse_failure_fallback (
148148 context : & RewriteContext ,
149+ indent : Indent ,
149150 span : Span ,
150151) -> Option < String > {
152+ // Mark this as a failure however we format it
151153 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
152173 Some ( context. snippet ( span) . to_owned ( ) )
153174}
154175
@@ -239,7 +260,9 @@ pub fn rewrite_macro_inner(
239260 loop {
240261 match parse_macro_arg ( & mut parser) {
241262 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+ }
243266 }
244267
245268 match parser. token {
@@ -260,17 +283,19 @@ pub fn rewrite_macro_inner(
260283 }
261284 }
262285 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+ ) ;
266291 }
267292 }
268293 }
269294 }
270- return return_original_snippet_with_failure_marked ( context, mac. span ) ;
295+ return return_macro_parse_failure_fallback ( context, shape . indent , mac. span ) ;
271296 }
272297 _ 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 ) ,
274299 }
275300
276301 parser. bump ( ) ;
@@ -376,7 +401,7 @@ pub fn rewrite_macro_inner(
376401 }
377402 DelimToken :: Brace => {
378403 // 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 )
380405 }
381406 _ => unreachable ! ( ) ,
382407 }
0 commit comments