]> git.lizzy.rs Git - rust.git/blob - src/visitor.rs
Comments in function decls and annotations/doc comments
[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         let indent = self.block_indent;
79         match fk {
80             visit::FkItemFn(ident, ref generics, ref unsafety, ref abi, vis) => {
81                 let new_fn = self.rewrite_fn(indent,
82                                              ident,
83                                              fd,
84                                              None,
85                                              generics,
86                                              unsafety,
87                                              abi,
88                                              vis,
89                                              b.span);
90                 self.changes.push_str_span(s, &new_fn);
91             }
92             visit::FkMethod(ident, ref sig, vis) => {
93                 let new_fn = self.rewrite_fn(indent,
94                                              ident,
95                                              fd,
96                                              Some(&sig.explicit_self),
97                                              &sig.generics,
98                                              &sig.unsafety,
99                                              &sig.abi,
100                                              vis.unwrap_or(ast::Visibility::Inherited),
101                                              b.span);
102                 self.changes.push_str_span(s, &new_fn);
103             }
104             visit::FkFnBlock(..) => {}
105         }
106
107         self.last_pos = b.span.lo;
108         self.visit_block(b)
109     }
110
111     fn visit_item(&mut self, item: &'v ast::Item) {
112         if self.visit_attrs(&item.attrs) {
113             return;
114         }
115
116         match item.node {
117             ast::Item_::ItemUse(ref vp) => {
118                 match vp.node {
119                     ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
120                         self.format_missing(item.span.lo);
121                         let new_str = self.rewrite_use_list(path, path_list, vp.span);
122                         self.changes.push_str_span(item.span, &new_str);
123                         self.last_pos = item.span.hi;
124                     }
125                     ast::ViewPath_::ViewPathGlob(_) => {
126                         // FIXME convert to list?
127                     }
128                     _ => {}
129                 }
130                 visit::walk_item(self, item);
131             }
132             ast::Item_::ItemImpl(..) => {
133                 self.block_indent += TAB_SPACES;
134                 visit::walk_item(self, item);
135                 self.block_indent -= TAB_SPACES;
136             }
137             ast::Item_::ItemMod(_) => {
138                 self.block_indent += TAB_SPACES;
139                 visit::walk_item(self, item);
140                 self.block_indent -= TAB_SPACES;
141             }
142             _ => {
143                 visit::walk_item(self, item);
144             }
145         }
146     }
147
148     fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
149         if self.visit_attrs(&ti.attrs) {
150             return;
151         }
152         visit::walk_trait_item(self, ti)
153     }
154
155     fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
156         if self.visit_attrs(&ii.attrs) {
157             return;
158         }
159         visit::walk_impl_item(self, ii)
160     }
161
162     fn visit_mac(&mut self, mac: &'v ast::Mac) {
163         visit::walk_mac(self, mac)
164     }
165
166     fn visit_mod(&mut self, m: &'v ast::Mod, s: Span, _: ast::NodeId) {
167         // Only visit inline mods here.
168         if self.codemap.lookup_char_pos(s.lo).file.name !=
169            self.codemap.lookup_char_pos(m.inner.lo).file.name {
170             return;
171         }
172         visit::walk_mod(self, m);
173     }
174 }
175
176 impl<'a> FmtVisitor<'a> {
177     pub fn from_codemap<'b>(codemap: &'b CodeMap) -> FmtVisitor<'b> {
178         FmtVisitor {
179             codemap: codemap,
180             changes: ChangeSet::from_codemap(codemap),
181             last_pos: BytePos(0),
182             block_indent: 0,
183         }
184     }
185
186     pub fn snippet(&self, span: Span) -> String {
187         match self.codemap.span_to_snippet(span) {
188             Ok(s) => s,
189             Err(_) => {
190                 println!("Couldn't make snippet for span {:?}->{:?}",
191                          self.codemap.lookup_char_pos(span.lo),
192                          self.codemap.lookup_char_pos(span.hi));
193                 "".to_string()
194             }
195         }
196     }
197
198     // Returns true if we should skip the following item.
199     fn visit_attrs(&mut self, attrs: &[ast::Attribute]) -> bool {
200         for a in attrs {
201             self.format_missing_with_indent(a.span.lo);
202             if is_skip(&a.node.value) {
203                 return true;
204             }
205
206             let attr_str = self.snippet(a.span);
207             self.changes.push_str_span(a.span, &attr_str);
208             self.last_pos = a.span.hi;
209         }
210
211         false
212     }
213 }
214
215 fn is_skip(meta_item: &ast::MetaItem) -> bool {
216     match meta_item.node {
217         ast::MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION,
218         _ => false,
219     }
220 }