]> git.lizzy.rs Git - rust.git/blob - src/visitor.rs
Extract out more files
[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};
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                 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                 self.changes.push_str_span(s, &new_fn);
102             }
103             visit::FkFnBlock(..) => {}
104         }
105
106         self.last_pos = b.span.lo;
107         self.visit_block(b)
108     }
109
110     fn visit_item(&mut self, item: &'v ast::Item) {
111         match item.node {
112             ast::Item_::ItemUse(ref vp) => {
113                 match vp.node {
114                     ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
115                         self.format_missing(item.span.lo);
116                         let new_str = self.rewrite_use_list(path, path_list, vp.span);
117                         self.changes.push_str_span(item.span, &new_str);
118                         self.last_pos = item.span.hi;
119                     }
120                     ast::ViewPath_::ViewPathGlob(_) => {
121                         // FIXME convert to list?
122                     }
123                     _ => {}
124                 }
125                 visit::walk_item(self, item);
126             }
127             ast::Item_::ItemImpl(..) => {
128                 self.block_indent += TAB_SPACES;
129                 visit::walk_item(self, item);
130                 self.block_indent -= TAB_SPACES;
131             }
132             _ => {
133                 visit::walk_item(self, item);
134             }
135         }
136     }
137
138     fn visit_mac(&mut self, mac: &'v ast::Mac) {
139         visit::walk_mac(self, mac)
140     }
141
142     fn visit_mod(&mut self, m: &'v ast::Mod, s: Span, _: ast::NodeId) {
143         // Only visit inline mods here.
144         if self.codemap.lookup_char_pos(s.lo).file.name !=
145            self.codemap.lookup_char_pos(m.inner.lo).file.name {
146             return;
147         }
148         visit::walk_mod(self, m);
149     }
150 }
151
152 impl<'a> FmtVisitor<'a> {
153     pub fn from_codemap<'b>(codemap: &'b CodeMap) -> FmtVisitor<'b> {
154         FmtVisitor {
155             codemap: codemap,
156             changes: ChangeSet::from_codemap(codemap),
157             last_pos: BytePos(0),
158             block_indent: 0,
159         }
160     }
161
162     pub fn snippet(&self, span: Span) -> String {
163         match self.codemap.span_to_snippet(span) {
164             Ok(s) => s,
165             Err(_) => {
166                 println!("Couldn't make snippet for span {:?}->{:?}",
167                          self.codemap.lookup_char_pos(span.lo),
168                          self.codemap.lookup_char_pos(span.hi));
169                 "".to_string()
170             }
171         }
172     }
173 }