]> git.lizzy.rs Git - rust.git/blob - src/visitor.rs
Merge pull request #1588 from topecongiro/nesting-macro
[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, ptr, visit};
14 use syntax::codemap::{self, CodeMap, Span, BytePos};
15 use syntax::parse::ParseSess;
16
17 use strings::string_buffer::StringBuffer;
18
19 use {Indent, Shape};
20 use utils;
21 use codemap::{LineRangeUtils, SpanUtils};
22 use config::Config;
23 use rewrite::{Rewrite, RewriteContext};
24 use comment::rewrite_comment;
25 use macros::{rewrite_macro, MacroPosition};
26 use items::{rewrite_static, rewrite_associated_type, rewrite_associated_impl_type,
27             rewrite_type_alias, format_impl, format_trait};
28
29 fn is_use_item(item: &ast::Item) -> bool {
30     match item.node {
31         ast::ItemKind::Use(_) => true,
32         _ => false,
33     }
34 }
35
36 fn item_bound(item: &ast::Item) -> Span {
37     item.attrs
38         .iter()
39         .map(|attr| attr.span)
40         .fold(item.span, |bound, span| {
41             Span {
42                 lo: cmp::min(bound.lo, span.lo),
43                 hi: cmp::max(bound.hi, span.hi),
44                 expn_id: span.expn_id,
45             }
46         })
47 }
48
49 pub struct FmtVisitor<'a> {
50     pub parse_session: &'a ParseSess,
51     pub codemap: &'a CodeMap,
52     pub buffer: StringBuffer,
53     pub last_pos: BytePos,
54     // FIXME: use an RAII util or closure for indenting
55     pub block_indent: Indent,
56     pub config: &'a Config,
57     pub failed: bool,
58 }
59
60 impl<'a> FmtVisitor<'a> {
61     fn visit_stmt(&mut self, stmt: &ast::Stmt) {
62         debug!("visit_stmt: {:?} {:?}",
63                self.codemap.lookup_char_pos(stmt.span.lo),
64                self.codemap.lookup_char_pos(stmt.span.hi));
65
66         // FIXME(#434): Move this check to somewhere more central, eg Rewrite.
67         if !self.config
68                .file_lines()
69                .intersects(&self.codemap.lookup_line_range(stmt.span)) {
70             return;
71         }
72
73         match stmt.node {
74             ast::StmtKind::Item(ref item) => {
75                 self.visit_item(item);
76             }
77             ast::StmtKind::Local(..) |
78             ast::StmtKind::Expr(..) |
79             ast::StmtKind::Semi(..) => {
80                 let rewrite =
81                     stmt.rewrite(&self.get_context(),
82                                  Shape::indented(self.block_indent, self.config));
83                 self.push_rewrite(stmt.span, rewrite);
84             }
85             ast::StmtKind::Mac(ref mac) => {
86                 let (ref mac, _macro_style, _) = **mac;
87                 self.visit_mac(mac, None, MacroPosition::Statement);
88                 self.format_missing(stmt.span.hi);
89             }
90         }
91     }
92
93     pub fn visit_block(&mut self, b: &ast::Block) {
94         debug!("visit_block: {:?} {:?}",
95                self.codemap.lookup_char_pos(b.span.lo),
96                self.codemap.lookup_char_pos(b.span.hi));
97
98         // Check if this block has braces.
99         let snippet = self.snippet(b.span);
100         let has_braces = snippet.starts_with('{') || snippet.starts_with("unsafe");
101         let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
102
103         self.last_pos = self.last_pos + brace_compensation;
104         self.block_indent = self.block_indent.block_indent(self.config);
105         self.buffer.push_str("{");
106
107         for stmt in &b.stmts {
108             self.visit_stmt(stmt)
109         }
110
111         if !b.stmts.is_empty() {
112             if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
113                 if utils::semicolon_for_expr(expr) {
114                     self.buffer.push_str(";");
115                 }
116             }
117         }
118
119         // FIXME: we should compress any newlines here to just one
120         self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation);
121         self.close_block();
122         self.last_pos = source!(self, b.span).hi;
123     }
124
125     // FIXME: this is a terrible hack to indent the comments between the last
126     // item in the block and the closing brace to the block's level.
127     // The closing brace itself, however, should be indented at a shallower
128     // level.
129     fn close_block(&mut self) {
130         let total_len = self.buffer.len;
131         let chars_too_many = if self.config.hard_tabs() {
132             1
133         } else {
134             self.config.tab_spaces()
135         };
136         self.buffer.truncate(total_len - chars_too_many);
137         self.buffer.push_str("}");
138         self.block_indent = self.block_indent.block_unindent(self.config);
139     }
140
141     // Note that this only gets called for function definitions. Required methods
142     // on traits do not get handled here.
143     fn visit_fn(&mut self,
144                 fk: visit::FnKind,
145                 fd: &ast::FnDecl,
146                 s: Span,
147                 _: ast::NodeId,
148                 defaultness: ast::Defaultness) {
149         let indent = self.block_indent;
150         let block;
151         let rewrite = match fk {
152             visit::FnKind::ItemFn(ident, generics, unsafety, constness, abi, vis, b) => {
153                 block = b;
154                 self.rewrite_fn(indent,
155                                 ident,
156                                 fd,
157                                 generics,
158                                 unsafety,
159                                 constness.node,
160                                 defaultness,
161                                 abi,
162                                 vis,
163                                 codemap::mk_sp(s.lo, b.span.lo),
164                                 &b)
165             }
166             visit::FnKind::Method(ident, sig, vis, b) => {
167                 block = b;
168                 self.rewrite_fn(indent,
169                                 ident,
170                                 fd,
171                                 &sig.generics,
172                                 sig.unsafety,
173                                 sig.constness.node,
174                                 defaultness,
175                                 sig.abi,
176                                 vis.unwrap_or(&ast::Visibility::Inherited),
177                                 codemap::mk_sp(s.lo, b.span.lo),
178                                 &b)
179             }
180             visit::FnKind::Closure(_) => unreachable!(),
181         };
182
183         if let Some(fn_str) = rewrite {
184             self.format_missing_with_indent(source!(self, s).lo);
185             self.buffer.push_str(&fn_str);
186             if let Some(c) = fn_str.chars().last() {
187                 if c == '}' {
188                     self.last_pos = source!(self, block.span).hi;
189                     return;
190                 }
191             }
192         } else {
193             self.format_missing(source!(self, block.span).lo);
194         }
195
196         self.last_pos = source!(self, block.span).lo;
197         self.visit_block(block)
198     }
199
200     pub fn visit_item(&mut self, item: &ast::Item) {
201         // This is where we bail out if there is a skip attribute. This is only
202         // complex in the module case. It is complex because the module could be
203         // in a separate file and there might be attributes in both files, but
204         // the AST lumps them all together.
205         match item.node {
206             ast::ItemKind::Mod(ref m) => {
207                 let outer_file = self.codemap.lookup_char_pos(item.span.lo).file;
208                 let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file;
209                 if outer_file.name == inner_file.name {
210                     // Module is inline, in this case we treat modules like any
211                     // other item.
212                     if self.visit_attrs(&item.attrs) {
213                         self.push_rewrite(item.span, None);
214                         return;
215                     }
216                 } else if utils::contains_skip(&item.attrs) {
217                     // Module is not inline, but should be skipped.
218                     return;
219                 } else {
220                     // Module is not inline and should not be skipped. We want
221                     // to process only the attributes in the current file.
222                     let attrs = item.attrs
223                         .iter()
224                         .filter_map(|a| {
225                             let attr_file = self.codemap.lookup_char_pos(a.span.lo).file;
226                             if attr_file.name == outer_file.name {
227                                 Some(a.clone())
228                             } else {
229                                 None
230                             }
231                         })
232                         .collect::<Vec<_>>();
233                     // Assert because if we should skip it should be caught by
234                     // the above case.
235                     assert!(!self.visit_attrs(&attrs));
236                 }
237             }
238             _ => {
239                 if self.visit_attrs(&item.attrs) {
240                     self.push_rewrite(item.span, None);
241                     return;
242                 }
243             }
244         }
245
246         match item.node {
247             ast::ItemKind::Use(ref vp) => {
248                 self.format_import(&item.vis, vp, item.span);
249             }
250             ast::ItemKind::Impl(..) => {
251                 self.format_missing_with_indent(source!(self, item.span).lo);
252                 if let Some(impl_str) = format_impl(&self.get_context(), item, self.block_indent) {
253                     self.buffer.push_str(&impl_str);
254                     self.last_pos = source!(self, item.span).hi;
255                 }
256             }
257             ast::ItemKind::Trait(..) => {
258                 self.format_missing_with_indent(item.span.lo);
259                 if let Some(trait_str) = format_trait(&self.get_context(),
260                                                       item,
261                                                       self.block_indent) {
262                     self.buffer.push_str(&trait_str);
263                     self.last_pos = source!(self, item.span).hi;
264                 }
265             }
266             ast::ItemKind::ExternCrate(_) => {
267                 self.format_missing_with_indent(source!(self, item.span).lo);
268                 let new_str = self.snippet(item.span);
269                 self.buffer.push_str(&new_str);
270                 self.last_pos = source!(self, item.span).hi;
271             }
272             ast::ItemKind::Struct(ref def, ref generics) => {
273                 let rewrite = {
274                     let indent = self.block_indent;
275                     let context = self.get_context();
276                     ::items::format_struct(&context,
277                                            "struct ",
278                                            item.ident,
279                                            &item.vis,
280                                            def,
281                                            Some(generics),
282                                            item.span,
283                                            indent,
284                                            None)
285                         .map(|s| match *def {
286                                  ast::VariantData::Tuple(..) => s + ";",
287                                  _ => s,
288                              })
289                 };
290                 self.push_rewrite(item.span, rewrite);
291             }
292             ast::ItemKind::Enum(ref def, ref generics) => {
293                 self.format_missing_with_indent(source!(self, item.span).lo);
294                 self.visit_enum(item.ident, &item.vis, def, generics, item.span);
295                 self.last_pos = source!(self, item.span).hi;
296             }
297             ast::ItemKind::Mod(ref module) => {
298                 self.format_missing_with_indent(source!(self, item.span).lo);
299                 self.format_mod(module, &item.vis, item.span, item.ident);
300             }
301             ast::ItemKind::Mac(ref mac) => {
302                 self.visit_mac(mac, Some(item.ident), MacroPosition::Item);
303             }
304             ast::ItemKind::ForeignMod(ref foreign_mod) => {
305                 self.format_missing_with_indent(source!(self, item.span).lo);
306                 self.format_foreign_mod(foreign_mod, item.span);
307             }
308             ast::ItemKind::Static(ref ty, mutability, ref expr) => {
309                 let rewrite = rewrite_static("static",
310                                              &item.vis,
311                                              item.ident,
312                                              ty,
313                                              mutability,
314                                              Some(expr),
315                                              self.block_indent,
316                                              &self.get_context());
317                 self.push_rewrite(item.span, rewrite);
318             }
319             ast::ItemKind::Const(ref ty, ref expr) => {
320                 let rewrite = rewrite_static("const",
321                                              &item.vis,
322                                              item.ident,
323                                              ty,
324                                              ast::Mutability::Immutable,
325                                              Some(expr),
326                                              self.block_indent,
327                                              &self.get_context());
328                 self.push_rewrite(item.span, rewrite);
329             }
330             ast::ItemKind::DefaultImpl(..) => {
331                 // FIXME(#78): format impl definitions.
332             }
333             ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
334                 self.visit_fn(visit::FnKind::ItemFn(item.ident,
335                                                     generics,
336                                                     unsafety,
337                                                     constness,
338                                                     abi,
339                                                     &item.vis,
340                                                     body),
341                               decl,
342                               item.span,
343                               item.id,
344                               ast::Defaultness::Final)
345             }
346             ast::ItemKind::Ty(ref ty, ref generics) => {
347                 let rewrite = rewrite_type_alias(&self.get_context(),
348                                                  self.block_indent,
349                                                  item.ident,
350                                                  ty,
351                                                  generics,
352                                                  &item.vis,
353                                                  item.span);
354                 self.push_rewrite(item.span, rewrite);
355             }
356             ast::ItemKind::Union(..) => {
357                 // FIXME(#1157): format union definitions.
358             }
359         }
360     }
361
362     pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
363         if self.visit_attrs(&ti.attrs) {
364             self.push_rewrite(ti.span, None);
365             return;
366         }
367
368         match ti.node {
369             ast::TraitItemKind::Const(ref ty, ref expr_opt) => {
370                 let rewrite = rewrite_static("const",
371                                              &ast::Visibility::Inherited,
372                                              ti.ident,
373                                              ty,
374                                              ast::Mutability::Immutable,
375                                              expr_opt.as_ref(),
376                                              self.block_indent,
377                                              &self.get_context());
378                 self.push_rewrite(ti.span, rewrite);
379             }
380             ast::TraitItemKind::Method(ref sig, None) => {
381                 let indent = self.block_indent;
382                 let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
383                 self.push_rewrite(ti.span, rewrite);
384             }
385             ast::TraitItemKind::Method(ref sig, Some(ref body)) => {
386                 self.visit_fn(visit::FnKind::Method(ti.ident, sig, None, body),
387                               &sig.decl,
388                               ti.span,
389                               ti.id,
390                               ast::Defaultness::Final);
391             }
392             ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => {
393                 let rewrite = rewrite_associated_type(ti.ident,
394                                                       type_default.as_ref(),
395                                                       Some(type_param_bounds),
396                                                       &self.get_context(),
397                                                       self.block_indent);
398                 self.push_rewrite(ti.span, rewrite);
399             }
400             ast::TraitItemKind::Macro(ref mac) => {
401                 self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
402             }
403         }
404     }
405
406     pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
407         if self.visit_attrs(&ii.attrs) {
408             self.push_rewrite(ii.span, None);
409             return;
410         }
411
412         match ii.node {
413             ast::ImplItemKind::Method(ref sig, ref body) => {
414                 self.visit_fn(visit::FnKind::Method(ii.ident, sig, Some(&ii.vis), body),
415                               &sig.decl,
416                               ii.span,
417                               ii.id,
418                               ii.defaultness);
419             }
420             ast::ImplItemKind::Const(ref ty, ref expr) => {
421                 let rewrite = rewrite_static("const",
422                                              &ii.vis,
423                                              ii.ident,
424                                              ty,
425                                              ast::Mutability::Immutable,
426                                              Some(expr),
427                                              self.block_indent,
428                                              &self.get_context());
429                 self.push_rewrite(ii.span, rewrite);
430             }
431             ast::ImplItemKind::Type(ref ty) => {
432                 let rewrite = rewrite_associated_impl_type(ii.ident,
433                                                            ii.defaultness,
434                                                            Some(ty),
435                                                            None,
436                                                            &self.get_context(),
437                                                            self.block_indent);
438                 self.push_rewrite(ii.span, rewrite);
439             }
440             ast::ImplItemKind::Macro(ref mac) => {
441                 self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
442             }
443         }
444     }
445
446     fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPosition) {
447         // 1 = ;
448         let shape = Shape::indented(self.block_indent, self.config)
449             .sub_width(1)
450             .unwrap();
451         let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
452         self.push_rewrite(mac.span, rewrite);
453     }
454
455     fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
456         self.format_missing_with_indent(source!(self, span).lo);
457         self.failed = match rewrite {
458             Some(ref s) if s.rewrite(&self.get_context(),
459                                      Shape::indented(self.block_indent, self.config))
460                                .is_none() => true,
461             None => true,
462             _ => self.failed,
463         };
464         let result = rewrite.unwrap_or_else(|| self.snippet(span));
465         self.buffer.push_str(&result);
466         self.last_pos = source!(self, span).hi;
467     }
468
469     pub fn from_codemap(parse_session: &'a ParseSess, config: &'a Config) -> FmtVisitor<'a> {
470         FmtVisitor {
471             parse_session: parse_session,
472             codemap: parse_session.codemap(),
473             buffer: StringBuffer::new(),
474             last_pos: BytePos(0),
475             block_indent: Indent::empty(),
476             config: config,
477             failed: false,
478         }
479     }
480
481     pub fn snippet(&self, span: Span) -> String {
482         match self.codemap.span_to_snippet(span) {
483             Ok(s) => s,
484             Err(_) => {
485                 println!("Couldn't make snippet for span {:?}->{:?}",
486                          self.codemap.lookup_char_pos(span.lo),
487                          self.codemap.lookup_char_pos(span.hi));
488                 "".to_owned()
489             }
490         }
491     }
492
493     // Returns true if we should skip the following item.
494     pub fn visit_attrs(&mut self, attrs: &[ast::Attribute]) -> bool {
495         if utils::contains_skip(attrs) {
496             return true;
497         }
498
499         let outers: Vec<_> = attrs
500             .iter()
501             .filter(|a| a.style == ast::AttrStyle::Outer)
502             .cloned()
503             .collect();
504         if outers.is_empty() {
505             return false;
506         }
507
508         let first = &outers[0];
509         self.format_missing_with_indent(source!(self, first.span).lo);
510
511         let rewrite = outers
512             .rewrite(&self.get_context(),
513                      Shape::indented(self.block_indent, self.config))
514             .unwrap();
515         self.buffer.push_str(&rewrite);
516         let last = outers.last().unwrap();
517         self.last_pos = source!(self, last.span).hi;
518         false
519     }
520
521     fn walk_mod_items(&mut self, m: &ast::Mod) {
522         let mut items_left: &[ptr::P<ast::Item>] = &m.items;
523         while !items_left.is_empty() {
524             // If the next item is a `use` declaration, then extract it and any subsequent `use`s
525             // to be potentially reordered within `format_imports`. Otherwise, just format the
526             // next item for output.
527             if self.config.reorder_imports() && is_use_item(&*items_left[0]) {
528                 let reorder_imports_in_group = self.config.reorder_imports_in_group();
529                 let mut last = self.codemap.lookup_line_range(item_bound(&items_left[0]));
530                 let use_item_length = items_left
531                     .iter()
532                     .take_while(|ppi| {
533                         is_use_item(&***ppi) &&
534                         (!reorder_imports_in_group ||
535                          {
536                              let current = self.codemap.lookup_line_range(item_bound(&ppi));
537                              let in_same_group = current.lo < last.hi + 2;
538                              last = current;
539                              in_same_group
540                          })
541                     })
542                     .count();
543                 let (use_items, rest) = items_left.split_at(use_item_length);
544                 self.format_imports(use_items);
545                 items_left = rest;
546             } else {
547                 // `unwrap()` is safe here because we know `items_left`
548                 // has elements from the loop condition
549                 let (item, rest) = items_left.split_first().unwrap();
550                 self.visit_item(item);
551                 items_left = rest;
552             }
553         }
554     }
555
556     fn format_mod(&mut self, m: &ast::Mod, vis: &ast::Visibility, s: Span, ident: ast::Ident) {
557         // Decide whether this is an inline mod or an external mod.
558         let local_file_name = self.codemap.span_to_filename(s);
559         let inner_span = source!(self, m.inner);
560         let is_internal = !(inner_span.lo.0 == 0 && inner_span.hi.0 == 0) &&
561                           local_file_name == self.codemap.span_to_filename(inner_span);
562
563         self.buffer.push_str(&*utils::format_visibility(vis));
564         self.buffer.push_str("mod ");
565         self.buffer.push_str(&ident.to_string());
566
567         if is_internal {
568             self.buffer.push_str(" {");
569             // Hackery to account for the closing }.
570             let mod_lo = self.codemap.span_after(source!(self, s), "{");
571             let body_snippet =
572                 self.snippet(codemap::mk_sp(mod_lo, source!(self, m.inner).hi - BytePos(1)));
573             let body_snippet = body_snippet.trim();
574             if body_snippet.is_empty() {
575                 self.buffer.push_str("}");
576             } else {
577                 self.last_pos = mod_lo;
578                 self.block_indent = self.block_indent.block_indent(self.config);
579                 self.walk_mod_items(m);
580                 self.format_missing_with_indent(source!(self, m.inner).hi - BytePos(1));
581                 self.close_block();
582             }
583             self.last_pos = source!(self, m.inner).hi;
584         } else {
585             self.buffer.push_str(";");
586             self.last_pos = source!(self, s).hi;
587         }
588     }
589
590     pub fn format_separate_mod(&mut self, m: &ast::Mod) {
591         let filemap = self.codemap.lookup_char_pos(m.inner.lo).file;
592         self.last_pos = filemap.start_pos;
593         self.block_indent = Indent::empty();
594         self.walk_mod_items(m);
595         self.format_missing_with_indent(filemap.end_pos);
596     }
597
598     pub fn get_context(&self) -> RewriteContext {
599         RewriteContext {
600             parse_session: self.parse_session,
601             codemap: self.codemap,
602             config: self.config,
603             inside_macro: false,
604         }
605     }
606 }
607
608 impl<'a> Rewrite for [ast::Attribute] {
609     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
610         let mut result = String::new();
611         if self.is_empty() {
612             return Some(result);
613         }
614         let indent = shape.indent.to_string(context.config);
615
616         for (i, a) in self.iter().enumerate() {
617             let mut a_str = context.snippet(a.span);
618
619             // Write comments and blank lines between attributes.
620             if i > 0 {
621                 let comment = context.snippet(codemap::mk_sp(self[i - 1].span.hi, a.span.lo));
622                 // This particular horror show is to preserve line breaks in between doc
623                 // comments. An alternative would be to force such line breaks to start
624                 // with the usual doc comment token.
625                 let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1;
626                 let comment = comment.trim();
627                 if !comment.is_empty() {
628                     let comment =
629                         try_opt!(rewrite_comment(comment,
630                                                  false,
631                                                  Shape::legacy(context.config.comment_width() -
632                                                                shape.indent.width(),
633                                                                shape.indent),
634                                                  context.config));
635                     result.push_str(&indent);
636                     result.push_str(&comment);
637                     result.push('\n');
638                 } else if multi_line {
639                     result.push('\n');
640                 }
641                 result.push_str(&indent);
642             }
643
644             if a_str.starts_with("//") {
645                 a_str = try_opt!(rewrite_comment(&a_str,
646                                                  false,
647                                                  Shape::legacy(context.config.comment_width() -
648                                                                shape.indent.width(),
649                                                                shape.indent),
650                                                  context.config));
651             }
652
653             // Write the attribute itself.
654             result.push_str(&a_str);
655
656             if i < self.len() - 1 {
657                 result.push('\n');
658             }
659         }
660
661         Some(result)
662     }
663 }