]> git.lizzy.rs Git - rust.git/blob - src/visitor.rs
Comments WIP
[rust.git] / src / visitor.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 use std::cmp;
12
13 use syntax::{ast, visit};
14 use syntax::attr::HasAttrs;
15 use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
16 use syntax::parse::ParseSess;
17
18 use codemap::{LineRangeUtils, SpanUtils};
19 use comment::{combine_strs_with_missing_comments, contains_comment, CodeCharKind,
20               CommentCodeSlices, FindUncommented};
21 use comment::rewrite_comment;
22 use config::{BraceStyle, Config};
23 use expr::rewrite_literal;
24 use items::{format_impl, format_trait, format_trait_alias, rewrite_associated_impl_type,
25             rewrite_associated_type, rewrite_type_alias, FnSig, StaticParts, StructParts};
26 use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorPlace,
27             SeparatorTactic};
28 use macros::{rewrite_macro, rewrite_macro_def, MacroPosition};
29 use regex::Regex;
30 use rewrite::{Rewrite, RewriteContext};
31 use shape::{Indent, Shape};
32 use spanned::Spanned;
33 use utils::{self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec};
34
35 /// Returns attributes that are within `outer_span`.
36 pub fn filter_inline_attrs(attrs: &[ast::Attribute], outer_span: Span) -> Vec<ast::Attribute> {
37     attrs
38         .iter()
39         .filter(|a| outer_span.lo() <= a.span.lo() && a.span.hi() <= outer_span.hi())
40         .cloned()
41         .collect()
42 }
43
44 /// Returns true for `mod foo;`, false for `mod foo { .. }`.
45 fn is_mod_decl(item: &ast::Item) -> bool {
46     match item.node {
47         ast::ItemKind::Mod(ref m) => m.inner.hi() != item.span.hi(),
48         _ => false,
49     }
50 }
51
52 /// Returns true for `mod foo;` without any inline attributes.
53 /// We cannot reorder modules with attributes because doing so can break the code.
54 /// e.g. `#[macro_use]`.
55 fn is_mod_decl_without_attr(item: &ast::Item) -> bool {
56     is_mod_decl(item) && filter_inline_attrs(&item.attrs, item.span()).is_empty()
57 }
58
59 fn is_use_item(item: &ast::Item) -> bool {
60     match item.node {
61         ast::ItemKind::Use(_) => true,
62         _ => false,
63     }
64 }
65
66 fn is_extern_crate(item: &ast::Item) -> bool {
67     match item.node {
68         ast::ItemKind::ExternCrate(..) => true,
69         _ => false,
70     }
71 }
72
73 /// Creates a string slice corresponding to the specified span.
74 pub struct SnippetProvider<'a> {
75     /// A pointer to the content of the file we are formatting.
76     big_snippet: &'a str,
77     /// A position of the start of `big_snippet`, used as an offset.
78     start_pos: usize,
79 }
80
81 impl<'a> SnippetProvider<'a> {
82     pub fn span_to_snippet(&self, span: Span) -> Option<&str> {
83         let start_index = span.lo().to_usize().checked_sub(self.start_pos)?;
84         let end_index = span.hi().to_usize().checked_sub(self.start_pos)?;
85         Some(&self.big_snippet[start_index..end_index])
86     }
87
88     pub fn new(start_pos: BytePos, big_snippet: &'a str) -> Self {
89         let start_pos = start_pos.to_usize();
90         SnippetProvider {
91             big_snippet,
92             start_pos,
93         }
94     }
95 }
96
97 pub struct FmtVisitor<'a> {
98     pub parse_session: &'a ParseSess,
99     pub codemap: &'a CodeMap,
100     pub buffer: String,
101     pub last_pos: BytePos,
102     // FIXME: use an RAII util or closure for indenting
103     pub block_indent: Indent,
104     pub config: &'a Config,
105     pub is_if_else_block: bool,
106     pub snippet_provider: &'a SnippetProvider<'a>,
107     pub line_number: usize,
108     pub skipped_range: Vec<(usize, usize)>,
109 }
110
111 impl<'b, 'a: 'b> FmtVisitor<'a> {
112     pub fn shape(&self) -> Shape {
113         Shape::indented(self.block_indent, self.config)
114     }
115
116     fn visit_stmt(&mut self, stmt: &ast::Stmt) {
117         debug!(
118             "visit_stmt: {:?} {:?}",
119             self.codemap.lookup_char_pos(stmt.span.lo()),
120             self.codemap.lookup_char_pos(stmt.span.hi())
121         );
122
123         match stmt.node {
124             ast::StmtKind::Item(ref item) => {
125                 self.visit_item(item);
126             }
127             ast::StmtKind::Local(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
128                 if contains_skip(get_attrs_from_stmt(stmt)) {
129                     self.push_skipped_with_span(stmt.span());
130                 } else {
131                     let rewrite = stmt.rewrite(&self.get_context(), self.shape());
132                     self.push_rewrite(stmt.span(), rewrite)
133                 }
134             }
135             ast::StmtKind::Mac(ref mac) => {
136                 let (ref mac, _macro_style, ref attrs) = **mac;
137                 if self.visit_attrs(attrs, ast::AttrStyle::Outer) {
138                     self.push_skipped_with_span(stmt.span());
139                 } else {
140                     self.visit_mac(mac, None, MacroPosition::Statement);
141                 }
142                 self.format_missing(stmt.span.hi());
143             }
144         }
145     }
146
147     pub fn visit_block(
148         &mut self,
149         b: &ast::Block,
150         inner_attrs: Option<&[ast::Attribute]>,
151         has_braces: bool,
152     ) {
153         debug!(
154             "visit_block: {:?} {:?}",
155             self.codemap.lookup_char_pos(b.span.lo()),
156             self.codemap.lookup_char_pos(b.span.hi())
157         );
158
159         // Check if this block has braces.
160         let brace_compensation = BytePos(if has_braces { 1 } else { 0 });
161
162         self.last_pos = self.last_pos + brace_compensation;
163         self.block_indent = self.block_indent.block_indent(self.config);
164         self.push_str("{");
165
166         if self.config.remove_blank_lines_at_start_or_end_of_block() {
167             if let Some(first_stmt) = b.stmts.first() {
168                 let attr_lo = inner_attrs
169                     .and_then(|attrs| inner_attributes(attrs).first().map(|attr| attr.span.lo()))
170                     .or_else(|| {
171                         // Attributes for an item in a statement position
172                         // do not belong to the statement. (rust-lang/rust#34459)
173                         if let ast::StmtKind::Item(ref item) = first_stmt.node {
174                             item.attrs.first()
175                         } else {
176                             first_stmt.attrs().first()
177                         }.and_then(|attr| {
178                             // Some stmts can have embedded attributes.
179                             // e.g. `match { #![attr] ... }`
180                             let attr_lo = attr.span.lo();
181                             if attr_lo < first_stmt.span.lo() {
182                                 Some(attr_lo)
183                             } else {
184                                 None
185                             }
186                         })
187                     });
188
189                 let snippet = self.snippet(mk_sp(
190                     self.last_pos,
191                     attr_lo.unwrap_or(first_stmt.span.lo()),
192                 ));
193                 let len = CommentCodeSlices::new(snippet)
194                     .nth(0)
195                     .and_then(|(kind, _, s)| {
196                         if kind == CodeCharKind::Normal {
197                             s.rfind('\n')
198                         } else {
199                             None
200                         }
201                     });
202                 if let Some(len) = len {
203                     self.last_pos = self.last_pos + BytePos::from_usize(len);
204                 }
205             }
206         }
207
208         // Format inner attributes if available.
209         let skip_rewrite = if let Some(attrs) = inner_attrs {
210             self.visit_attrs(attrs, ast::AttrStyle::Inner)
211         } else {
212             false
213         };
214
215         if skip_rewrite {
216             self.push_rewrite(b.span, None);
217             self.close_block(false);
218             self.last_pos = source!(self, b.span).hi();
219             return;
220         }
221
222         self.walk_block_stmts(b);
223
224         if !b.stmts.is_empty() {
225             if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
226                 if utils::semicolon_for_expr(&self.get_context(), expr) {
227                     self.push_str(";");
228                 }
229             }
230         }
231
232         let mut remove_len = BytePos(0);
233         if self.config.remove_blank_lines_at_start_or_end_of_block() {
234             if let Some(stmt) = b.stmts.last() {
235                 let snippet = self.snippet(mk_sp(
236                     stmt.span.hi(),
237                     source!(self, b.span).hi() - brace_compensation,
238                 ));
239                 let len = CommentCodeSlices::new(snippet)
240                     .last()
241                     .and_then(|(kind, _, s)| {
242                         if kind == CodeCharKind::Normal && s.trim().is_empty() {
243                             Some(s.len())
244                         } else {
245                             None
246                         }
247                     });
248                 if let Some(len) = len {
249                     remove_len = BytePos::from_usize(len);
250                 }
251             }
252         }
253
254         let unindent_comment = (self.is_if_else_block && !b.stmts.is_empty()) && {
255             let end_pos = source!(self, b.span).hi() - brace_compensation - remove_len;
256             let snippet = self.snippet(mk_sp(self.last_pos, end_pos));
257             snippet.contains("//") || snippet.contains("/*")
258         };
259         // FIXME: we should compress any newlines here to just one
260         if unindent_comment {
261             self.block_indent = self.block_indent.block_unindent(self.config);
262         }
263         self.format_missing_with_indent(
264             source!(self, b.span).hi() - brace_compensation - remove_len,
265         );
266         if unindent_comment {
267             self.block_indent = self.block_indent.block_indent(self.config);
268         }
269         self.close_block(unindent_comment);
270         self.last_pos = source!(self, b.span).hi();
271     }
272
273     // FIXME: this is a terrible hack to indent the comments between the last
274     // item in the block and the closing brace to the block's level.
275     // The closing brace itself, however, should be indented at a shallower
276     // level.
277     fn close_block(&mut self, unindent_comment: bool) {
278         let total_len = self.buffer.len();
279         let chars_too_many = if unindent_comment {
280             0
281         } else if self.config.hard_tabs() {
282             1
283         } else {
284             self.config.tab_spaces()
285         };
286         self.buffer.truncate(total_len - chars_too_many);
287         self.push_str("}");
288         self.block_indent = self.block_indent.block_unindent(self.config);
289     }
290
291     // Note that this only gets called for function definitions. Required methods
292     // on traits do not get handled here.
293     fn visit_fn(
294         &mut self,
295         fk: visit::FnKind,
296         generics: &ast::Generics,
297         fd: &ast::FnDecl,
298         s: Span,
299         defaultness: ast::Defaultness,
300         inner_attrs: Option<&[ast::Attribute]>,
301     ) {
302         let indent = self.block_indent;
303         let block;
304         let rewrite = match fk {
305             visit::FnKind::ItemFn(ident, _, _, _, _, b) | visit::FnKind::Method(ident, _, _, b) => {
306                 block = b;
307                 self.rewrite_fn(
308                     indent,
309                     ident,
310                     &FnSig::from_fn_kind(&fk, generics, fd, defaultness),
311                     mk_sp(s.lo(), b.span.lo()),
312                     b,
313                 )
314             }
315             visit::FnKind::Closure(_) => unreachable!(),
316         };
317
318         if let Some(fn_str) = rewrite {
319             self.format_missing_with_indent(source!(self, s).lo());
320             self.push_str(&fn_str);
321             if let Some(c) = fn_str.chars().last() {
322                 if c == '}' {
323                     self.last_pos = source!(self, block.span).hi();
324                     return;
325                 }
326             }
327         } else {
328             self.format_missing(source!(self, block.span).lo());
329         }
330
331         self.last_pos = source!(self, block.span).lo();
332         self.visit_block(block, inner_attrs, true)
333     }
334
335     pub fn visit_item(&mut self, item: &ast::Item) {
336         skip_out_of_file_lines_range_visitor!(self, item.span);
337
338         // This is where we bail out if there is a skip attribute. This is only
339         // complex in the module case. It is complex because the module could be
340         // in a separate file and there might be attributes in both files, but
341         // the AST lumps them all together.
342         let filtered_attrs;
343         let mut attrs = &item.attrs;
344         match item.node {
345             // Module is inline, in this case we treat it like any other item.
346             _ if !is_mod_decl(item) => {
347                 if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
348                     self.push_skipped_with_span(item.span());
349                     return;
350                 }
351             }
352             // Module is not inline, but should be skipped.
353             ast::ItemKind::Mod(..) if contains_skip(&item.attrs) => {
354                 return;
355             }
356             // Module is not inline and should not be skipped. We want
357             // to process only the attributes in the current file.
358             ast::ItemKind::Mod(..) => {
359                 filtered_attrs = filter_inline_attrs(&item.attrs, item.span());
360                 // Assert because if we should skip it should be caught by
361                 // the above case.
362                 assert!(!self.visit_attrs(&filtered_attrs, ast::AttrStyle::Outer));
363                 attrs = &filtered_attrs;
364             }
365             _ => {
366                 if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
367                     self.push_skipped_with_span(item.span());
368                     return;
369                 }
370             }
371         }
372
373         match item.node {
374             ast::ItemKind::Use(ref tree) => self.format_import(item, tree),
375             ast::ItemKind::Impl(..) => {
376                 let snippet = self.snippet(item.span);
377                 let where_span_end = snippet
378                     .find_uncommented("{")
379                     .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo());
380                 let rw = format_impl(&self.get_context(), item, self.block_indent, where_span_end);
381                 self.push_rewrite(item.span, rw);
382             }
383             ast::ItemKind::Trait(..) => {
384                 let rw = format_trait(&self.get_context(), item, self.block_indent);
385                 self.push_rewrite(item.span, rw);
386             }
387             ast::ItemKind::TraitAlias(ref generics, ref ty_param_bounds) => {
388                 let shape = Shape::indented(self.block_indent, self.config);
389                 let rw = format_trait_alias(
390                     &self.get_context(),
391                     item.ident,
392                     generics,
393                     ty_param_bounds,
394                     shape,
395                 );
396                 self.push_rewrite(item.span, rw);
397             }
398             ast::ItemKind::ExternCrate(_) => {
399                 let rw = rewrite_extern_crate(&self.get_context(), item);
400                 self.push_rewrite(item.span, rw);
401             }
402             ast::ItemKind::Struct(..) | ast::ItemKind::Union(..) => {
403                 self.visit_struct(&StructParts::from_item(item));
404             }
405             ast::ItemKind::Enum(ref def, ref generics) => {
406                 self.format_missing_with_indent(source!(self, item.span).lo());
407                 self.visit_enum(item.ident, &item.vis, def, generics, item.span);
408                 self.last_pos = source!(self, item.span).hi();
409             }
410             ast::ItemKind::Mod(ref module) => {
411                 let is_inline = !is_mod_decl(item);
412                 self.format_missing_with_indent(source!(self, item.span).lo());
413                 self.format_mod(module, &item.vis, item.span, item.ident, attrs, is_inline);
414             }
415             ast::ItemKind::Mac(ref mac) => {
416                 self.visit_mac(mac, Some(item.ident), MacroPosition::Item);
417             }
418             ast::ItemKind::ForeignMod(ref foreign_mod) => {
419                 self.format_missing_with_indent(source!(self, item.span).lo());
420                 self.format_foreign_mod(foreign_mod, item.span);
421             }
422             ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
423                 self.visit_static(&StaticParts::from_item(item));
424             }
425             ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
426                 self.visit_fn(
427                     visit::FnKind::ItemFn(item.ident, unsafety, constness, abi, &item.vis, body),
428                     generics,
429                     decl,
430                     item.span,
431                     ast::Defaultness::Final,
432                     Some(&item.attrs),
433                 )
434             }
435             ast::ItemKind::Ty(ref ty, ref generics) => {
436                 let rewrite = rewrite_type_alias(
437                     &self.get_context(),
438                     self.block_indent,
439                     item.ident,
440                     ty,
441                     generics,
442                     &item.vis,
443                     item.span,
444                 );
445                 self.push_rewrite(item.span, rewrite);
446             }
447             ast::ItemKind::GlobalAsm(..) => {
448                 let snippet = Some(self.snippet(item.span).to_owned());
449                 self.push_rewrite(item.span, snippet);
450             }
451             ast::ItemKind::MacroDef(ref def) => {
452                 let rewrite = rewrite_macro_def(
453                     &self.get_context(),
454                     self.shape(),
455                     self.block_indent,
456                     def,
457                     item.ident,
458                     &item.vis,
459                     item.span,
460                 );
461                 self.push_rewrite(item.span, rewrite);
462             }
463         }
464     }
465
466     pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
467         skip_out_of_file_lines_range_visitor!(self, ti.span);
468
469         if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
470             self.push_skipped_with_span(ti.span());
471             return;
472         }
473
474         match ti.node {
475             ast::TraitItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
476             ast::TraitItemKind::Method(ref sig, None) => {
477                 let indent = self.block_indent;
478                 let rewrite =
479                     self.rewrite_required_fn(indent, ti.ident, sig, &ti.generics, ti.span);
480                 self.push_rewrite(ti.span, rewrite);
481             }
482             ast::TraitItemKind::Method(ref sig, Some(ref body)) => {
483                 self.visit_fn(
484                     visit::FnKind::Method(ti.ident, sig, None, body),
485                     &ti.generics,
486                     &sig.decl,
487                     ti.span,
488                     ast::Defaultness::Final,
489                     Some(&ti.attrs),
490                 );
491             }
492             ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => {
493                 let rewrite = rewrite_associated_type(
494                     ti.ident,
495                     type_default.as_ref(),
496                     Some(type_param_bounds),
497                     &self.get_context(),
498                     self.block_indent,
499                 );
500                 self.push_rewrite(ti.span, rewrite);
501             }
502             ast::TraitItemKind::Macro(ref mac) => {
503                 self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
504             }
505         }
506     }
507
508     pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
509         skip_out_of_file_lines_range_visitor!(self, ii.span);
510
511         if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
512             self.push_skipped_with_span(ii.span());
513             return;
514         }
515
516         match ii.node {
517             ast::ImplItemKind::Method(ref sig, ref body) => {
518                 self.visit_fn(
519                     visit::FnKind::Method(ii.ident, sig, Some(&ii.vis), body),
520                     &ii.generics,
521                     &sig.decl,
522                     ii.span,
523                     ii.defaultness,
524                     Some(&ii.attrs),
525                 );
526             }
527             ast::ImplItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
528             ast::ImplItemKind::Type(ref ty) => {
529                 let rewrite = rewrite_associated_impl_type(
530                     ii.ident,
531                     ii.defaultness,
532                     Some(ty),
533                     None,
534                     &self.get_context(),
535                     self.block_indent,
536                 );
537                 self.push_rewrite(ii.span, rewrite);
538             }
539             ast::ImplItemKind::Macro(ref mac) => {
540                 self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
541             }
542         }
543     }
544
545     fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPosition) {
546         skip_out_of_file_lines_range_visitor!(self, mac.span);
547
548         // 1 = ;
549         let shape = self.shape().sub_width(1).unwrap();
550         let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
551         self.push_rewrite(mac.span, rewrite);
552     }
553
554     pub fn push_str(&mut self, s: &str) {
555         self.line_number += count_newlines(s);
556         self.buffer.push_str(s);
557     }
558
559     fn push_rewrite_inner(&mut self, span: Span, rewrite: Option<String>) {
560         if let Some(ref s) = rewrite {
561             self.push_str(s);
562         } else {
563             let snippet = self.snippet(span);
564             self.push_str(snippet);
565         }
566         self.last_pos = source!(self, span).hi();
567     }
568
569     pub fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
570         self.format_missing_with_indent(source!(self, span).lo());
571         self.push_rewrite_inner(span, rewrite);
572     }
573
574     pub fn push_skipped_with_span(&mut self, span: Span) {
575         self.format_missing_with_indent(source!(self, span).lo());
576         let lo = self.line_number + 1;
577         self.push_rewrite_inner(span, None);
578         let hi = self.line_number + 1;
579         self.skipped_range.push((lo, hi));
580     }
581
582     pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
583         FmtVisitor::from_codemap(ctx.parse_session, ctx.config, ctx.snippet_provider)
584     }
585
586     pub fn from_codemap(
587         parse_session: &'a ParseSess,
588         config: &'a Config,
589         snippet_provider: &'a SnippetProvider,
590     ) -> FmtVisitor<'a> {
591         FmtVisitor {
592             parse_session,
593             codemap: parse_session.codemap(),
594             buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
595             last_pos: BytePos(0),
596             block_indent: Indent::empty(),
597             config,
598             is_if_else_block: false,
599             snippet_provider,
600             line_number: 0,
601             skipped_range: vec![],
602         }
603     }
604
605     pub fn opt_snippet(&'b self, span: Span) -> Option<&'a str> {
606         self.snippet_provider.span_to_snippet(span)
607     }
608
609     pub fn snippet(&'b self, span: Span) -> &'a str {
610         self.opt_snippet(span).unwrap()
611     }
612
613     // Returns true if we should skip the following item.
614     pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
615         if contains_skip(attrs) {
616             return true;
617         }
618
619         let attrs: Vec<_> = attrs.iter().filter(|a| a.style == style).cloned().collect();
620         if attrs.is_empty() {
621             return false;
622         }
623
624         let rewrite = attrs.rewrite(&self.get_context(), self.shape());
625         let span = mk_sp(attrs[0].span.lo(), attrs[attrs.len() - 1].span.hi());
626         self.push_rewrite(span, rewrite);
627
628         false
629     }
630
631     fn reorder_items<F>(&mut self, items_left: &[&ast::Item], is_item: &F, in_group: bool) -> usize
632     where
633         F: Fn(&ast::Item) -> bool,
634     {
635         let mut last = self.codemap.lookup_line_range(items_left[0].span());
636         let item_length = items_left
637             .iter()
638             .take_while(|ppi| {
639                 is_item(&***ppi) && (!in_group || {
640                     let current = self.codemap.lookup_line_range(ppi.span());
641                     let in_same_group = current.lo < last.hi + 2;
642                     last = current;
643                     in_same_group
644                 })
645             })
646             .count();
647         let items = &items_left[..item_length];
648
649         let at_least_one_in_file_lines = items
650             .iter()
651             .any(|item| !out_of_file_lines_range!(self, item.span));
652
653         if at_least_one_in_file_lines {
654             self.format_imports(items);
655         } else {
656             for item in items {
657                 self.push_rewrite(item.span, None);
658             }
659         }
660
661         item_length
662     }
663
664     fn walk_items(&mut self, mut items_left: &[&ast::Item]) {
665         macro try_reorder_items_with($reorder: ident, $in_group: ident, $pred: ident) {
666             if self.config.$reorder() && $pred(&*items_left[0]) {
667                 let used_items_len =
668                     self.reorder_items(items_left, &$pred, self.config.$in_group());
669                 let (_, rest) = items_left.split_at(used_items_len);
670                 items_left = rest;
671                 continue;
672             }
673         }
674
675         while !items_left.is_empty() {
676             // If the next item is a `use`, `extern crate` or `mod`, then extract it and any
677             // subsequent items that have the same item kind to be reordered within
678             // `format_imports`. Otherwise, just format the next item for output.
679             {
680                 try_reorder_items_with!(reorder_imports, reorder_imports_in_group, is_use_item);
681                 try_reorder_items_with!(
682                     reorder_extern_crates,
683                     reorder_extern_crates_in_group,
684                     is_extern_crate
685                 );
686                 try_reorder_items_with!(reorder_modules, reorder_modules, is_mod_decl_without_attr);
687             }
688             // Reaching here means items were not reordered. There must be at least
689             // one item left in `items_left`, so calling `unwrap()` here is safe.
690             let (item, rest) = items_left.split_first().unwrap();
691             self.visit_item(item);
692             items_left = rest;
693         }
694     }
695
696     fn walk_mod_items(&mut self, m: &ast::Mod) {
697         self.walk_items(&ptr_vec_to_ref_vec(&m.items));
698     }
699
700     fn walk_stmts(&mut self, stmts: &[ast::Stmt]) {
701         fn to_stmt_item(stmt: &ast::Stmt) -> Option<&ast::Item> {
702             match stmt.node {
703                 ast::StmtKind::Item(ref item) => Some(&**item),
704                 _ => None,
705             }
706         }
707
708         if stmts.is_empty() {
709             return;
710         }
711
712         // Extract leading `use ...;`.
713         let items: Vec<_> = stmts
714             .iter()
715             .take_while(|stmt| to_stmt_item(stmt).map_or(false, is_use_item))
716             .filter_map(|stmt| to_stmt_item(stmt))
717             .collect();
718
719         if items.is_empty() {
720             self.visit_stmt(&stmts[0]);
721             self.walk_stmts(&stmts[1..]);
722         } else {
723             self.walk_items(&items);
724             self.walk_stmts(&stmts[items.len()..]);
725         }
726     }
727
728     fn walk_block_stmts(&mut self, b: &ast::Block) {
729         self.walk_stmts(&b.stmts)
730     }
731
732     fn format_mod(
733         &mut self,
734         m: &ast::Mod,
735         vis: &ast::Visibility,
736         s: Span,
737         ident: ast::Ident,
738         attrs: &[ast::Attribute],
739         is_internal: bool,
740     ) {
741         self.push_str(&*utils::format_visibility(vis));
742         self.push_str("mod ");
743         self.push_str(&ident.to_string());
744
745         if is_internal {
746             match self.config.brace_style() {
747                 BraceStyle::AlwaysNextLine => {
748                     let sep_str = format!("\n{}{{", self.block_indent.to_string(self.config));
749                     self.push_str(&sep_str);
750                 }
751                 _ => self.push_str(" {"),
752             }
753             // Hackery to account for the closing }.
754             let mod_lo = self.codemap.span_after(source!(self, s), "{");
755             let body_snippet =
756                 self.snippet(mk_sp(mod_lo, source!(self, m.inner).hi() - BytePos(1)));
757             let body_snippet = body_snippet.trim();
758             if body_snippet.is_empty() {
759                 self.push_str("}");
760             } else {
761                 self.last_pos = mod_lo;
762                 self.block_indent = self.block_indent.block_indent(self.config);
763                 self.visit_attrs(attrs, ast::AttrStyle::Inner);
764                 self.walk_mod_items(m);
765                 self.format_missing_with_indent(source!(self, m.inner).hi() - BytePos(1));
766                 self.close_block(false);
767             }
768             self.last_pos = source!(self, m.inner).hi();
769         } else {
770             self.push_str(";");
771             self.last_pos = source!(self, s).hi();
772         }
773     }
774
775     pub fn format_separate_mod(&mut self, m: &ast::Mod, filemap: &codemap::FileMap) {
776         self.block_indent = Indent::empty();
777         self.walk_mod_items(m);
778         self.format_missing_with_indent(filemap.end_pos);
779     }
780
781     pub fn skip_empty_lines(&mut self, end_pos: BytePos) {
782         while let Some(pos) = self.codemap
783             .opt_span_after(mk_sp(self.last_pos, end_pos), "\n")
784         {
785             if let Some(snippet) = self.opt_snippet(mk_sp(self.last_pos, pos)) {
786                 if snippet.trim().is_empty() {
787                     self.last_pos = pos;
788                 } else {
789                     return;
790                 }
791             }
792         }
793     }
794
795     pub fn get_context(&self) -> RewriteContext {
796         RewriteContext {
797             parse_session: self.parse_session,
798             codemap: self.codemap,
799             config: self.config,
800             inside_macro: false,
801             use_block: false,
802             is_if_else_block: false,
803             force_one_line_chain: false,
804             snippet_provider: self.snippet_provider,
805         }
806     }
807 }
808
809 impl Rewrite for ast::NestedMetaItem {
810     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
811         match self.node {
812             ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
813             ast::NestedMetaItemKind::Literal(ref l) => rewrite_literal(context, l, shape),
814         }
815     }
816 }
817
818 impl Rewrite for ast::MetaItem {
819     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
820         Some(match self.node {
821             ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
822             ast::MetaItemKind::List(ref list) => {
823                 let name = self.name.as_str();
824                 // 1 = `(`, 2 = `]` and `)`
825                 let item_shape = shape
826                     .visual_indent(0)
827                     .shrink_left(name.len() + 1)
828                     .and_then(|s| s.sub_width(2))?;
829                 let items = itemize_list(
830                     context.codemap,
831                     list.iter(),
832                     ")",
833                     ",",
834                     |nested_meta_item| nested_meta_item.span.lo(),
835                     |nested_meta_item| nested_meta_item.span.hi(),
836                     |nested_meta_item| nested_meta_item.rewrite(context, item_shape),
837                     self.span.lo(),
838                     self.span.hi(),
839                     false,
840                 );
841                 let item_vec = items.collect::<Vec<_>>();
842                 let fmt = ListFormatting {
843                     tactic: DefinitiveListTactic::Mixed,
844                     separator: ",",
845                     trailing_separator: SeparatorTactic::Never,
846                     separator_place: SeparatorPlace::Back,
847                     shape: item_shape,
848                     ends_with_newline: false,
849                     preserve_newline: false,
850                     config: context.config,
851                 };
852                 format!("{}({})", name, write_list(&item_vec, &fmt)?)
853             }
854             ast::MetaItemKind::NameValue(ref literal) => {
855                 let name = self.name.as_str();
856                 // 3 = ` = `
857                 let lit_shape = shape.shrink_left(name.len() + 3)?;
858                 let value = rewrite_literal(context, literal, lit_shape)?;
859                 format!("{} = {}", name, value)
860             }
861         })
862     }
863 }
864
865 impl Rewrite for ast::Attribute {
866     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
867         let prefix = match self.style {
868             ast::AttrStyle::Inner => "#!",
869             ast::AttrStyle::Outer => "#",
870         };
871         let snippet = context.snippet(self.span);
872         if self.is_sugared_doc {
873             let doc_shape = Shape {
874                 width: cmp::min(shape.width, context.config.comment_width())
875                     .checked_sub(shape.indent.width())
876                     .unwrap_or(0),
877                 ..shape
878             };
879             rewrite_comment(snippet, false, doc_shape, context.config)
880         } else {
881             if contains_comment(snippet) {
882                 return Some(snippet.to_owned());
883             }
884             // 1 = `[`
885             let shape = shape.offset_left(prefix.len() + 1)?;
886             self.meta()?
887                 .rewrite(context, shape)
888                 .map(|rw| format!("{}[{}]", prefix, rw))
889         }
890     }
891 }
892
893 /// Returns the first group of attributes that fills the given predicate.
894 /// We consider two doc comments are in different group if they are separated by normal comments.
895 fn take_while_with_pred<'a, P>(
896     context: &RewriteContext,
897     attrs: &'a [ast::Attribute],
898     pred: P,
899 ) -> &'a [ast::Attribute]
900 where
901     P: Fn(&ast::Attribute) -> bool,
902 {
903     let mut last_index = 0;
904     let mut iter = attrs.iter().enumerate().peekable();
905     while let Some((i, attr)) = iter.next() {
906         if !pred(attr) {
907             break;
908         }
909         if let Some(&(_, next_attr)) = iter.peek() {
910             // Extract comments between two attributes.
911             let span_between_attr = mk_sp(attr.span.hi(), next_attr.span.lo());
912             let snippet = context.snippet(span_between_attr);
913             if count_newlines(snippet) >= 2 || snippet.contains('/') {
914                 break;
915             }
916         }
917         last_index = i;
918     }
919     if last_index == 0 {
920         &[]
921     } else {
922         &attrs[..last_index + 1]
923     }
924 }
925
926 fn rewrite_first_group_attrs(
927     context: &RewriteContext,
928     attrs: &[ast::Attribute],
929     shape: Shape,
930 ) -> Option<(usize, String)> {
931     if attrs.is_empty() {
932         return Some((0, String::new()));
933     }
934     // Rewrite doc comments
935     let sugared_docs = take_while_with_pred(context, attrs, |a| a.is_sugared_doc);
936     if !sugared_docs.is_empty() {
937         let snippet = sugared_docs
938             .iter()
939             .map(|a| context.snippet(a.span))
940             .collect::<Vec<_>>()
941             .join("\n");
942         return Some((
943             sugared_docs.len(),
944             rewrite_comment(&snippet, false, shape, context.config)?,
945         ));
946     }
947     // Rewrite `#[derive(..)]`s.
948     if context.config.merge_derives() {
949         let derives = take_while_with_pred(context, attrs, is_derive);
950         if !derives.is_empty() {
951             let mut derive_args = vec![];
952             for derive in derives {
953                 derive_args.append(&mut get_derive_args(context, derive)?);
954             }
955             return Some((derives.len(), format_derive(context, &derive_args, shape)?));
956         }
957     }
958     // Rewrite the first attribute.
959     Some((1, attrs[0].rewrite(context, shape)?))
960 }
961
962 fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
963     // Look at before and after comment and see if there are any empty lines.
964     let comment_begin = comment.chars().position(|c| c == '/');
965     let len = comment_begin.unwrap_or_else(|| comment.len());
966     let mlb = count_newlines(&comment[..len]) > 1;
967     let mla = if comment_begin.is_none() {
968         mlb
969     } else {
970         let comment_end = comment.chars().rev().position(|c| !c.is_whitespace());
971         let len = comment_end.unwrap();
972         comment
973             .chars()
974             .rev()
975             .take(len)
976             .filter(|c| *c == '\n')
977             .count() > 1
978     };
979     (if mlb { "\n" } else { "" }, if mla { "\n" } else { "" })
980 }
981
982 impl<'a> Rewrite for [ast::Attribute] {
983     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
984         if self.is_empty() {
985             return Some(String::new());
986         }
987         let (first_group_len, first_group_str) = rewrite_first_group_attrs(context, self, shape)?;
988         if self.len() == 1 || first_group_len == self.len() {
989             Some(first_group_str)
990         } else {
991             let rest_str = self[first_group_len..].rewrite(context, shape)?;
992             let missing_span = mk_sp(
993                 self[first_group_len - 1].span.hi(),
994                 self[first_group_len].span.lo(),
995             );
996             // Preserve an empty line before/after doc comments.
997             if self[0].is_sugared_doc || self[first_group_len].is_sugared_doc {
998                 let snippet = context.snippet(missing_span);
999                 let (mla, mlb) = has_newlines_before_after_comment(snippet);
1000                 let comment = ::comment::recover_missing_comment_in_span(
1001                     missing_span,
1002                     shape.with_max_width(context.config),
1003                     context,
1004                     0,
1005                 )?;
1006                 let comment = if comment.is_empty() {
1007                     format!("\n{}", mlb)
1008                 } else {
1009                     format!("{}{}\n{}", mla, comment, mlb)
1010                 };
1011                 Some(format!(
1012                     "{}{}{}{}",
1013                     first_group_str,
1014                     comment,
1015                     shape.indent.to_string(context.config),
1016                     rest_str
1017                 ))
1018             } else {
1019                 combine_strs_with_missing_comments(
1020                     context,
1021                     &first_group_str,
1022                     &rest_str,
1023                     missing_span,
1024                     shape,
1025                     false,
1026                 )
1027             }
1028         }
1029     }
1030 }
1031
1032 // Format `#[derive(..)]`, using visual indent & mixed style when we need to go multiline.
1033 fn format_derive(context: &RewriteContext, derive_args: &[&str], shape: Shape) -> Option<String> {
1034     let mut result = String::with_capacity(128);
1035     result.push_str("#[derive(");
1036     // 11 = `#[derive()]`
1037     let initial_budget = shape.width.checked_sub(11)?;
1038     let mut budget = initial_budget;
1039     let num = derive_args.len();
1040     for (i, a) in derive_args.iter().enumerate() {
1041         // 2 = `, ` or `)]`
1042         let width = a.len() + 2;
1043         if width > budget {
1044             if i > 0 {
1045                 // Remove trailing whitespace.
1046                 result.pop();
1047             }
1048             result.push('\n');
1049             // 9 = `#[derive(`
1050             result.push_str(&(shape.indent + 9).to_string(context.config));
1051             budget = initial_budget;
1052         } else {
1053             budget = budget.checked_sub(width).unwrap_or(0);
1054         }
1055         result.push_str(a);
1056         if i != num - 1 {
1057             result.push_str(", ")
1058         }
1059     }
1060     result.push_str(")]");
1061     Some(result)
1062 }
1063
1064 fn is_derive(attr: &ast::Attribute) -> bool {
1065     attr.check_name("derive")
1066 }
1067
1068 /// Returns the arguments of `#[derive(...)]`.
1069 fn get_derive_args<'a>(context: &'a RewriteContext, attr: &ast::Attribute) -> Option<Vec<&'a str>> {
1070     attr.meta_item_list().map(|meta_item_list| {
1071         meta_item_list
1072             .iter()
1073             .map(|nested_meta_item| context.snippet(nested_meta_item.span))
1074             .collect()
1075     })
1076 }
1077
1078 // Rewrite `extern crate foo;` WITHOUT attributes.
1079 pub fn rewrite_extern_crate(context: &RewriteContext, item: &ast::Item) -> Option<String> {
1080     assert!(is_extern_crate(item));
1081     let new_str = context.snippet(item.span);
1082     Some(if contains_comment(new_str) {
1083         new_str.to_owned()
1084     } else {
1085         let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
1086         String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
1087     })
1088 }
1089
1090 fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
1091     match stmt.node {
1092         ast::StmtKind::Local(ref local) => &local.attrs,
1093         ast::StmtKind::Item(ref item) => &item.attrs,
1094         ast::StmtKind::Expr(ref expr) | ast::StmtKind::Semi(ref expr) => &expr.attrs,
1095         ast::StmtKind::Mac(ref mac) => &mac.2,
1096     }
1097 }