]> git.lizzy.rs Git - rust.git/blob - src/visitor.rs
Remove count_missing_closing_parens()
[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 strings::string_buffer::StringBuffer;
14 use syntax::{ast, ptr, visit};
15 use syntax::attr::HasAttrs;
16 use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
17 use syntax::parse::ParseSess;
18
19 use {Indent, Shape, Spanned};
20 use codemap::{LineRangeUtils, SpanUtils};
21 use comment::{contains_comment, CodeCharKind, CommentCodeSlices, FindUncommented};
22 use comment::rewrite_comment;
23 use config::{BraceStyle, Config};
24 use expr::{format_expr, ExprType};
25 use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_associated_type,
26             rewrite_static, rewrite_type_alias};
27 use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorTactic};
28 use macros::{rewrite_macro, MacroPosition};
29 use regex::Regex;
30 use rewrite::{Rewrite, RewriteContext};
31 use utils::{self, contains_skip, mk_sp};
32
33 fn is_use_item(item: &ast::Item) -> bool {
34     match item.node {
35         ast::ItemKind::Use(_) => true,
36         _ => false,
37     }
38 }
39
40 fn is_extern_crate(item: &ast::Item) -> bool {
41     match item.node {
42         ast::ItemKind::ExternCrate(..) => true,
43         _ => false,
44     }
45 }
46
47 pub struct FmtVisitor<'a> {
48     pub parse_session: &'a ParseSess,
49     pub codemap: &'a CodeMap,
50     pub buffer: StringBuffer,
51     pub last_pos: BytePos,
52     // FIXME: use an RAII util or closure for indenting
53     pub block_indent: Indent,
54     pub config: &'a Config,
55     pub is_if_else_block: bool,
56 }
57
58 impl<'a> FmtVisitor<'a> {
59     fn visit_stmt(&mut self, stmt: &ast::Stmt) {
60         debug!(
61             "visit_stmt: {:?} {:?}",
62             self.codemap.lookup_char_pos(stmt.span.lo),
63             self.codemap.lookup_char_pos(stmt.span.hi)
64         );
65
66         match stmt.node {
67             ast::StmtKind::Item(ref item) => {
68                 self.visit_item(item);
69             }
70             ast::StmtKind::Local(ref local) => {
71                 let rewrite = if contains_skip(&local.attrs) {
72                     None
73                 } else {
74                     stmt.rewrite(
75                         &self.get_context(),
76                         Shape::indented(self.block_indent, self.config),
77                     )
78                 };
79                 self.push_rewrite(stmt.span, rewrite);
80             }
81             ast::StmtKind::Expr(ref expr) => {
82                 let rewrite = format_expr(
83                     expr,
84                     ExprType::Statement,
85                     &self.get_context(),
86                     Shape::indented(self.block_indent, self.config),
87                 );
88                 let span = if expr.attrs.is_empty() {
89                     stmt.span
90                 } else {
91                     mk_sp(expr.span().lo, stmt.span.hi)
92                 };
93                 self.push_rewrite(span, rewrite)
94             }
95             ast::StmtKind::Semi(ref expr) => {
96                 let rewrite = stmt.rewrite(
97                     &self.get_context(),
98                     Shape::indented(self.block_indent, self.config),
99                 );
100                 let span = if expr.attrs.is_empty() {
101                     stmt.span
102                 } else {
103                     mk_sp(expr.span().lo, stmt.span.hi)
104                 };
105                 self.push_rewrite(span, rewrite)
106             }
107             ast::StmtKind::Mac(ref mac) => {
108                 let (ref mac, _macro_style, ref attrs) = **mac;
109                 if contains_skip(attrs) {
110                     self.push_rewrite(mac.span, None);
111                 } else {
112                     self.visit_mac(mac, None, MacroPosition::Statement);
113                 }
114                 self.format_missing(stmt.span.hi);
115             }
116         }
117     }
118
119     pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribute]>) {
120         debug!(
121             "visit_block: {:?} {:?}",
122             self.codemap.lookup_char_pos(b.span.lo),
123             self.codemap.lookup_char_pos(b.span.hi)
124         );
125
126         // Check if this block has braces.
127         let snippet = self.snippet(b.span);
128         let has_braces = snippet.starts_with('{') || snippet.starts_with("unsafe");
129         let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
130
131         self.last_pos = self.last_pos + brace_compensation;
132         self.block_indent = self.block_indent.block_indent(self.config);
133         self.buffer.push_str("{");
134
135         if self.config.remove_blank_lines_at_start_or_end_of_block() {
136             if let Some(first_stmt) = b.stmts.first() {
137                 let attr_lo = inner_attrs
138                     .and_then(|attrs| {
139                         utils::inner_attributes(attrs)
140                             .first()
141                             .map(|attr| attr.span.lo)
142                     })
143                     .or_else(|| {
144                         // Attributes for an item in a statement position
145                         // do not belong to the statement. (rust-lang/rust#34459)
146                         if let ast::StmtKind::Item(ref item) = first_stmt.node {
147                             item.attrs.first()
148                         } else {
149                             first_stmt.attrs().first()
150                         }.and_then(|attr| {
151                             // Some stmts can have embedded attributes.
152                             // e.g. `match { #![attr] ... }`
153                             let attr_lo = attr.span.lo;
154                             if attr_lo < first_stmt.span.lo {
155                                 Some(attr_lo)
156                             } else {
157                                 None
158                             }
159                         })
160                     });
161
162                 let snippet =
163                     self.snippet(mk_sp(self.last_pos, attr_lo.unwrap_or(first_stmt.span.lo)));
164                 let len = CommentCodeSlices::new(&snippet).nth(0).and_then(
165                     |(kind, _, s)| if kind == CodeCharKind::Normal {
166                         s.rfind('\n')
167                     } else {
168                         None
169                     },
170                 );
171                 if let Some(len) = len {
172                     self.last_pos = self.last_pos + BytePos::from_usize(len);
173                 }
174             }
175         }
176
177         // Format inner attributes if available.
178         if let Some(attrs) = inner_attrs {
179             self.visit_attrs(attrs, ast::AttrStyle::Inner);
180         }
181
182         for stmt in &b.stmts {
183             self.visit_stmt(stmt)
184         }
185
186         if !b.stmts.is_empty() {
187             if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
188                 if utils::semicolon_for_expr(&self.get_context(), expr) {
189                     self.buffer.push_str(";");
190                 }
191             }
192         }
193
194         let mut remove_len = BytePos(0);
195         if self.config.remove_blank_lines_at_start_or_end_of_block() {
196             if let Some(stmt) = b.stmts.last() {
197                 let snippet = self.snippet(mk_sp(
198                     stmt.span.hi,
199                     source!(self, b.span).hi - brace_compensation,
200                 ));
201                 let len = CommentCodeSlices::new(&snippet)
202                     .last()
203                     .and_then(|(kind, _, s)| {
204                         if kind == CodeCharKind::Normal && s.trim().is_empty() {
205                             Some(s.len())
206                         } else {
207                             None
208                         }
209                     });
210                 if let Some(len) = len {
211                     remove_len = BytePos::from_usize(len);
212                 }
213             }
214         }
215
216         let mut unindent_comment = self.is_if_else_block && !b.stmts.is_empty();
217         if unindent_comment {
218             let end_pos = source!(self, b.span).hi - brace_compensation - remove_len;
219             let snippet = self.get_context().snippet(mk_sp(self.last_pos, end_pos));
220             unindent_comment = snippet.contains("//") || snippet.contains("/*");
221         }
222         // FIXME: we should compress any newlines here to just one
223         if unindent_comment {
224             self.block_indent = self.block_indent.block_unindent(self.config);
225         }
226         self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation - remove_len);
227         if unindent_comment {
228             self.block_indent = self.block_indent.block_indent(self.config);
229         }
230         self.close_block(unindent_comment);
231         self.last_pos = source!(self, b.span).hi;
232     }
233
234     // FIXME: this is a terrible hack to indent the comments between the last
235     // item in the block and the closing brace to the block's level.
236     // The closing brace itself, however, should be indented at a shallower
237     // level.
238     fn close_block(&mut self, unindent_comment: bool) {
239         let total_len = self.buffer.len;
240         let chars_too_many = if unindent_comment {
241             0
242         } else if self.config.hard_tabs() {
243             1
244         } else {
245             self.config.tab_spaces()
246         };
247         self.buffer.truncate(total_len - chars_too_many);
248         self.buffer.push_str("}");
249         self.block_indent = self.block_indent.block_unindent(self.config);
250     }
251
252     // Note that this only gets called for function definitions. Required methods
253     // on traits do not get handled here.
254     fn visit_fn(
255         &mut self,
256         fk: visit::FnKind,
257         fd: &ast::FnDecl,
258         s: Span,
259         _: ast::NodeId,
260         defaultness: ast::Defaultness,
261         inner_attrs: Option<&[ast::Attribute]>,
262     ) {
263         let indent = self.block_indent;
264         let block;
265         let rewrite = match fk {
266             visit::FnKind::ItemFn(ident, generics, unsafety, constness, abi, vis, b) => {
267                 block = b;
268                 self.rewrite_fn(
269                     indent,
270                     ident,
271                     fd,
272                     generics,
273                     unsafety,
274                     constness.node,
275                     defaultness,
276                     abi,
277                     vis,
278                     mk_sp(s.lo, b.span.lo),
279                     &b,
280                 )
281             }
282             visit::FnKind::Method(ident, sig, vis, b) => {
283                 block = b;
284                 self.rewrite_fn(
285                     indent,
286                     ident,
287                     fd,
288                     &sig.generics,
289                     sig.unsafety,
290                     sig.constness.node,
291                     defaultness,
292                     sig.abi,
293                     vis.unwrap_or(&ast::Visibility::Inherited),
294                     mk_sp(s.lo, b.span.lo),
295                     &b,
296                 )
297             }
298             visit::FnKind::Closure(_) => unreachable!(),
299         };
300
301         if let Some(fn_str) = rewrite {
302             self.format_missing_with_indent(source!(self, s).lo);
303             self.buffer.push_str(&fn_str);
304             if let Some(c) = fn_str.chars().last() {
305                 if c == '}' {
306                     self.last_pos = source!(self, block.span).hi;
307                     return;
308                 }
309             }
310         } else {
311             self.format_missing(source!(self, block.span).lo);
312         }
313
314         self.last_pos = source!(self, block.span).lo;
315         self.visit_block(block, inner_attrs)
316     }
317
318     pub fn visit_item(&mut self, item: &ast::Item) {
319         skip_out_of_file_lines_range_visitor!(self, item.span);
320
321         // This is where we bail out if there is a skip attribute. This is only
322         // complex in the module case. It is complex because the module could be
323         // in a separate file and there might be attributes in both files, but
324         // the AST lumps them all together.
325         let mut attrs = item.attrs.clone();
326         match item.node {
327             ast::ItemKind::Mod(ref m) => {
328                 let outer_file = self.codemap.lookup_char_pos(item.span.lo).file;
329                 let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file;
330                 if outer_file.name == inner_file.name {
331                     // Module is inline, in this case we treat modules like any
332                     // other item.
333                     if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
334                         self.push_rewrite(item.span, None);
335                         return;
336                     }
337                 } else if utils::contains_skip(&item.attrs) {
338                     // Module is not inline, but should be skipped.
339                     return;
340                 } else {
341                     // Module is not inline and should not be skipped. We want
342                     // to process only the attributes in the current file.
343                     let filterd_attrs = item.attrs
344                         .iter()
345                         .filter_map(|a| {
346                             let attr_file = self.codemap.lookup_char_pos(a.span.lo).file;
347                             if attr_file.name == outer_file.name {
348                                 Some(a.clone())
349                             } else {
350                                 None
351                             }
352                         })
353                         .collect::<Vec<_>>();
354                     // Assert because if we should skip it should be caught by
355                     // the above case.
356                     assert!(!self.visit_attrs(&filterd_attrs, ast::AttrStyle::Outer));
357                     attrs = filterd_attrs;
358                 }
359             }
360             _ => if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
361                 self.push_rewrite(item.span, None);
362                 return;
363             },
364         }
365
366         match item.node {
367             ast::ItemKind::Use(ref vp) => {
368                 self.format_import(&item.vis, vp, item.span, &item.attrs);
369             }
370             ast::ItemKind::Impl(..) => {
371                 self.format_missing_with_indent(source!(self, item.span).lo);
372                 let snippet = self.get_context().snippet(item.span);
373                 let where_span_end = snippet
374                     .find_uncommented("{")
375                     .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
376                 if let Some(impl_str) =
377                     format_impl(&self.get_context(), item, self.block_indent, where_span_end)
378                 {
379                     self.buffer.push_str(&impl_str);
380                     self.last_pos = source!(self, item.span).hi;
381                 }
382             }
383             ast::ItemKind::Trait(..) => {
384                 self.format_missing_with_indent(item.span.lo);
385                 if let Some(trait_str) = format_trait(&self.get_context(), item, self.block_indent)
386                 {
387                     self.buffer.push_str(&trait_str);
388                     self.last_pos = source!(self, item.span).hi;
389                 }
390             }
391             ast::ItemKind::ExternCrate(_) => {
392                 self.format_missing_with_indent(source!(self, item.span).lo);
393                 let new_str = self.snippet(item.span);
394                 if contains_comment(&new_str) {
395                     self.buffer.push_str(&new_str)
396                 } else {
397                     let no_whitespace =
398                         &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
399                     self.buffer
400                         .push_str(&Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"));
401                 }
402                 self.last_pos = source!(self, item.span).hi;
403             }
404             ast::ItemKind::Struct(ref def, ref generics) => {
405                 let rewrite = {
406                     let indent = self.block_indent;
407                     let context = self.get_context();
408                     ::items::format_struct(
409                         &context,
410                         "struct ",
411                         item.ident,
412                         &item.vis,
413                         def,
414                         Some(generics),
415                         item.span,
416                         indent,
417                         None,
418                     ).map(|s| match *def {
419                         ast::VariantData::Tuple(..) => s + ";",
420                         _ => s,
421                     })
422                 };
423                 self.push_rewrite(item.span, rewrite);
424             }
425             ast::ItemKind::Enum(ref def, ref generics) => {
426                 self.format_missing_with_indent(source!(self, item.span).lo);
427                 self.visit_enum(item.ident, &item.vis, def, generics, item.span);
428                 self.last_pos = source!(self, item.span).hi;
429             }
430             ast::ItemKind::Mod(ref module) => {
431                 self.format_missing_with_indent(source!(self, item.span).lo);
432                 self.format_mod(module, &item.vis, item.span, item.ident, &attrs);
433             }
434             ast::ItemKind::Mac(ref mac) => {
435                 self.visit_mac(mac, Some(item.ident), MacroPosition::Item);
436             }
437             ast::ItemKind::ForeignMod(ref foreign_mod) => {
438                 self.format_missing_with_indent(source!(self, item.span).lo);
439                 self.format_foreign_mod(foreign_mod, item.span);
440             }
441             ast::ItemKind::Static(ref ty, mutability, ref expr) => {
442                 let rewrite = rewrite_static(
443                     "static",
444                     &item.vis,
445                     item.ident,
446                     ty,
447                     mutability,
448                     Some(expr),
449                     self.block_indent,
450                     item.span,
451                     &self.get_context(),
452                 );
453                 self.push_rewrite(item.span, rewrite);
454             }
455             ast::ItemKind::Const(ref ty, ref expr) => {
456                 let rewrite = rewrite_static(
457                     "const",
458                     &item.vis,
459                     item.ident,
460                     ty,
461                     ast::Mutability::Immutable,
462                     Some(expr),
463                     self.block_indent,
464                     item.span,
465                     &self.get_context(),
466                 );
467                 self.push_rewrite(item.span, rewrite);
468             }
469             ast::ItemKind::DefaultImpl(..) => {
470                 // FIXME(#78): format impl definitions.
471             }
472             ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
473                 self.visit_fn(
474                     visit::FnKind::ItemFn(
475                         item.ident,
476                         generics,
477                         unsafety,
478                         constness,
479                         abi,
480                         &item.vis,
481                         body,
482                     ),
483                     decl,
484                     item.span,
485                     item.id,
486                     ast::Defaultness::Final,
487                     Some(&item.attrs),
488                 )
489             }
490             ast::ItemKind::Ty(ref ty, ref generics) => {
491                 let rewrite = rewrite_type_alias(
492                     &self.get_context(),
493                     self.block_indent,
494                     item.ident,
495                     ty,
496                     generics,
497                     &item.vis,
498                     item.span,
499                 );
500                 self.push_rewrite(item.span, rewrite);
501             }
502             ast::ItemKind::Union(ref def, ref generics) => {
503                 let rewrite = ::items::format_struct_struct(
504                     &self.get_context(),
505                     "union ",
506                     item.ident,
507                     &item.vis,
508                     def.fields(),
509                     Some(generics),
510                     item.span,
511                     self.block_indent,
512                     None,
513                 );
514                 self.push_rewrite(item.span, rewrite);
515             }
516             ast::ItemKind::GlobalAsm(..) => {
517                 let snippet = Some(self.snippet(item.span));
518                 self.push_rewrite(item.span, snippet);
519             }
520             ast::ItemKind::MacroDef(..) => {
521                 // FIXME(#1539): macros 2.0
522                 let snippet = Some(self.snippet(item.span));
523                 self.push_rewrite(item.span, snippet);
524             }
525         }
526     }
527
528     pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
529         skip_out_of_file_lines_range_visitor!(self, ti.span);
530
531         if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
532             self.push_rewrite(ti.span, None);
533             return;
534         }
535
536         match ti.node {
537             ast::TraitItemKind::Const(ref ty, ref expr_opt) => {
538                 let rewrite = rewrite_static(
539                     "const",
540                     &ast::Visibility::Inherited,
541                     ti.ident,
542                     ty,
543                     ast::Mutability::Immutable,
544                     expr_opt.as_ref(),
545                     self.block_indent,
546                     ti.span,
547                     &self.get_context(),
548                 );
549                 self.push_rewrite(ti.span, rewrite);
550             }
551             ast::TraitItemKind::Method(ref sig, None) => {
552                 let indent = self.block_indent;
553                 let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
554                 self.push_rewrite(ti.span, rewrite);
555             }
556             ast::TraitItemKind::Method(ref sig, Some(ref body)) => {
557                 self.visit_fn(
558                     visit::FnKind::Method(ti.ident, sig, None, body),
559                     &sig.decl,
560                     ti.span,
561                     ti.id,
562                     ast::Defaultness::Final,
563                     Some(&ti.attrs),
564                 );
565             }
566             ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => {
567                 let rewrite = rewrite_associated_type(
568                     ti.ident,
569                     type_default.as_ref(),
570                     Some(type_param_bounds),
571                     &self.get_context(),
572                     self.block_indent,
573                 );
574                 self.push_rewrite(ti.span, rewrite);
575             }
576             ast::TraitItemKind::Macro(ref mac) => {
577                 self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
578             }
579         }
580     }
581
582     pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
583         skip_out_of_file_lines_range_visitor!(self, ii.span);
584
585         if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
586             self.push_rewrite(ii.span, None);
587             return;
588         }
589
590         match ii.node {
591             ast::ImplItemKind::Method(ref sig, ref body) => {
592                 self.visit_fn(
593                     visit::FnKind::Method(ii.ident, sig, Some(&ii.vis), body),
594                     &sig.decl,
595                     ii.span,
596                     ii.id,
597                     ii.defaultness,
598                     Some(&ii.attrs),
599                 );
600             }
601             ast::ImplItemKind::Const(ref ty, ref expr) => {
602                 let rewrite = rewrite_static(
603                     "const",
604                     &ii.vis,
605                     ii.ident,
606                     ty,
607                     ast::Mutability::Immutable,
608                     Some(expr),
609                     self.block_indent,
610                     ii.span,
611                     &self.get_context(),
612                 );
613                 self.push_rewrite(ii.span, rewrite);
614             }
615             ast::ImplItemKind::Type(ref ty) => {
616                 let rewrite = rewrite_associated_impl_type(
617                     ii.ident,
618                     ii.defaultness,
619                     Some(ty),
620                     None,
621                     &self.get_context(),
622                     self.block_indent,
623                 );
624                 self.push_rewrite(ii.span, rewrite);
625             }
626             ast::ImplItemKind::Macro(ref mac) => {
627                 self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
628             }
629         }
630     }
631
632     fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPosition) {
633         skip_out_of_file_lines_range_visitor!(self, mac.span);
634
635         // 1 = ;
636         let shape = Shape::indented(self.block_indent, self.config)
637             .sub_width(1)
638             .unwrap();
639         let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
640         self.push_rewrite(mac.span, rewrite);
641     }
642
643     fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
644         self.format_missing_with_indent(source!(self, span).lo);
645         let result = rewrite.unwrap_or_else(|| self.snippet(span));
646         self.buffer.push_str(&result);
647         self.last_pos = source!(self, span).hi;
648     }
649
650     pub fn from_codemap(parse_session: &'a ParseSess, config: &'a Config) -> FmtVisitor<'a> {
651         FmtVisitor {
652             parse_session: parse_session,
653             codemap: parse_session.codemap(),
654             buffer: StringBuffer::new(),
655             last_pos: BytePos(0),
656             block_indent: Indent::empty(),
657             config: config,
658             is_if_else_block: false,
659         }
660     }
661
662     pub fn snippet(&self, span: Span) -> String {
663         match self.codemap.span_to_snippet(span) {
664             Ok(s) => s,
665             Err(_) => {
666                 println!(
667                     "Couldn't make snippet for span {:?}->{:?}",
668                     self.codemap.lookup_char_pos(span.lo),
669                     self.codemap.lookup_char_pos(span.hi)
670                 );
671                 "".to_owned()
672             }
673         }
674     }
675
676     // Returns true if we should skip the following item.
677     pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
678         if utils::contains_skip(attrs) {
679             return true;
680         }
681
682         let attrs: Vec<_> = attrs.iter().filter(|a| a.style == style).cloned().collect();
683         if attrs.is_empty() {
684             return false;
685         }
686
687         let rewrite = attrs.rewrite(
688             &self.get_context(),
689             Shape::indented(self.block_indent, self.config),
690         );
691         let span = mk_sp(attrs[0].span.lo, attrs[attrs.len() - 1].span.hi);
692         self.push_rewrite(span, rewrite);
693
694         false
695     }
696
697     fn reorder_items<F>(
698         &mut self,
699         items_left: &[ptr::P<ast::Item>],
700         is_item: &F,
701         in_group: bool,
702     ) -> usize
703     where
704         F: Fn(&ast::Item) -> bool,
705     {
706         let mut last = self.codemap.lookup_line_range(items_left[0].span());
707         let item_length = items_left
708             .iter()
709             .take_while(|ppi| {
710                 is_item(&***ppi) && (!in_group || {
711                     let current = self.codemap.lookup_line_range(ppi.span());
712                     let in_same_group = current.lo < last.hi + 2;
713                     last = current;
714                     in_same_group
715                 })
716             })
717             .count();
718         let items = &items_left[..item_length];
719
720         let at_least_one_in_file_lines = items
721             .iter()
722             .any(|item| !out_of_file_lines_range!(self, item.span));
723
724         if at_least_one_in_file_lines {
725             self.format_imports(items);
726         } else {
727             for item in items {
728                 self.push_rewrite(item.span, None);
729             }
730         }
731
732         item_length
733     }
734
735     fn walk_mod_items(&mut self, m: &ast::Mod) {
736         let mut items_left: &[ptr::P<ast::Item>] = &m.items;
737         while !items_left.is_empty() {
738             // If the next item is a `use` declaration, then extract it and any subsequent `use`s
739             // to be potentially reordered within `format_imports`. Otherwise, just format the
740             // next item for output.
741             if self.config.reorder_imports() && is_use_item(&*items_left[0]) {
742                 let used_items_len = self.reorder_items(
743                     &items_left,
744                     &is_use_item,
745                     self.config.reorder_imports_in_group(),
746                 );
747                 let (_, rest) = items_left.split_at(used_items_len);
748                 items_left = rest;
749             } else if self.config.reorder_extern_crates() && is_extern_crate(&*items_left[0]) {
750                 let used_items_len = self.reorder_items(
751                     &items_left,
752                     &is_extern_crate,
753                     self.config.reorder_extern_crates_in_group(),
754                 );
755                 let (_, rest) = items_left.split_at(used_items_len);
756                 items_left = rest;
757             } else {
758                 // `unwrap()` is safe here because we know `items_left`
759                 // has elements from the loop condition
760                 let (item, rest) = items_left.split_first().unwrap();
761                 self.visit_item(item);
762                 items_left = rest;
763             }
764         }
765     }
766
767     fn format_mod(
768         &mut self,
769         m: &ast::Mod,
770         vis: &ast::Visibility,
771         s: Span,
772         ident: ast::Ident,
773         attrs: &[ast::Attribute],
774     ) {
775         // Decide whether this is an inline mod or an external mod.
776         let local_file_name = self.codemap.span_to_filename(s);
777         let inner_span = source!(self, m.inner);
778         let is_internal = !(inner_span.lo.0 == 0 && inner_span.hi.0 == 0) &&
779             local_file_name == self.codemap.span_to_filename(inner_span);
780
781         self.buffer.push_str(&*utils::format_visibility(vis));
782         self.buffer.push_str("mod ");
783         self.buffer.push_str(&ident.to_string());
784
785         if is_internal {
786             match self.config.item_brace_style() {
787                 BraceStyle::AlwaysNextLine => self.buffer
788                     .push_str(&format!("\n{}{{", self.block_indent.to_string(self.config))),
789                 _ => self.buffer.push_str(" {"),
790             }
791             // Hackery to account for the closing }.
792             let mod_lo = self.codemap.span_after(source!(self, s), "{");
793             let body_snippet = self.snippet(mk_sp(mod_lo, source!(self, m.inner).hi - BytePos(1)));
794             let body_snippet = body_snippet.trim();
795             if body_snippet.is_empty() {
796                 self.buffer.push_str("}");
797             } else {
798                 self.last_pos = mod_lo;
799                 self.block_indent = self.block_indent.block_indent(self.config);
800                 self.visit_attrs(attrs, ast::AttrStyle::Inner);
801                 self.walk_mod_items(m);
802                 self.format_missing_with_indent(source!(self, m.inner).hi - BytePos(1));
803                 self.close_block(false);
804             }
805             self.last_pos = source!(self, m.inner).hi;
806         } else {
807             self.buffer.push_str(";");
808             self.last_pos = source!(self, s).hi;
809         }
810     }
811
812     pub fn format_separate_mod(&mut self, m: &ast::Mod, filemap: &codemap::FileMap) {
813         self.block_indent = Indent::empty();
814         self.walk_mod_items(m);
815         self.format_missing_with_indent(filemap.end_pos);
816     }
817
818     pub fn get_context(&self) -> RewriteContext {
819         RewriteContext {
820             parse_session: self.parse_session,
821             codemap: self.codemap,
822             config: self.config,
823             inside_macro: false,
824             use_block: false,
825             is_if_else_block: false,
826             force_one_line_chain: false,
827         }
828     }
829 }
830
831 impl Rewrite for ast::NestedMetaItem {
832     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
833         match self.node {
834             ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
835             ast::NestedMetaItemKind::Literal(..) => Some(context.snippet(self.span)),
836         }
837     }
838 }
839
840 impl Rewrite for ast::MetaItem {
841     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
842         Some(match self.node {
843             ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
844             ast::MetaItemKind::List(ref list) => {
845                 let name = self.name.as_str();
846                 // 3 = `#[` and `(`, 2 = `]` and `)`
847                 let item_shape = try_opt!(
848                     shape
849                         .shrink_left(name.len() + 3)
850                         .and_then(|s| s.sub_width(2))
851                 );
852                 let items = itemize_list(
853                     context.codemap,
854                     list.iter(),
855                     ")",
856                     |nested_meta_item| nested_meta_item.span.lo,
857                     |nested_meta_item| nested_meta_item.span.hi,
858                     |nested_meta_item| nested_meta_item.rewrite(context, item_shape),
859                     self.span.lo,
860                     self.span.hi,
861                     false,
862                 );
863                 let item_vec = items.collect::<Vec<_>>();
864                 let fmt = ListFormatting {
865                     tactic: DefinitiveListTactic::Mixed,
866                     separator: ",",
867                     trailing_separator: SeparatorTactic::Never,
868                     shape: item_shape,
869                     ends_with_newline: false,
870                     preserve_newline: false,
871                     config: context.config,
872                 };
873                 format!("{}({})", name, try_opt!(write_list(&item_vec, &fmt)))
874             }
875             ast::MetaItemKind::NameValue(ref literal) => {
876                 let name = self.name.as_str();
877                 let value = context.snippet(literal.span);
878                 if &*name == "doc" && contains_comment(&value) {
879                     let doc_shape = Shape {
880                         width: cmp::min(shape.width, context.config.comment_width())
881                             .checked_sub(shape.indent.width())
882                             .unwrap_or(0),
883                         ..shape
884                     };
885                     format!(
886                         "{}",
887                         try_opt!(rewrite_comment(&value, false, doc_shape, context.config))
888                     )
889                 } else {
890                     format!("{} = {}", name, value)
891                 }
892             }
893         })
894     }
895 }
896
897 impl Rewrite for ast::Attribute {
898     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
899         try_opt!(self.meta())
900             .rewrite(context, shape)
901             .map(|rw| if self.is_sugared_doc {
902                 rw
903             } else {
904                 let original = context.snippet(self.span);
905                 let prefix = match self.style {
906                     ast::AttrStyle::Inner => "#!",
907                     ast::AttrStyle::Outer => "#",
908                 };
909                 if contains_comment(&original) {
910                     original
911                 } else {
912                     format!("{}[{}]", prefix, rw)
913                 }
914             })
915     }
916 }
917
918 impl<'a> Rewrite for [ast::Attribute] {
919     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
920         let mut result = String::new();
921         if self.is_empty() {
922             return Some(result);
923         }
924         let indent = shape.indent.to_string(context.config);
925
926         for (i, a) in self.iter().enumerate() {
927             let a_str = try_opt!(a.rewrite(context, shape));
928
929             // Write comments and blank lines between attributes.
930             if i > 0 {
931                 let comment = context.snippet(mk_sp(self[i - 1].span.hi, a.span.lo));
932                 // This particular horror show is to preserve line breaks in between doc
933                 // comments. An alternative would be to force such line breaks to start
934                 // with the usual doc comment token.
935                 let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1;
936                 let comment = comment.trim();
937                 if !comment.is_empty() {
938                     let comment = try_opt!(rewrite_comment(
939                         comment,
940                         false,
941                         Shape::legacy(
942                             context.config.comment_width() - shape.indent.width(),
943                             shape.indent,
944                         ),
945                         context.config,
946                     ));
947                     result.push_str(&indent);
948                     result.push_str(&comment);
949                     result.push('\n');
950                 } else if multi_line {
951                     result.push('\n');
952                 }
953                 result.push_str(&indent);
954             }
955
956             // Write the attribute itself.
957             result.push_str(&a_str);
958
959             if i < self.len() - 1 {
960                 result.push('\n');
961             }
962         }
963
964         Some(result)
965     }
966 }