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.
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.
12 use syntax::codemap::{CodeMap, Span, BytePos};
15 use {MAX_WIDTH, TAB_SPACES, SKIP_ANNOTATION};
16 use changes::ChangeSet;
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,
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;
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);
44 self.changes.push_str_span(b.span, "{");
45 self.last_pos = self.last_pos + BytePos(1);
46 self.block_indent += TAB_SPACES;
48 for stmt in &b.stmts {
49 self.format_missing_with_indent(stmt.span.lo);
50 self.visit_stmt(&stmt)
54 self.format_missing_with_indent(e.span.lo);
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;
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>,
75 self.format_missing(s.lo);
78 // TODO need to check against expected indent
79 let indent = self.codemap.lookup_char_pos(s.lo).col.0;
81 visit::FkItemFn(ident, ref generics, ref unsafety, ref abi, vis) => {
82 let new_fn = self.rewrite_fn(indent,
91 self.changes.push_str_span(s, &new_fn);
93 visit::FkMethod(ident, ref sig, vis) => {
94 let new_fn = self.rewrite_fn(indent,
97 Some(&sig.explicit_self),
101 vis.unwrap_or(ast::Visibility::Inherited),
103 self.changes.push_str_span(s, &new_fn);
105 visit::FkFnBlock(..) => {}
108 self.last_pos = b.span.lo;
112 fn visit_item(&mut self, item: &'v ast::Item) {
113 if item.attrs.iter().any(|a| is_skip(&a.node.value)) {
118 ast::Item_::ItemUse(ref vp) => {
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;
126 ast::ViewPath_::ViewPathGlob(_) => {
127 // FIXME convert to list?
131 visit::walk_item(self, item);
133 ast::Item_::ItemImpl(..) => {
134 self.block_indent += TAB_SPACES;
135 visit::walk_item(self, item);
136 self.block_indent -= TAB_SPACES;
138 ast::Item_::ItemMod(_) => {
139 self.block_indent += TAB_SPACES;
140 visit::walk_item(self, item);
141 self.block_indent -= TAB_SPACES;
144 visit::walk_item(self, item);
149 fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
150 if ti.attrs.iter().any(|a| is_skip(&a.node.value)) {
153 visit::walk_trait_item(self, ti)
156 fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
157 if ii.attrs.iter().any(|a| is_skip(&a.node.value)) {
160 visit::walk_impl_item(self, ii)
163 fn visit_mac(&mut self, mac: &'v ast::Mac) {
164 visit::walk_mac(self, mac)
167 fn visit_mod(&mut self, m: &'v ast::Mod, s: Span, _: ast::NodeId) {
168 // Only visit inline mods here.
169 if self.codemap.lookup_char_pos(s.lo).file.name !=
170 self.codemap.lookup_char_pos(m.inner.lo).file.name {
173 visit::walk_mod(self, m);
177 impl<'a> FmtVisitor<'a> {
178 pub fn from_codemap<'b>(codemap: &'b CodeMap) -> FmtVisitor<'b> {
181 changes: ChangeSet::from_codemap(codemap),
182 last_pos: BytePos(0),
187 pub fn snippet(&self, span: Span) -> String {
188 match self.codemap.span_to_snippet(span) {
191 println!("Couldn't make snippet for span {:?}->{:?}",
192 self.codemap.lookup_char_pos(span.lo),
193 self.codemap.lookup_char_pos(span.hi));
200 fn is_skip(meta_item: &ast::MetaItem) -> bool {
201 match meta_item.node {
202 ast::MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION,