]> git.lizzy.rs Git - rust.git/blob - src/macros.rs
7348e7a0315cdcfe0b645a4df43791a8a8cafb56
[rust.git] / src / macros.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Format list-like macro invocations. These are invocations whose token trees
12 // can be interpreted as expressions and separated by commas.
13 // Note that these token trees do not actually have to be interpreted as
14 // expressions by the compiler. An example of an invocation we would reformat is
15 // foo!( x, y, z ). The token x may represent an identifier in the code, but we
16 // interpreted as an expression.
17 // Macro uses which are not-list like, such as bar!(key => val), will not be
18 // reformatted.
19 // List-like invocations with parentheses will be formatted as function calls,
20 // and those with brackets will be formatted as array literals.
21
22 use std::collections::HashMap;
23
24 use config::lists::*;
25 use syntax::parse::new_parser_from_tts;
26 use syntax::parse::parser::Parser;
27 use syntax::parse::token::{BinOpToken, DelimToken, Token};
28 use syntax::print::pprust;
29 use syntax::source_map::{BytePos, Span};
30 use syntax::symbol;
31 use syntax::tokenstream::{Cursor, ThinTokenStream, TokenStream, TokenTree};
32 use syntax::ThinVec;
33 use syntax::{ast, ptr};
34
35 use comment::{
36     contains_comment, remove_trailing_white_spaces, CharClasses, FindUncommented, FullCodeCharKind,
37     LineClasses,
38 };
39 use expr::rewrite_array;
40 use lists::{itemize_list, write_list, ListFormatting};
41 use overflow;
42 use rewrite::{Rewrite, RewriteContext};
43 use shape::{Indent, Shape};
44 use source_map::SpanUtils;
45 use spanned::Spanned;
46 use utils::{format_visibility, mk_sp, rewrite_ident, wrap_str};
47 use visitor::FmtVisitor;
48
49 const FORCED_BRACKET_MACROS: &[&str] = &["vec!"];
50
51 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
52 pub enum MacroPosition {
53     Item,
54     Statement,
55     Expression,
56     Pat,
57 }
58
59 #[derive(Debug)]
60 pub enum MacroArg {
61     Expr(ptr::P<ast::Expr>),
62     Ty(ptr::P<ast::Ty>),
63     Pat(ptr::P<ast::Pat>),
64     Item(ptr::P<ast::Item>),
65 }
66
67 impl Rewrite for ast::Item {
68     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
69         let mut visitor = ::visitor::FmtVisitor::from_context(context);
70         visitor.block_indent = shape.indent;
71         visitor.last_pos = self.span().lo();
72         visitor.visit_item(self);
73         Some(visitor.buffer.to_owned())
74     }
75 }
76
77 impl Rewrite for MacroArg {
78     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
79         match *self {
80             MacroArg::Expr(ref expr) => expr.rewrite(context, shape),
81             MacroArg::Ty(ref ty) => ty.rewrite(context, shape),
82             MacroArg::Pat(ref pat) => pat.rewrite(context, shape),
83             MacroArg::Item(ref item) => item.rewrite(context, shape),
84         }
85     }
86 }
87
88 fn parse_macro_arg(parser: &mut Parser) -> Option<MacroArg> {
89     macro_rules! parse_macro_arg {
90         ($macro_arg:ident, $parser:ident, $f:expr) => {
91             let mut cloned_parser = (*parser).clone();
92             match cloned_parser.$parser() {
93                 Ok(x) => {
94                     if parser.sess.span_diagnostic.has_errors() {
95                         parser.sess.span_diagnostic.reset_err_count();
96                     } else {
97                         // Parsing succeeded.
98                         *parser = cloned_parser;
99                         return Some(MacroArg::$macro_arg($f(x)?));
100                     }
101                 }
102                 Err(mut e) => {
103                     e.cancel();
104                     parser.sess.span_diagnostic.reset_err_count();
105                 }
106             }
107         };
108     }
109
110     parse_macro_arg!(Expr, parse_expr, |x: ptr::P<ast::Expr>| Some(x));
111     parse_macro_arg!(Ty, parse_ty, |x: ptr::P<ast::Ty>| Some(x));
112     parse_macro_arg!(Pat, parse_pat, |x: ptr::P<ast::Pat>| Some(x));
113     // `parse_item` returns `Option<ptr::P<ast::Item>>`.
114     parse_macro_arg!(Item, parse_item, |x: Option<ptr::P<ast::Item>>| x);
115
116     None
117 }
118
119 /// Rewrite macro name without using pretty-printer if possible.
120 fn rewrite_macro_name(
121     context: &RewriteContext,
122     path: &ast::Path,
123     extra_ident: Option<ast::Ident>,
124 ) -> String {
125     let name = if path.segments.len() == 1 {
126         // Avoid using pretty-printer in the common case.
127         format!("{}!", rewrite_ident(context, path.segments[0].ident))
128     } else {
129         format!("{}!", path)
130     };
131     match extra_ident {
132         Some(ident) if ident != symbol::keywords::Invalid.ident() => format!("{} {}", name, ident),
133         _ => name,
134     }
135 }
136
137 // Use this on failing to format the macro call.
138 fn return_original_snippet_with_failure_marked(
139     context: &RewriteContext,
140     span: Span,
141 ) -> Option<String> {
142     context.macro_rewrite_failure.replace(true);
143     Some(context.snippet(span).to_owned())
144 }
145
146 struct InsideMacroGuard<'a> {
147     context: &'a RewriteContext<'a>,
148     is_nested: bool,
149 }
150
151 impl<'a> InsideMacroGuard<'a> {
152     fn inside_macro_context(context: &'a RewriteContext) -> InsideMacroGuard<'a> {
153         let is_nested = context.inside_macro.replace(true);
154         InsideMacroGuard { context, is_nested }
155     }
156 }
157
158 impl<'a> Drop for InsideMacroGuard<'a> {
159     fn drop(&mut self) {
160         self.context.inside_macro.replace(self.is_nested);
161     }
162 }
163
164 pub fn rewrite_macro(
165     mac: &ast::Mac,
166     extra_ident: Option<ast::Ident>,
167     context: &RewriteContext,
168     shape: Shape,
169     position: MacroPosition,
170 ) -> Option<String> {
171     let guard = InsideMacroGuard::inside_macro_context(context);
172     let result = rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested);
173     if result.is_none() {
174         context.macro_rewrite_failure.replace(true);
175     }
176     result
177 }
178
179 pub fn rewrite_macro_inner(
180     mac: &ast::Mac,
181     extra_ident: Option<ast::Ident>,
182     context: &RewriteContext,
183     shape: Shape,
184     position: MacroPosition,
185     is_nested_macro: bool,
186 ) -> Option<String> {
187     if context.config.use_try_shorthand() {
188         if let Some(expr) = convert_try_mac(mac, context) {
189             context.inside_macro.replace(false);
190             return expr.rewrite(context, shape);
191         }
192     }
193
194     let original_style = macro_style(mac, context);
195
196     let macro_name = rewrite_macro_name(context, &mac.node.path, extra_ident);
197
198     let style = if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) && !is_nested_macro {
199         DelimToken::Bracket
200     } else {
201         original_style
202     };
203
204     let ts: TokenStream = mac.node.stream();
205     let has_comment = contains_comment(context.snippet(mac.span));
206     if ts.is_empty() && !has_comment {
207         return match style {
208             DelimToken::Paren if position == MacroPosition::Item => {
209                 Some(format!("{}();", macro_name))
210             }
211             DelimToken::Paren => Some(format!("{}()", macro_name)),
212             DelimToken::Bracket => Some(format!("{}[]", macro_name)),
213             DelimToken::Brace => Some(format!("{}{{}}", macro_name)),
214             _ => unreachable!(),
215         };
216     }
217     // Format well-known macros which cannot be parsed as a valid AST.
218     if macro_name == "lazy_static!" && !has_comment {
219         if let success @ Some(..) = format_lazy_static(context, shape, &ts) {
220             return success;
221         }
222     }
223
224     let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
225     let mut arg_vec = Vec::new();
226     let mut vec_with_semi = false;
227     let mut trailing_comma = false;
228
229     if DelimToken::Brace != style {
230         loop {
231             match parse_macro_arg(&mut parser) {
232                 Some(arg) => arg_vec.push(arg),
233                 None => return return_original_snippet_with_failure_marked(context, mac.span),
234             }
235
236             match parser.token {
237                 Token::Eof => break,
238                 Token::Comma => (),
239                 Token::Semi => {
240                     // Try to parse `vec![expr; expr]`
241                     if FORCED_BRACKET_MACROS.contains(&&macro_name[..]) {
242                         parser.bump();
243                         if parser.token != Token::Eof {
244                             match parse_macro_arg(&mut parser) {
245                                 Some(arg) => {
246                                     arg_vec.push(arg);
247                                     parser.bump();
248                                     if parser.token == Token::Eof && arg_vec.len() == 2 {
249                                         vec_with_semi = true;
250                                         break;
251                                     }
252                                 }
253                                 None => {
254                                     return return_original_snippet_with_failure_marked(
255                                         context, mac.span,
256                                     )
257                                 }
258                             }
259                         }
260                     }
261                     return return_original_snippet_with_failure_marked(context, mac.span);
262                 }
263                 _ => return return_original_snippet_with_failure_marked(context, mac.span),
264             }
265
266             parser.bump();
267
268             if parser.token == Token::Eof {
269                 trailing_comma = true;
270                 break;
271             }
272         }
273     }
274
275     if !arg_vec.is_empty() && arg_vec.iter().all(|arg| {
276         if let MacroArg::Item(..) = arg {
277             true
278         } else {
279             false
280         }
281     }) {
282         return rewrite_macro_with_items(
283             context,
284             &arg_vec,
285             &macro_name,
286             shape,
287             style,
288             position,
289             mac.span,
290         );
291     }
292
293     match style {
294         DelimToken::Paren => {
295             // Format macro invocation as function call, preserve the trailing
296             // comma because not all macros support them.
297             overflow::rewrite_with_parens(
298                 context,
299                 &macro_name,
300                 arg_vec.iter(),
301                 shape,
302                 mac.span,
303                 context.config.width_heuristics().fn_call_width,
304                 if trailing_comma {
305                     Some(SeparatorTactic::Always)
306                 } else {
307                     Some(SeparatorTactic::Never)
308                 },
309             )
310             .map(|rw| match position {
311                 MacroPosition::Item => format!("{};", rw),
312                 _ => rw,
313             })
314         }
315         DelimToken::Bracket => {
316             // Handle special case: `vec![expr; expr]`
317             if vec_with_semi {
318                 let mac_shape = shape.offset_left(macro_name.len())?;
319                 // 8 = `vec![]` + `; `
320                 let total_overhead = 8;
321                 let nested_shape = mac_shape.block_indent(context.config.tab_spaces());
322                 let lhs = arg_vec[0].rewrite(context, nested_shape)?;
323                 let rhs = arg_vec[1].rewrite(context, nested_shape)?;
324                 if !lhs.contains('\n')
325                     && !rhs.contains('\n')
326                     && lhs.len() + rhs.len() + total_overhead <= shape.width
327                 {
328                     Some(format!("{}[{}; {}]", macro_name, lhs, rhs))
329                 } else {
330                     Some(format!(
331                         "{}[{}{};{}{}{}]",
332                         macro_name,
333                         nested_shape.indent.to_string_with_newline(context.config),
334                         lhs,
335                         nested_shape.indent.to_string_with_newline(context.config),
336                         rhs,
337                         shape.indent.to_string_with_newline(context.config),
338                     ))
339                 }
340             } else {
341                 // If we are rewriting `vec!` macro or other special macros,
342                 // then we can rewrite this as an usual array literal.
343                 // Otherwise, we must preserve the original existence of trailing comma.
344                 let macro_name = &macro_name.as_str();
345                 let mut force_trailing_comma = if trailing_comma {
346                     Some(SeparatorTactic::Always)
347                 } else {
348                     Some(SeparatorTactic::Never)
349                 };
350                 if FORCED_BRACKET_MACROS.contains(macro_name) && !is_nested_macro {
351                     context.inside_macro.replace(false);
352                     if context.use_block_indent() {
353                         force_trailing_comma = Some(SeparatorTactic::Vertical);
354                     };
355                 }
356                 let rewrite = rewrite_array(
357                     macro_name,
358                     arg_vec.iter(),
359                     mac.span,
360                     context,
361                     shape,
362                     force_trailing_comma,
363                     Some(original_style),
364                 )?;
365                 let comma = match position {
366                     MacroPosition::Item => ";",
367                     _ => "",
368                 };
369
370                 Some(format!("{}{}", rewrite, comma))
371             }
372         }
373         DelimToken::Brace => {
374             // Skip macro invocations with braces, for now.
375             indent_macro_snippet(context, context.snippet(mac.span), shape.indent)
376         }
377         _ => unreachable!(),
378     }
379 }
380
381 pub fn rewrite_macro_def(
382     context: &RewriteContext,
383     shape: Shape,
384     indent: Indent,
385     def: &ast::MacroDef,
386     ident: ast::Ident,
387     vis: &ast::Visibility,
388     span: Span,
389 ) -> Option<String> {
390     let snippet = Some(remove_trailing_white_spaces(context.snippet(span)));
391     if snippet.as_ref().map_or(true, |s| s.ends_with(';')) {
392         return snippet;
393     }
394
395     let mut parser = MacroParser::new(def.stream().into_trees());
396     let parsed_def = match parser.parse() {
397         Some(def) => def,
398         None => return snippet,
399     };
400
401     let mut result = if def.legacy {
402         String::from("macro_rules!")
403     } else {
404         format!("{}macro", format_visibility(context, vis))
405     };
406
407     result += " ";
408     result += rewrite_ident(context, ident);
409
410     let multi_branch_style = def.legacy || parsed_def.branches.len() != 1;
411
412     let arm_shape = if multi_branch_style {
413         shape
414             .block_indent(context.config.tab_spaces())
415             .with_max_width(context.config)
416     } else {
417         shape
418     };
419
420     let branch_items = itemize_list(
421         context.snippet_provider,
422         parsed_def.branches.iter(),
423         "}",
424         ";",
425         |branch| branch.span.lo(),
426         |branch| branch.span.hi(),
427         |branch| match branch.rewrite(context, arm_shape, multi_branch_style) {
428             Some(v) => Some(v),
429             // if the rewrite returned None because a macro could not be rewritten, then return the
430             // original body
431             None if *context.macro_rewrite_failure.borrow() == true => {
432                 Some(context.snippet(branch.body).trim().to_string())
433             }
434             None => None,
435         },
436         context.snippet_provider.span_after(span, "{"),
437         span.hi(),
438         false,
439     )
440     .collect::<Vec<_>>();
441
442     let fmt = ListFormatting::new(arm_shape, context.config)
443         .separator(if def.legacy { ";" } else { "" })
444         .trailing_separator(SeparatorTactic::Always)
445         .preserve_newline(true);
446
447     if multi_branch_style {
448         result += " {";
449         result += &arm_shape.indent.to_string_with_newline(context.config);
450     }
451
452     match write_list(&branch_items, &fmt) {
453         Some(ref s) => result += s,
454         None => return snippet,
455     }
456
457     if multi_branch_style {
458         result += &indent.to_string_with_newline(context.config);
459         result += "}";
460     }
461
462     Some(result)
463 }
464
465 fn register_metavariable(
466     map: &mut HashMap<String, String>,
467     result: &mut String,
468     name: &str,
469     dollar_count: usize,
470 ) {
471     let mut new_name = String::new();
472     let mut old_name = String::new();
473
474     old_name.push('$');
475     for _ in 0..(dollar_count - 1) {
476         new_name.push('$');
477         old_name.push('$');
478     }
479     new_name.push('z');
480     new_name.push_str(&name);
481     old_name.push_str(&name);
482
483     result.push_str(&new_name);
484     map.insert(old_name, new_name);
485 }
486
487 // Replaces `$foo` with `zfoo`. We must check for name overlap to ensure we
488 // aren't causing problems.
489 // This should also work for escaped `$` variables, where we leave earlier `$`s.
490 fn replace_names(input: &str) -> Option<(String, HashMap<String, String>)> {
491     // Each substitution will require five or six extra bytes.
492     let mut result = String::with_capacity(input.len() + 64);
493     let mut substs = HashMap::new();
494     let mut dollar_count = 0;
495     let mut cur_name = String::new();
496
497     for (kind, c) in CharClasses::new(input.chars()) {
498         if kind != FullCodeCharKind::Normal {
499             result.push(c);
500         } else if c == '$' {
501             dollar_count += 1;
502         } else if dollar_count == 0 {
503             result.push(c);
504         } else if !c.is_alphanumeric() && !cur_name.is_empty() {
505             // Terminates a name following one or more dollars.
506             register_metavariable(&mut substs, &mut result, &cur_name, dollar_count);
507
508             result.push(c);
509             dollar_count = 0;
510             cur_name.clear();
511         } else if c == '(' && cur_name.is_empty() {
512             // FIXME: Support macro def with repeat.
513             return None;
514         } else if c.is_alphanumeric() || c == '_' {
515             cur_name.push(c);
516         }
517     }
518
519     if !cur_name.is_empty() {
520         register_metavariable(&mut substs, &mut result, &cur_name, dollar_count);
521     }
522
523     debug!("replace_names `{}` {:?}", result, substs);
524
525     Some((result, substs))
526 }
527
528 #[derive(Debug, Clone)]
529 enum MacroArgKind {
530     /// e.g. `$x: expr`.
531     MetaVariable(ast::Ident, String),
532     /// e.g. `$($foo: expr),*`
533     Repeat(
534         /// `()`, `[]` or `{}`.
535         DelimToken,
536         /// Inner arguments inside delimiters.
537         Vec<ParsedMacroArg>,
538         /// Something after the closing delimiter and the repeat token, if available.
539         Option<Box<ParsedMacroArg>>,
540         /// The repeat token. This could be one of `*`, `+` or `?`.
541         Token,
542     ),
543     /// e.g. `[derive(Debug)]`
544     Delimited(DelimToken, Vec<ParsedMacroArg>),
545     /// A possible separator. e.g. `,` or `;`.
546     Separator(String, String),
547     /// Other random stuff that does not fit to other kinds.
548     /// e.g. `== foo` in `($x: expr == foo)`.
549     Other(String, String),
550 }
551
552 fn delim_token_to_str(
553     context: &RewriteContext,
554     delim_token: DelimToken,
555     shape: Shape,
556     use_multiple_lines: bool,
557     inner_is_empty: bool,
558 ) -> (String, String) {
559     let (lhs, rhs) = match delim_token {
560         DelimToken::Paren => ("(", ")"),
561         DelimToken::Bracket => ("[", "]"),
562         DelimToken::Brace => {
563             if inner_is_empty || use_multiple_lines {
564                 ("{", "}")
565             } else {
566                 ("{ ", " }")
567             }
568         }
569         DelimToken::NoDelim => ("", ""),
570     };
571     if use_multiple_lines {
572         let indent_str = shape.indent.to_string_with_newline(context.config);
573         let nested_indent_str = shape
574             .indent
575             .block_indent(context.config)
576             .to_string_with_newline(context.config);
577         (
578             format!("{}{}", lhs, nested_indent_str),
579             format!("{}{}", indent_str, rhs),
580         )
581     } else {
582         (lhs.to_owned(), rhs.to_owned())
583     }
584 }
585
586 impl MacroArgKind {
587     fn starts_with_brace(&self) -> bool {
588         match *self {
589             MacroArgKind::Repeat(DelimToken::Brace, _, _, _)
590             | MacroArgKind::Delimited(DelimToken::Brace, _) => true,
591             _ => false,
592         }
593     }
594
595     fn starts_with_dollar(&self) -> bool {
596         match *self {
597             MacroArgKind::Repeat(..) | MacroArgKind::MetaVariable(..) => true,
598             _ => false,
599         }
600     }
601
602     fn ends_with_space(&self) -> bool {
603         match *self {
604             MacroArgKind::Separator(..) => true,
605             _ => false,
606         }
607     }
608
609     fn has_meta_var(&self) -> bool {
610         match *self {
611             MacroArgKind::MetaVariable(..) => true,
612             MacroArgKind::Repeat(_, ref args, _, _) => args.iter().any(|a| a.kind.has_meta_var()),
613             _ => false,
614         }
615     }
616
617     fn rewrite(
618         &self,
619         context: &RewriteContext,
620         shape: Shape,
621         use_multiple_lines: bool,
622     ) -> Option<String> {
623         let rewrite_delimited_inner = |delim_tok, args| -> Option<(String, String, String)> {
624             let inner = wrap_macro_args(context, args, shape)?;
625             let (lhs, rhs) = delim_token_to_str(context, delim_tok, shape, false, inner.is_empty());
626             if lhs.len() + inner.len() + rhs.len() <= shape.width {
627                 return Some((lhs, inner, rhs));
628             }
629
630             let (lhs, rhs) = delim_token_to_str(context, delim_tok, shape, true, false);
631             let nested_shape = shape
632                 .block_indent(context.config.tab_spaces())
633                 .with_max_width(context.config);
634             let inner = wrap_macro_args(context, args, nested_shape)?;
635             Some((lhs, inner, rhs))
636         };
637
638         match *self {
639             MacroArgKind::MetaVariable(ty, ref name) => {
640                 Some(format!("${}:{}", name, ty.name.as_str()))
641             }
642             MacroArgKind::Repeat(delim_tok, ref args, ref another, ref tok) => {
643                 let (lhs, inner, rhs) = rewrite_delimited_inner(delim_tok, args)?;
644                 let another = another
645                     .as_ref()
646                     .and_then(|a| a.rewrite(context, shape, use_multiple_lines))
647                     .unwrap_or_else(|| "".to_owned());
648                 let repeat_tok = pprust::token_to_string(tok);
649
650                 Some(format!("${}{}{}{}{}", lhs, inner, rhs, another, repeat_tok))
651             }
652             MacroArgKind::Delimited(delim_tok, ref args) => {
653                 rewrite_delimited_inner(delim_tok, args)
654                     .map(|(lhs, inner, rhs)| format!("{}{}{}", lhs, inner, rhs))
655             }
656             MacroArgKind::Separator(ref sep, ref prefix) => Some(format!("{}{} ", prefix, sep)),
657             MacroArgKind::Other(ref inner, ref prefix) => Some(format!("{}{}", prefix, inner)),
658         }
659     }
660 }
661
662 #[derive(Debug, Clone)]
663 struct ParsedMacroArg {
664     kind: MacroArgKind,
665     span: Span,
666 }
667
668 impl ParsedMacroArg {
669     pub fn rewrite(
670         &self,
671         context: &RewriteContext,
672         shape: Shape,
673         use_multiple_lines: bool,
674     ) -> Option<String> {
675         self.kind.rewrite(context, shape, use_multiple_lines)
676     }
677 }
678
679 /// Parses macro arguments on macro def.
680 struct MacroArgParser {
681     /// Holds either a name of the next metavariable, a separator or a junk.
682     buf: String,
683     /// The start position on the current buffer.
684     lo: BytePos,
685     /// The first token of the current buffer.
686     start_tok: Token,
687     /// Set to true if we are parsing a metavariable or a repeat.
688     is_meta_var: bool,
689     /// The position of the last token.
690     hi: BytePos,
691     /// The last token parsed.
692     last_tok: Token,
693     /// Holds the parsed arguments.
694     result: Vec<ParsedMacroArg>,
695 }
696
697 fn last_tok(tt: &TokenTree) -> Token {
698     match *tt {
699         TokenTree::Token(_, ref t) => t.clone(),
700         TokenTree::Delimited(_, ref d) => d.close_token(),
701     }
702 }
703
704 impl MacroArgParser {
705     pub fn new() -> MacroArgParser {
706         MacroArgParser {
707             lo: BytePos(0),
708             hi: BytePos(0),
709             buf: String::new(),
710             is_meta_var: false,
711             last_tok: Token::Eof,
712             start_tok: Token::Eof,
713             result: vec![],
714         }
715     }
716
717     fn set_last_tok(&mut self, tok: &TokenTree) {
718         self.hi = tok.span().hi();
719         self.last_tok = last_tok(tok);
720     }
721
722     fn add_separator(&mut self) {
723         let prefix = if self.need_space_prefix() {
724             " ".to_owned()
725         } else {
726             "".to_owned()
727         };
728         self.result.push(ParsedMacroArg {
729             kind: MacroArgKind::Separator(self.buf.clone(), prefix),
730             span: mk_sp(self.lo, self.hi),
731         });
732         self.buf.clear();
733     }
734
735     fn add_other(&mut self) {
736         let prefix = if self.need_space_prefix() {
737             " ".to_owned()
738         } else {
739             "".to_owned()
740         };
741         self.result.push(ParsedMacroArg {
742             kind: MacroArgKind::Other(self.buf.clone(), prefix),
743             span: mk_sp(self.lo, self.hi),
744         });
745         self.buf.clear();
746     }
747
748     fn add_meta_variable(&mut self, iter: &mut Cursor) -> Option<()> {
749         match iter.next() {
750             Some(TokenTree::Token(sp, Token::Ident(ref ident, _))) => {
751                 self.result.push(ParsedMacroArg {
752                     kind: MacroArgKind::MetaVariable(*ident, self.buf.clone()),
753                     span: mk_sp(self.lo, sp.hi()),
754                 });
755
756                 self.buf.clear();
757                 self.is_meta_var = false;
758                 Some(())
759             }
760             _ => None,
761         }
762     }
763
764     fn add_delimited(&mut self, inner: Vec<ParsedMacroArg>, delim: DelimToken, span: Span) {
765         self.result.push(ParsedMacroArg {
766             kind: MacroArgKind::Delimited(delim, inner),
767             span,
768         });
769     }
770
771     // $($foo: expr),?
772     fn add_repeat(
773         &mut self,
774         inner: Vec<ParsedMacroArg>,
775         delim: DelimToken,
776         iter: &mut Cursor,
777         span: Span,
778     ) -> Option<()> {
779         let mut buffer = String::new();
780         let mut first = false;
781         let mut lo = span.lo();
782         let mut hi = span.hi();
783
784         // Parse '*', '+' or '?.
785         for tok in iter {
786             self.set_last_tok(&tok);
787             if first {
788                 first = false;
789                 lo = tok.span().lo();
790             }
791
792             match tok {
793                 TokenTree::Token(_, Token::BinOp(BinOpToken::Plus))
794                 | TokenTree::Token(_, Token::Question)
795                 | TokenTree::Token(_, Token::BinOp(BinOpToken::Star)) => {
796                     break;
797                 }
798                 TokenTree::Token(sp, ref t) => {
799                     buffer.push_str(&pprust::token_to_string(t));
800                     hi = sp.hi();
801                 }
802                 _ => return None,
803             }
804         }
805
806         // There could be some random stuff between ')' and '*', '+' or '?'.
807         let another = if buffer.trim().is_empty() {
808             None
809         } else {
810             Some(Box::new(ParsedMacroArg {
811                 kind: MacroArgKind::Other(buffer, "".to_owned()),
812                 span: mk_sp(lo, hi),
813             }))
814         };
815
816         self.result.push(ParsedMacroArg {
817             kind: MacroArgKind::Repeat(delim, inner, another, self.last_tok.clone()),
818             span: mk_sp(self.lo, self.hi),
819         });
820         Some(())
821     }
822
823     fn update_buffer(&mut self, lo: BytePos, t: &Token) {
824         if self.buf.is_empty() {
825             self.lo = lo;
826             self.start_tok = t.clone();
827         } else {
828             let needs_space = match next_space(&self.last_tok) {
829                 SpaceState::Ident => ident_like(t),
830                 SpaceState::Punctuation => !ident_like(t),
831                 SpaceState::Always => true,
832                 SpaceState::Never => false,
833             };
834             if force_space_before(t) || needs_space {
835                 self.buf.push(' ');
836             }
837         }
838
839         self.buf.push_str(&pprust::token_to_string(t));
840     }
841
842     fn need_space_prefix(&self) -> bool {
843         if self.result.is_empty() {
844             return false;
845         }
846
847         let last_arg = self.result.last().unwrap();
848         if let MacroArgKind::MetaVariable(..) = last_arg.kind {
849             if ident_like(&self.start_tok) {
850                 return true;
851             }
852             if self.start_tok == Token::Colon {
853                 return true;
854             }
855         }
856
857         if force_space_before(&self.start_tok) {
858             return true;
859         }
860
861         false
862     }
863
864     /// Returns a collection of parsed macro def's arguments.
865     pub fn parse(mut self, tokens: ThinTokenStream) -> Option<Vec<ParsedMacroArg>> {
866         let mut iter = (tokens.into(): TokenStream).trees();
867
868         while let Some(ref tok) = iter.next() {
869             match tok {
870                 TokenTree::Token(sp, Token::Dollar) => {
871                     // We always want to add a separator before meta variables.
872                     if !self.buf.is_empty() {
873                         self.add_separator();
874                     }
875
876                     // Start keeping the name of this metavariable in the buffer.
877                     self.is_meta_var = true;
878                     self.lo = sp.lo();
879                     self.start_tok = Token::Dollar;
880                 }
881                 TokenTree::Token(_, Token::Colon) if self.is_meta_var => {
882                     self.add_meta_variable(&mut iter)?;
883                 }
884                 TokenTree::Token(sp, ref t) => self.update_buffer(sp.lo(), t),
885                 TokenTree::Delimited(delimited_span, delimited) => {
886                     if !self.buf.is_empty() {
887                         if next_space(&self.last_tok) == SpaceState::Always {
888                             self.add_separator();
889                         } else {
890                             self.add_other();
891                         }
892                     }
893
894                     // Parse the stuff inside delimiters.
895                     let mut parser = MacroArgParser::new();
896                     parser.lo = delimited_span.open.lo();
897                     let delimited_arg = parser.parse(delimited.tts.clone())?;
898
899                     let span = delimited_span.entire();
900                     if self.is_meta_var {
901                         self.add_repeat(delimited_arg, delimited.delim, &mut iter, span)?;
902                         self.is_meta_var = false;
903                     } else {
904                         self.add_delimited(delimited_arg, delimited.delim, span);
905                     }
906                 }
907             }
908
909             self.set_last_tok(tok);
910         }
911
912         // We are left with some stuff in the buffer. Since there is nothing
913         // left to separate, add this as `Other`.
914         if !self.buf.is_empty() {
915             self.add_other();
916         }
917
918         Some(self.result)
919     }
920 }
921
922 fn wrap_macro_args(
923     context: &RewriteContext,
924     args: &[ParsedMacroArg],
925     shape: Shape,
926 ) -> Option<String> {
927     wrap_macro_args_inner(context, args, shape, false)
928         .or_else(|| wrap_macro_args_inner(context, args, shape, true))
929 }
930
931 fn wrap_macro_args_inner(
932     context: &RewriteContext,
933     args: &[ParsedMacroArg],
934     shape: Shape,
935     use_multiple_lines: bool,
936 ) -> Option<String> {
937     let mut result = String::with_capacity(128);
938     let mut iter = args.iter().peekable();
939     let indent_str = shape.indent.to_string_with_newline(context.config);
940
941     while let Some(ref arg) = iter.next() {
942         result.push_str(&arg.rewrite(context, shape, use_multiple_lines)?);
943
944         if use_multiple_lines
945             && (arg.kind.ends_with_space() || iter.peek().map_or(false, |a| a.kind.has_meta_var()))
946         {
947             if arg.kind.ends_with_space() {
948                 result.pop();
949             }
950             result.push_str(&indent_str);
951         } else if let Some(ref next_arg) = iter.peek() {
952             let space_before_dollar =
953                 !arg.kind.ends_with_space() && next_arg.kind.starts_with_dollar();
954             let space_before_brace = next_arg.kind.starts_with_brace();
955             if space_before_dollar || space_before_brace {
956                 result.push(' ');
957             }
958         }
959     }
960
961     if !use_multiple_lines && result.len() >= shape.width {
962         None
963     } else {
964         Some(result)
965     }
966 }
967
968 // This is a bit sketchy. The token rules probably need tweaking, but it works
969 // for some common cases. I hope the basic logic is sufficient. Note that the
970 // meaning of some tokens is a bit different here from usual Rust, e.g., `*`
971 // and `(`/`)` have special meaning.
972 //
973 // We always try and format on one line.
974 // FIXME: Use multi-line when every thing does not fit on one line.
975 fn format_macro_args(
976     context: &RewriteContext,
977     toks: ThinTokenStream,
978     shape: Shape,
979 ) -> Option<String> {
980     if !context.config.format_macro_matchers() {
981         let token_stream: TokenStream = toks.into();
982         let span = span_for_token_stream(token_stream);
983         return Some(match span {
984             Some(span) => context.snippet(span).to_owned(),
985             None => String::new(),
986         });
987     }
988     let parsed_args = MacroArgParser::new().parse(toks)?;
989     wrap_macro_args(context, &parsed_args, shape)
990 }
991
992 fn span_for_token_stream(token_stream: TokenStream) -> Option<Span> {
993     token_stream.trees().next().map(|tt| tt.span())
994 }
995
996 // We should insert a space if the next token is a:
997 #[derive(Copy, Clone, PartialEq)]
998 enum SpaceState {
999     Never,
1000     Punctuation,
1001     Ident, // Or ident/literal-like thing.
1002     Always,
1003 }
1004
1005 fn force_space_before(tok: &Token) -> bool {
1006     debug!("tok: force_space_before {:?}", tok);
1007
1008     match tok {
1009         Token::Eq
1010         | Token::Lt
1011         | Token::Le
1012         | Token::EqEq
1013         | Token::Ne
1014         | Token::Ge
1015         | Token::Gt
1016         | Token::AndAnd
1017         | Token::OrOr
1018         | Token::Not
1019         | Token::Tilde
1020         | Token::BinOpEq(_)
1021         | Token::At
1022         | Token::RArrow
1023         | Token::LArrow
1024         | Token::FatArrow
1025         | Token::BinOp(_)
1026         | Token::Pound
1027         | Token::Dollar => true,
1028         _ => false,
1029     }
1030 }
1031
1032 fn ident_like(tok: &Token) -> bool {
1033     match tok {
1034         Token::Ident(..) | Token::Literal(..) | Token::Lifetime(_) => true,
1035         _ => false,
1036     }
1037 }
1038
1039 fn next_space(tok: &Token) -> SpaceState {
1040     debug!("next_space: {:?}", tok);
1041
1042     match tok {
1043         Token::Not
1044         | Token::BinOp(BinOpToken::And)
1045         | Token::Tilde
1046         | Token::At
1047         | Token::Comma
1048         | Token::Dot
1049         | Token::DotDot
1050         | Token::DotDotDot
1051         | Token::DotDotEq
1052         | Token::DotEq
1053         | Token::Question => SpaceState::Punctuation,
1054
1055         Token::ModSep
1056         | Token::Pound
1057         | Token::Dollar
1058         | Token::OpenDelim(_)
1059         | Token::CloseDelim(_)
1060         | Token::Whitespace => SpaceState::Never,
1061
1062         Token::Literal(..) | Token::Ident(..) | Token::Lifetime(_) => SpaceState::Ident,
1063
1064         _ => SpaceState::Always,
1065     }
1066 }
1067
1068 /// Tries to convert a macro use into a short hand try expression. Returns None
1069 /// when the macro is not an instance of try! (or parsing the inner expression
1070 /// failed).
1071 pub fn convert_try_mac(mac: &ast::Mac, context: &RewriteContext) -> Option<ast::Expr> {
1072     if &format!("{}", mac.node.path) == "try" {
1073         let ts: TokenStream = mac.node.tts.clone().into();
1074         let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
1075
1076         Some(ast::Expr {
1077             id: ast::NodeId::new(0), // dummy value
1078             node: ast::ExprKind::Try(parser.parse_expr().ok()?),
1079             span: mac.span, // incorrect span, but shouldn't matter too much
1080             attrs: ThinVec::new(),
1081         })
1082     } else {
1083         None
1084     }
1085 }
1086
1087 fn macro_style(mac: &ast::Mac, context: &RewriteContext) -> DelimToken {
1088     let snippet = context.snippet(mac.span);
1089     let paren_pos = snippet.find_uncommented("(").unwrap_or(usize::max_value());
1090     let bracket_pos = snippet.find_uncommented("[").unwrap_or(usize::max_value());
1091     let brace_pos = snippet.find_uncommented("{").unwrap_or(usize::max_value());
1092
1093     if paren_pos < bracket_pos && paren_pos < brace_pos {
1094         DelimToken::Paren
1095     } else if bracket_pos < brace_pos {
1096         DelimToken::Bracket
1097     } else {
1098         DelimToken::Brace
1099     }
1100 }
1101
1102 /// Indent each line according to the specified `indent`.
1103 /// e.g.
1104 ///
1105 /// ```rust,ignore
1106 /// foo!{
1107 /// x,
1108 /// y,
1109 /// foo(
1110 ///     a,
1111 ///     b,
1112 ///     c,
1113 /// ),
1114 /// }
1115 /// ```
1116 ///
1117 /// will become
1118 ///
1119 /// ```rust,ignore
1120 /// foo!{
1121 ///     x,
1122 ///     y,
1123 ///     foo(
1124 ///         a,
1125 ///         b,
1126 ///         c,
1127 ///     ),
1128 /// }
1129 /// ```
1130 fn indent_macro_snippet(
1131     context: &RewriteContext,
1132     macro_str: &str,
1133     indent: Indent,
1134 ) -> Option<String> {
1135     let mut lines = LineClasses::new(macro_str);
1136     let first_line = lines.next().map(|(_, s)| s.trim_right().to_owned())?;
1137     let mut trimmed_lines = Vec::with_capacity(16);
1138
1139     let mut veto_trim = false;
1140     let min_prefix_space_width = lines
1141         .filter_map(|(kind, line)| {
1142             let mut trimmed = true;
1143             let prefix_space_width = if is_empty_line(&line) {
1144                 None
1145             } else {
1146                 Some(get_prefix_space_width(context, &line))
1147             };
1148
1149             let line = if veto_trim || (kind.is_string() && !line.ends_with('\\')) {
1150                 veto_trim = kind.is_string() && !line.ends_with('\\');
1151                 trimmed = false;
1152                 line
1153             } else {
1154                 line.trim().to_owned()
1155             };
1156             trimmed_lines.push((trimmed, line, prefix_space_width));
1157
1158             // when computing the minimum, do not consider lines within a string
1159             match kind {
1160                 FullCodeCharKind::InString | FullCodeCharKind::EndString => None,
1161                 _ => prefix_space_width,
1162             }
1163         })
1164         .min()?;
1165
1166     Some(
1167         first_line + "\n" + &trimmed_lines
1168             .iter()
1169             .map(
1170                 |&(trimmed, ref line, prefix_space_width)| match prefix_space_width {
1171                     _ if !trimmed => line.to_owned(),
1172                     Some(original_indent_width) => {
1173                         let new_indent_width = indent.width() + original_indent_width
1174                             .saturating_sub(min_prefix_space_width);
1175                         let new_indent = Indent::from_width(context.config, new_indent_width);
1176                         format!("{}{}", new_indent.to_string(context.config), line)
1177                     }
1178                     None => String::new(),
1179                 },
1180             )
1181             .collect::<Vec<_>>()
1182             .join("\n"),
1183     )
1184 }
1185
1186 fn get_prefix_space_width(context: &RewriteContext, s: &str) -> usize {
1187     let mut width = 0;
1188     for c in s.chars() {
1189         match c {
1190             ' ' => width += 1,
1191             '\t' => width += context.config.tab_spaces(),
1192             _ => return width,
1193         }
1194     }
1195     width
1196 }
1197
1198 fn is_empty_line(s: &str) -> bool {
1199     s.is_empty() || s.chars().all(char::is_whitespace)
1200 }
1201
1202 // A very simple parser that just parses a macros 2.0 definition into its branches.
1203 // Currently we do not attempt to parse any further than that.
1204 #[derive(new)]
1205 struct MacroParser {
1206     toks: Cursor,
1207 }
1208
1209 impl MacroParser {
1210     // (`(` ... `)` `=>` `{` ... `}`)*
1211     fn parse(&mut self) -> Option<Macro> {
1212         let mut branches = vec![];
1213         while self.toks.look_ahead(1).is_some() {
1214             branches.push(self.parse_branch()?);
1215         }
1216
1217         Some(Macro { branches })
1218     }
1219
1220     // `(` ... `)` `=>` `{` ... `}`
1221     fn parse_branch(&mut self) -> Option<MacroBranch> {
1222         let tok = self.toks.next()?;
1223         let (lo, args_paren_kind) = match tok {
1224             TokenTree::Token(..) => return None,
1225             TokenTree::Delimited(delimited_span, ref d) => (delimited_span.open.lo(), d.delim),
1226         };
1227         let args = tok.joint().into();
1228         match self.toks.next()? {
1229             TokenTree::Token(_, Token::FatArrow) => {}
1230             _ => return None,
1231         }
1232         let (mut hi, body, whole_body) = match self.toks.next()? {
1233             TokenTree::Token(..) => return None,
1234             TokenTree::Delimited(delimited_span, _) => {
1235                 let data = delimited_span.entire().data();
1236                 (
1237                     data.hi,
1238                     Span::new(data.lo + BytePos(1), data.hi - BytePos(1), data.ctxt),
1239                     delimited_span.entire(),
1240                 )
1241             }
1242         };
1243         if let Some(TokenTree::Token(sp, Token::Semi)) = self.toks.look_ahead(0) {
1244             self.toks.next();
1245             hi = sp.hi();
1246         }
1247         Some(MacroBranch {
1248             span: mk_sp(lo, hi),
1249             args_paren_kind,
1250             args,
1251             body,
1252             whole_body,
1253         })
1254     }
1255 }
1256
1257 // A parsed macros 2.0 macro definition.
1258 struct Macro {
1259     branches: Vec<MacroBranch>,
1260 }
1261
1262 // FIXME: it would be more efficient to use references to the token streams
1263 // rather than clone them, if we can make the borrowing work out.
1264 struct MacroBranch {
1265     span: Span,
1266     args_paren_kind: DelimToken,
1267     args: ThinTokenStream,
1268     body: Span,
1269     whole_body: Span,
1270 }
1271
1272 impl MacroBranch {
1273     fn rewrite(
1274         &self,
1275         context: &RewriteContext,
1276         shape: Shape,
1277         multi_branch_style: bool,
1278     ) -> Option<String> {
1279         // Only attempt to format function-like macros.
1280         if self.args_paren_kind != DelimToken::Paren {
1281             // FIXME(#1539): implement for non-sugared macros.
1282             return None;
1283         }
1284
1285         // 5 = " => {"
1286         let mut result = format_macro_args(context, self.args.clone(), shape.sub_width(5)?)?;
1287
1288         if multi_branch_style {
1289             result += " =>";
1290         }
1291
1292         if !context.config.format_macro_bodies() {
1293             result += " ";
1294             result += context.snippet(self.whole_body);
1295             return Some(result);
1296         }
1297
1298         // The macro body is the most interesting part. It might end up as various
1299         // AST nodes, but also has special variables (e.g, `$foo`) which can't be
1300         // parsed as regular Rust code (and note that these can be escaped using
1301         // `$$`). We'll try and format like an AST node, but we'll substitute
1302         // variables for new names with the same length first.
1303
1304         let old_body = context.snippet(self.body).trim();
1305         let (body_str, substs) = replace_names(old_body)?;
1306         let has_block_body = old_body.starts_with('{');
1307
1308         let mut config = context.config.clone();
1309         config.set().hide_parse_errors(true);
1310
1311         result += " {";
1312
1313         let body_indent = if has_block_body {
1314             shape.indent
1315         } else {
1316             shape.indent.block_indent(&config)
1317         };
1318         let new_width = config.max_width() - body_indent.width();
1319         config.set().max_width(new_width);
1320
1321         // First try to format as items, then as statements.
1322         let new_body = match ::format_snippet(&body_str, &config) {
1323             Some(new_body) => new_body,
1324             None => {
1325                 let new_width = new_width + config.tab_spaces();
1326                 config.set().max_width(new_width);
1327                 match ::format_code_block(&body_str, &config) {
1328                     Some(new_body) => new_body,
1329                     None => return None,
1330                 }
1331             }
1332         };
1333         let new_body = wrap_str(new_body, config.max_width(), shape)?;
1334
1335         // Indent the body since it is in a block.
1336         let indent_str = body_indent.to_string(&config);
1337         let mut new_body = LineClasses::new(new_body.trim_right())
1338             .fold(
1339                 (String::new(), true),
1340                 |(mut s, need_indent), (kind, ref l)| {
1341                     if !l.is_empty() && need_indent {
1342                         s += &indent_str;
1343                     }
1344                     (s + l + "\n", !kind.is_string() || l.ends_with('\\'))
1345                 },
1346             )
1347             .0;
1348
1349         // Undo our replacement of macro variables.
1350         // FIXME: this could be *much* more efficient.
1351         for (old, new) in &substs {
1352             if old_body.find(new).is_some() {
1353                 debug!("rewrite_macro_def: bailing matching variable: `{}`", new);
1354                 return None;
1355             }
1356             new_body = new_body.replace(new, old);
1357         }
1358
1359         if has_block_body {
1360             result += new_body.trim();
1361         } else if !new_body.is_empty() {
1362             result += "\n";
1363             result += &new_body;
1364             result += &shape.indent.to_string(&config);
1365         }
1366
1367         result += "}";
1368
1369         Some(result)
1370     }
1371 }
1372
1373 /// Format `lazy_static!` from https://crates.io/crates/lazy_static.
1374 ///
1375 /// # Expected syntax
1376 ///
1377 /// ```ignore
1378 /// lazy_static! {
1379 ///     [pub] static ref NAME_1: TYPE_1 = EXPR_1;
1380 ///     [pub] static ref NAME_2: TYPE_2 = EXPR_2;
1381 ///     ...
1382 ///     [pub] static ref NAME_N: TYPE_N = EXPR_N;
1383 /// }
1384 /// ```
1385 fn format_lazy_static(context: &RewriteContext, shape: Shape, ts: &TokenStream) -> Option<String> {
1386     let mut result = String::with_capacity(1024);
1387     let mut parser = new_parser_from_tts(context.parse_session, ts.trees().collect());
1388     let nested_shape = shape
1389         .block_indent(context.config.tab_spaces())
1390         .with_max_width(context.config);
1391
1392     result.push_str("lazy_static! {");
1393     result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
1394
1395     macro parse_or($method:ident $(,)* $($arg:expr),* $(,)*) {
1396         match parser.$method($($arg,)*) {
1397             Ok(val) => {
1398                 if parser.sess.span_diagnostic.has_errors() {
1399                     parser.sess.span_diagnostic.reset_err_count();
1400                     return None;
1401                 } else {
1402                     val
1403                 }
1404             }
1405             Err(mut err) => {
1406                 err.cancel();
1407                 parser.sess.span_diagnostic.reset_err_count();
1408                 return None;
1409             }
1410         }
1411     }
1412
1413     while parser.token != Token::Eof {
1414         // Parse a `lazy_static!` item.
1415         let vis = ::utils::format_visibility(context, &parse_or!(parse_visibility, false));
1416         parser.eat_keyword(symbol::keywords::Static);
1417         parser.eat_keyword(symbol::keywords::Ref);
1418         let id = parse_or!(parse_ident);
1419         parser.eat(&Token::Colon);
1420         let ty = parse_or!(parse_ty);
1421         parser.eat(&Token::Eq);
1422         let expr = parse_or!(parse_expr);
1423         parser.eat(&Token::Semi);
1424
1425         // Rewrite as a static item.
1426         let mut stmt = String::with_capacity(128);
1427         stmt.push_str(&format!(
1428             "{}static ref {}: {} =",
1429             vis,
1430             id,
1431             ty.rewrite(context, nested_shape)?
1432         ));
1433         result.push_str(&::expr::rewrite_assign_rhs(
1434             context,
1435             stmt,
1436             &*expr,
1437             nested_shape.sub_width(1)?,
1438         )?);
1439         result.push(';');
1440         if parser.token != Token::Eof {
1441             result.push_str(&nested_shape.indent.to_string_with_newline(context.config));
1442         }
1443     }
1444
1445     result.push_str(&shape.indent.to_string_with_newline(context.config));
1446     result.push('}');
1447
1448     Some(result)
1449 }
1450
1451 fn rewrite_macro_with_items(
1452     context: &RewriteContext,
1453     items: &[MacroArg],
1454     macro_name: &str,
1455     shape: Shape,
1456     style: DelimToken,
1457     position: MacroPosition,
1458     span: Span,
1459 ) -> Option<String> {
1460     let (opener, closer) = match style {
1461         DelimToken::Paren => ("(", ")"),
1462         DelimToken::Bracket => ("[", "]"),
1463         DelimToken::Brace => (" {", "}"),
1464         _ => return None,
1465     };
1466     let trailing_semicolon = match style {
1467         DelimToken::Paren | DelimToken::Bracket if position == MacroPosition::Item => ";",
1468         _ => "",
1469     };
1470
1471     let mut visitor = FmtVisitor::from_context(context);
1472     visitor.block_indent = shape.indent.block_indent(context.config);
1473     visitor.last_pos = context.snippet_provider.span_after(span, opener.trim());
1474     for item in items {
1475         let item = match item {
1476             MacroArg::Item(item) => item,
1477             _ => return None,
1478         };
1479         visitor.visit_item(&item);
1480     }
1481
1482     let mut result = String::with_capacity(256);
1483     result.push_str(&macro_name);
1484     result.push_str(opener);
1485     result.push_str(&visitor.block_indent.to_string_with_newline(context.config));
1486     result.push_str(visitor.buffer.trim());
1487     result.push_str(&shape.indent.to_string_with_newline(context.config));
1488     result.push_str(closer);
1489     result.push_str(trailing_semicolon);
1490     return Some(result);
1491 }