]> git.lizzy.rs Git - rust.git/blob - src/visitor.rs
Idempotent tests and comments in function decls
[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 syntax::ast;
12 use syntax::codemap::{CodeMap, Span, BytePos};
13 use syntax::visit;
14
15 use {MAX_WIDTH, TAB_SPACES, SKIP_ANNOTATION};
16 use changes::ChangeSet;
17
18 pub struct FmtVisitor<'a> {
19     pub codemap: &'a CodeMap,
20     pub changes: ChangeSet<'a>,
21     pub last_pos: BytePos,
22     // TODO RAII util for indenting
23     pub block_indent: usize,
24 }
25
26 impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
27     fn visit_expr(&mut self, ex: &'v ast::Expr) {
28         debug!("visit_expr: {:?} {:?}",
29                self.codemap.lookup_char_pos(ex.span.lo),
30                self.codemap.lookup_char_pos(ex.span.hi));
31         self.format_missing(ex.span.lo);
32         let offset = self.changes.cur_offset_span(ex.span);
33         let new_str = self.rewrite_expr(ex, MAX_WIDTH - offset, offset);
34         self.changes.push_str_span(ex.span, &new_str);
35         self.last_pos = ex.span.hi;
36     }
37
38     fn visit_block(&mut self, b: &'v ast::Block) {
39         debug!("visit_block: {:?} {:?}",
40                self.codemap.lookup_char_pos(b.span.lo),
41                self.codemap.lookup_char_pos(b.span.hi));
42         self.format_missing(b.span.lo);
43
44         self.changes.push_str_span(b.span, "{");
45         self.last_pos = self.last_pos + BytePos(1);
46         self.block_indent += TAB_SPACES;
47
48         for stmt in &b.stmts {
49             self.format_missing_with_indent(stmt.span.lo);
50             self.visit_stmt(&stmt)
51         }
52         match b.expr {
53             Some(ref e) => {
54                 self.format_missing_with_indent(e.span.lo);
55                 self.visit_expr(e);
56             }
57             None => {}
58         }
59
60         self.block_indent -= TAB_SPACES;
61         // TODO we should compress any newlines here to just one
62         self.format_missing_with_indent(b.span.hi - BytePos(1));
63         self.changes.push_str_span(b.span, "}");
64         self.last_pos = b.span.hi;
65     }
66
67     // Note that this only gets called for function defintions. Required methods
68     // on traits do not get handled here.
69     fn visit_fn(&mut self,
70                 fk: visit::FnKind<'v>,
71                 fd: &'v ast::FnDecl,
72                 b: &'v ast::Block,
73                 s: Span,
74                 _: ast::NodeId) {
75         self.format_missing(s.lo);
76         self.last_pos = s.lo;
77
78         // TODO need to check against expected indent
79         let indent = self.codemap.lookup_char_pos(s.lo).col.0;
80         match fk {
81             visit::FkItemFn(ident, ref generics, ref unsafety, ref abi, vis) => {
82                 let new_fn = self.rewrite_fn(indent,
83                                              ident,
84                                              fd,
85                                              None,
86                                              generics,
87                                              unsafety,
88                                              abi,
89                                              vis,
90                                              b.span);
91                 self.changes.push_str_span(s, &new_fn);
92             }
93             visit::FkMethod(ident, ref sig, vis) => {
94                 let new_fn = self.rewrite_fn(indent,
95                                              ident,
96                                              fd,
97                                              Some(&sig.explicit_self),
98                                              &sig.generics,
99                                              &sig.unsafety,
100                                              &sig.abi,
101                                              vis.unwrap_or(ast::Visibility::Inherited),
102                                              b.span);
103                 self.changes.push_str_span(s, &new_fn);
104             }
105             visit::FkFnBlock(..) => {}
106         }
107
108         self.last_pos = b.span.lo;
109         self.visit_block(b)
110     }
111
112     fn visit_item(&mut self, item: &'v ast::Item) {
113         if item.attrs.iter().any(|a| is_skip(&a.node.value)) {
114             return;
115         }
116
117         match item.node {
118             ast::Item_::ItemUse(ref vp) => {
119                 match vp.node {
120                     ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
121                         self.format_missing(item.span.lo);
122                         let new_str = self.rewrite_use_list(path, path_list, vp.span);
123                         self.changes.push_str_span(item.span, &new_str);
124                         self.last_pos = item.span.hi;
125                     }
126                     ast::ViewPath_::ViewPathGlob(_) => {
127                         // FIXME convert to list?
128                     }
129                     _ => {}
130                 }
131                 visit::walk_item(self, item);
132             }
133             ast::Item_::ItemImpl(..) => {
134                 self.block_indent += TAB_SPACES;
135                 visit::walk_item(self, item);
136                 self.block_indent -= TAB_SPACES;
137             }
138             _ => {
139                 visit::walk_item(self, item);
140             }
141         }
142     }
143
144     fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
145         if ti.attrs.iter().any(|a| is_skip(&a.node.value)) {
146             return;
147         }
148         visit::walk_trait_item(self, ti)
149     }
150
151     fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
152         if ii.attrs.iter().any(|a| is_skip(&a.node.value)) {
153             return;
154         }
155         visit::walk_impl_item(self, ii)
156     }
157
158     fn visit_mac(&mut self, mac: &'v ast::Mac) {
159         visit::walk_mac(self, mac)
160     }
161
162     fn visit_mod(&mut self, m: &'v ast::Mod, s: Span, _: ast::NodeId) {
163         // Only visit inline mods here.
164         if self.codemap.lookup_char_pos(s.lo).file.name !=
165            self.codemap.lookup_char_pos(m.inner.lo).file.name {
166             return;
167         }
168         visit::walk_mod(self, m);
169     }
170 }
171
172 impl<'a> FmtVisitor<'a> {
173     pub fn from_codemap<'b>(codemap: &'b CodeMap) -> FmtVisitor<'b> {
174         FmtVisitor {
175             codemap: codemap,
176             changes: ChangeSet::from_codemap(codemap),
177             last_pos: BytePos(0),
178             block_indent: 0,
179         }
180     }
181
182     pub fn snippet(&self, span: Span) -> String {
183         match self.codemap.span_to_snippet(span) {
184             Ok(s) => s,
185             Err(_) => {
186                 println!("Couldn't make snippet for span {:?}->{:?}",
187                          self.codemap.lookup_char_pos(span.lo),
188                          self.codemap.lookup_char_pos(span.hi));
189                 "".to_string()
190             }
191         }
192     }
193 }
194
195 fn is_skip(meta_item: &ast::MetaItem) -> bool {
196     match meta_item.node {
197         ast::MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION,
198         _ => false,
199     }
200 }