]> git.lizzy.rs Git - rust.git/blob - src/visitor.rs
Merge pull request #1886 from topecongiro/issue-1882
[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 first = &attrs[0];
688         self.format_missing_with_indent(source!(self, first.span).lo);
689
690         let rewrite = attrs
691             .rewrite(
692                 &self.get_context(),
693                 Shape::indented(self.block_indent, self.config),
694             )
695             .unwrap();
696         self.buffer.push_str(&rewrite);
697         let last = attrs.last().unwrap();
698         self.last_pos = source!(self, last.span).hi;
699         false
700     }
701
702     fn reorder_items<F>(
703         &mut self,
704         items_left: &[ptr::P<ast::Item>],
705         is_item: &F,
706         in_group: bool,
707     ) -> usize
708     where
709         F: Fn(&ast::Item) -> bool,
710     {
711         let mut last = self.codemap.lookup_line_range(items_left[0].span());
712         let item_length = items_left
713             .iter()
714             .take_while(|ppi| {
715                 is_item(&***ppi) && (!in_group || {
716                     let current = self.codemap.lookup_line_range(ppi.span());
717                     let in_same_group = current.lo < last.hi + 2;
718                     last = current;
719                     in_same_group
720                 })
721             })
722             .count();
723         let items = &items_left[..item_length];
724
725         let at_least_one_in_file_lines = items
726             .iter()
727             .any(|item| !out_of_file_lines_range!(self, item.span));
728
729         if at_least_one_in_file_lines {
730             self.format_imports(items);
731         } else {
732             for item in items {
733                 self.push_rewrite(item.span, None);
734             }
735         }
736
737         item_length
738     }
739
740     fn walk_mod_items(&mut self, m: &ast::Mod) {
741         let mut items_left: &[ptr::P<ast::Item>] = &m.items;
742         while !items_left.is_empty() {
743             // If the next item is a `use` declaration, then extract it and any subsequent `use`s
744             // to be potentially reordered within `format_imports`. Otherwise, just format the
745             // next item for output.
746             if self.config.reorder_imports() && is_use_item(&*items_left[0]) {
747                 let used_items_len = self.reorder_items(
748                     &items_left,
749                     &is_use_item,
750                     self.config.reorder_imports_in_group(),
751                 );
752                 let (_, rest) = items_left.split_at(used_items_len);
753                 items_left = rest;
754             } else if self.config.reorder_extern_crates() && is_extern_crate(&*items_left[0]) {
755                 let used_items_len = self.reorder_items(
756                     &items_left,
757                     &is_extern_crate,
758                     self.config.reorder_extern_crates_in_group(),
759                 );
760                 let (_, rest) = items_left.split_at(used_items_len);
761                 items_left = rest;
762             } else {
763                 // `unwrap()` is safe here because we know `items_left`
764                 // has elements from the loop condition
765                 let (item, rest) = items_left.split_first().unwrap();
766                 self.visit_item(item);
767                 items_left = rest;
768             }
769         }
770     }
771
772     fn format_mod(
773         &mut self,
774         m: &ast::Mod,
775         vis: &ast::Visibility,
776         s: Span,
777         ident: ast::Ident,
778         attrs: &[ast::Attribute],
779     ) {
780         // Decide whether this is an inline mod or an external mod.
781         let local_file_name = self.codemap.span_to_filename(s);
782         let inner_span = source!(self, m.inner);
783         let is_internal = !(inner_span.lo.0 == 0 && inner_span.hi.0 == 0) &&
784             local_file_name == self.codemap.span_to_filename(inner_span);
785
786         self.buffer.push_str(&*utils::format_visibility(vis));
787         self.buffer.push_str("mod ");
788         self.buffer.push_str(&ident.to_string());
789
790         if is_internal {
791             match self.config.item_brace_style() {
792                 BraceStyle::AlwaysNextLine => self.buffer
793                     .push_str(&format!("\n{}{{", self.block_indent.to_string(self.config))),
794                 _ => self.buffer.push_str(" {"),
795             }
796             // Hackery to account for the closing }.
797             let mod_lo = self.codemap.span_after(source!(self, s), "{");
798             let body_snippet = self.snippet(mk_sp(mod_lo, source!(self, m.inner).hi - BytePos(1)));
799             let body_snippet = body_snippet.trim();
800             if body_snippet.is_empty() {
801                 self.buffer.push_str("}");
802             } else {
803                 self.last_pos = mod_lo;
804                 self.block_indent = self.block_indent.block_indent(self.config);
805                 self.visit_attrs(attrs, ast::AttrStyle::Inner);
806                 self.walk_mod_items(m);
807                 self.format_missing_with_indent(source!(self, m.inner).hi - BytePos(1));
808                 self.close_block(false);
809             }
810             self.last_pos = source!(self, m.inner).hi;
811         } else {
812             self.buffer.push_str(";");
813             self.last_pos = source!(self, s).hi;
814         }
815     }
816
817     pub fn format_separate_mod(&mut self, m: &ast::Mod, filemap: &codemap::FileMap) {
818         self.block_indent = Indent::empty();
819         self.walk_mod_items(m);
820         self.format_missing_with_indent(filemap.end_pos);
821     }
822
823     pub fn get_context(&self) -> RewriteContext {
824         RewriteContext {
825             parse_session: self.parse_session,
826             codemap: self.codemap,
827             config: self.config,
828             inside_macro: false,
829             use_block: false,
830             is_if_else_block: false,
831             force_one_line_chain: false,
832         }
833     }
834 }
835
836 impl Rewrite for ast::NestedMetaItem {
837     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
838         match self.node {
839             ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
840             ast::NestedMetaItemKind::Literal(..) => Some(context.snippet(self.span)),
841         }
842     }
843 }
844
845 fn count_missing_closing_parens(s: &str) -> u32 {
846     let mut op_parens: u32 = 0;
847     let mut cl_parens: u32 = 0;
848
849     #[derive(Eq, PartialEq)]
850     pub enum SnippetState {
851         Normal,
852         InsideStr,
853         InsideBulletComment,
854         InsideSingleLineComment,
855     }
856
857     let mut state = SnippetState::Normal;
858     let mut iter = s.chars().peekable();
859     let mut prev_char: Option<char> = None;
860     while let Some(c) = iter.next() {
861         let next_char = iter.peek();
862         match c {
863             '/' if state == SnippetState::Normal => match next_char {
864                 Some(&'*') => state = SnippetState::InsideBulletComment,
865                 Some(&'/') if prev_char.map_or(true, |c| c != '*') => {
866                     state = SnippetState::InsideSingleLineComment;
867                 }
868                 _ => (),
869             },
870             '*' if state == SnippetState::InsideBulletComment &&
871                 next_char.map_or(false, |c| *c == '/') =>
872             {
873                 state = SnippetState::Normal;
874             }
875             '\n' if state == SnippetState::InsideSingleLineComment => state = SnippetState::Normal,
876             '"' if state == SnippetState::InsideStr && prev_char.map_or(false, |c| c != '\\') => {
877                 state = SnippetState::Normal;
878             }
879             '"' if state == SnippetState::Normal && prev_char.map_or(false, |c| c != '\\') => {
880                 state = SnippetState::InsideStr
881             }
882             '(' if state == SnippetState::Normal => op_parens += 1,
883             ')' if state == SnippetState::Normal => cl_parens += 1,
884             _ => (),
885         }
886         prev_char = Some(c);
887     }
888     op_parens.checked_sub(cl_parens).unwrap_or(0)
889 }
890
891 impl Rewrite for ast::MetaItem {
892     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
893         Some(match self.node {
894             ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
895             ast::MetaItemKind::List(ref list) => {
896                 let name = self.name.as_str();
897                 // 3 = `#[` and `(`, 2 = `]` and `)`
898                 let item_shape = try_opt!(
899                     shape
900                         .shrink_left(name.len() + 3)
901                         .and_then(|s| s.sub_width(2))
902                 );
903                 let hi = self.span.hi +
904                     BytePos(count_missing_closing_parens(&context.snippet(self.span)));
905                 let items = itemize_list(
906                     context.codemap,
907                     list.iter(),
908                     ")",
909                     |nested_meta_item| nested_meta_item.span.lo,
910                     // FIXME: Span from MetaItem is missing closing parens.
911                     |nested_meta_item| {
912                         let snippet = context.snippet(nested_meta_item.span);
913                         nested_meta_item.span.hi + BytePos(count_missing_closing_parens(&snippet))
914                     },
915                     |nested_meta_item| nested_meta_item.rewrite(context, item_shape),
916                     self.span.lo,
917                     hi,
918                     false,
919                 );
920                 let item_vec = items.collect::<Vec<_>>();
921                 let fmt = ListFormatting {
922                     tactic: DefinitiveListTactic::Mixed,
923                     separator: ",",
924                     trailing_separator: SeparatorTactic::Never,
925                     shape: item_shape,
926                     ends_with_newline: false,
927                     preserve_newline: false,
928                     config: context.config,
929                 };
930                 format!("{}({})", name, try_opt!(write_list(&item_vec, &fmt)))
931             }
932             ast::MetaItemKind::NameValue(ref literal) => {
933                 let name = self.name.as_str();
934                 let value = context.snippet(literal.span);
935                 if &*name == "doc" && contains_comment(&value) {
936                     let doc_shape = Shape {
937                         width: cmp::min(shape.width, context.config.comment_width())
938                             .checked_sub(shape.indent.width())
939                             .unwrap_or(0),
940                         ..shape
941                     };
942                     format!(
943                         "{}",
944                         try_opt!(rewrite_comment(&value, false, doc_shape, context.config))
945                     )
946                 } else {
947                     format!("{} = {}", name, value)
948                 }
949             }
950         })
951     }
952 }
953
954 impl Rewrite for ast::Attribute {
955     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
956         try_opt!(self.meta())
957             .rewrite(context, shape)
958             .map(|rw| if self.is_sugared_doc {
959                 rw
960             } else {
961                 let original = context.snippet(self.span);
962                 let prefix = match self.style {
963                     ast::AttrStyle::Inner => "#!",
964                     ast::AttrStyle::Outer => "#",
965                 };
966                 if contains_comment(&original) {
967                     original
968                 } else {
969                     format!("{}[{}]", prefix, rw)
970                 }
971             })
972     }
973 }
974
975 impl<'a> Rewrite for [ast::Attribute] {
976     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
977         let mut result = String::new();
978         if self.is_empty() {
979             return Some(result);
980         }
981         let indent = shape.indent.to_string(context.config);
982
983         for (i, a) in self.iter().enumerate() {
984             let a_str = try_opt!(a.rewrite(context, shape));
985
986             // Write comments and blank lines between attributes.
987             if i > 0 {
988                 let comment = context.snippet(mk_sp(self[i - 1].span.hi, a.span.lo));
989                 // This particular horror show is to preserve line breaks in between doc
990                 // comments. An alternative would be to force such line breaks to start
991                 // with the usual doc comment token.
992                 let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1;
993                 let comment = comment.trim();
994                 if !comment.is_empty() {
995                     let comment = try_opt!(rewrite_comment(
996                         comment,
997                         false,
998                         Shape::legacy(
999                             context.config.comment_width() - shape.indent.width(),
1000                             shape.indent,
1001                         ),
1002                         context.config,
1003                     ));
1004                     result.push_str(&indent);
1005                     result.push_str(&comment);
1006                     result.push('\n');
1007                 } else if multi_line {
1008                     result.push('\n');
1009                 }
1010                 result.push_str(&indent);
1011             }
1012
1013             // Write the attribute itself.
1014             result.push_str(&a_str);
1015
1016             if i < self.len() - 1 {
1017                 result.push('\n');
1018             }
1019         }
1020
1021         Some(result)
1022     }
1023 }