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::{self, CodeMap, Span, BytePos};
17 use {IDEAL_WIDTH, MAX_WIDTH, TAB_SPACES, SKIP_ANNOTATION};
18 use changes::ChangeSet;
20 pub struct FmtVisitor<'a> {
21 pub codemap: &'a CodeMap,
22 pub changes: ChangeSet<'a>,
23 pub last_pos: BytePos,
24 // TODO RAII util for indenting
25 pub block_indent: usize,
28 impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
29 fn visit_expr(&mut self, ex: &'v ast::Expr) {
30 debug!("visit_expr: {:?} {:?}",
31 self.codemap.lookup_char_pos(ex.span.lo),
32 self.codemap.lookup_char_pos(ex.span.hi));
33 self.format_missing(ex.span.lo);
34 let offset = self.changes.cur_offset_span(ex.span);
35 let new_str = self.rewrite_expr(ex, MAX_WIDTH - offset, offset);
36 self.changes.push_str_span(ex.span, &new_str);
37 self.last_pos = ex.span.hi;
40 fn visit_stmt(&mut self, stmt: &'v ast::Stmt) {
41 // If the stmt is actually an item, then we'll handle any missing spans
42 // there. This is important because of annotations.
43 // Although it might make more sense for the statement span to include
44 // any annotations on the item.
45 let skip_missing = match stmt.node {
46 ast::Stmt_::StmtDecl(ref decl, _) => {
48 ast::Decl_::DeclItem(_) => true,
55 self.format_missing_with_indent(stmt.span.lo);
57 visit::walk_stmt(self, stmt);
60 fn visit_block(&mut self, b: &'v ast::Block) {
61 debug!("visit_block: {:?} {:?}",
62 self.codemap.lookup_char_pos(b.span.lo),
63 self.codemap.lookup_char_pos(b.span.hi));
64 self.format_missing(b.span.lo);
66 self.changes.push_str_span(b.span, "{");
67 self.last_pos = self.last_pos + BytePos(1);
68 self.block_indent += TAB_SPACES;
70 for stmt in &b.stmts {
71 self.visit_stmt(&stmt)
75 self.format_missing_with_indent(e.span.lo);
81 self.block_indent -= TAB_SPACES;
82 // TODO we should compress any newlines here to just one
83 self.format_missing_with_indent(b.span.hi - BytePos(1));
84 self.changes.push_str_span(b.span, "}");
85 self.last_pos = b.span.hi;
88 // Note that this only gets called for function definitions. Required methods
89 // on traits do not get handled here.
90 fn visit_fn(&mut self,
91 fk: visit::FnKind<'v>,
96 self.format_missing_with_indent(s.lo);
99 let indent = self.block_indent;
101 visit::FkItemFn(ident, ref generics, ref unsafety, ref abi, vis) => {
102 let new_fn = self.rewrite_fn(indent,
111 self.changes.push_str_span(s, &new_fn);
113 visit::FkMethod(ident, ref sig, vis) => {
114 let new_fn = self.rewrite_fn(indent,
117 Some(&sig.explicit_self),
121 vis.unwrap_or(ast::Visibility::Inherited),
123 self.changes.push_str_span(s, &new_fn);
125 visit::FkFnBlock(..) => {}
128 self.last_pos = b.span.lo;
132 fn visit_item(&mut self, item: &'v ast::Item) {
133 // Don't look at attributes for modules.
134 // We want to avoid looking at attributes in another file, which the AST
135 // doesn't distinguish. FIXME This is overly conservative and means we miss
136 // attributes on inline modules.
138 ast::Item_::ItemMod(_) => {}
140 if self.visit_attrs(&item.attrs) {
147 ast::Item_::ItemUse(ref vp) => {
148 self.format_missing_with_indent(item.span.lo);
150 ast::ViewPath_::ViewPathList(ref path, ref path_list) => {
151 let block_indent = self.block_indent;
152 let budget = IDEAL_WIDTH - block_indent;
153 if let Some(new_str) = self.rewrite_use_list(block_indent,
158 self.changes.push_str_span(item.span, &new_str);
159 self.last_pos = item.span.hi;
162 ast::ViewPath_::ViewPathGlob(_) => {
163 // FIXME convert to list?
165 ast::ViewPath_::ViewPathSimple(_,_) => {}
167 visit::walk_item(self, item);
169 ast::Item_::ItemImpl(..) |
170 ast::Item_::ItemMod(_) |
171 ast::Item_::ItemTrait(..) => {
172 self.block_indent += TAB_SPACES;
173 visit::walk_item(self, item);
174 self.block_indent -= TAB_SPACES;
176 ast::Item_::ItemExternCrate(_) => {
177 self.format_missing_with_indent(item.span.lo);
178 let new_str = self.snippet(item.span);
179 self.changes.push_str_span(item.span, &new_str);
180 self.last_pos = item.span.hi;
183 visit::walk_item(self, item);
188 fn visit_trait_item(&mut self, ti: &'v ast::TraitItem) {
189 if self.visit_attrs(&ti.attrs) {
192 visit::walk_trait_item(self, ti)
195 fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
196 if self.visit_attrs(&ii.attrs) {
199 visit::walk_impl_item(self, ii)
202 fn visit_mac(&mut self, mac: &'v ast::Mac) {
203 visit::walk_mac(self, mac)
206 fn visit_mod(&mut self, m: &'v ast::Mod, s: Span, _: ast::NodeId) {
207 // Only visit inline mods here.
208 if self.codemap.lookup_char_pos(s.lo).file.name !=
209 self.codemap.lookup_char_pos(m.inner.lo).file.name {
212 visit::walk_mod(self, m);
216 impl<'a> FmtVisitor<'a> {
217 pub fn from_codemap<'b>(codemap: &'b CodeMap) -> FmtVisitor<'b> {
220 changes: ChangeSet::from_codemap(codemap),
221 last_pos: BytePos(0),
226 pub fn snippet(&self, span: Span) -> String {
227 match self.codemap.span_to_snippet(span) {
230 println!("Couldn't make snippet for span {:?}->{:?}",
231 self.codemap.lookup_char_pos(span.lo),
232 self.codemap.lookup_char_pos(span.hi));
238 // Returns true if we should skip the following item.
239 fn visit_attrs(&mut self, attrs: &[ast::Attribute]) -> bool {
240 if attrs.len() == 0 {
244 let first = &attrs[0];
245 self.format_missing_with_indent(first.span.lo);
247 match self.rewrite_attrs(attrs, self.block_indent) {
249 self.changes.push_str_span(first.span, &s);
250 let last = attrs.last().unwrap();
251 self.last_pos = last.span.hi;
258 fn rewrite_attrs(&self, attrs: &[ast::Attribute], indent: usize) -> Option<String> {
259 let mut result = String::new();
260 let indent = utils::make_indent(indent);
262 for (i, a) in attrs.iter().enumerate() {
263 if is_skip(&a.node.value) {
267 let a_str = self.snippet(a.span);
270 let comment = self.snippet(codemap::mk_sp(attrs[i-1].span.hi, a.span.lo));
271 // This particular horror show is to preserve line breaks in between doc
272 // comments. An alternative would be to force such line breaks to start
273 // with the usual doc comment token.
274 let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1;
275 let comment = comment.trim();
276 if comment.len() > 0 {
277 result.push_str(&indent);
278 result.push_str(comment);
280 } else if multi_line {
283 result.push_str(&indent);
286 result.push_str(&a_str);
288 if i < attrs.len() -1 {
297 fn is_skip(meta_item: &ast::MetaItem) -> bool {
298 match meta_item.node {
299 ast::MetaItem_::MetaWord(ref s) => *s == SKIP_ANNOTATION,