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.
13 use syntax::{ast, ptr, visit};
14 use syntax::codemap::{self, CodeMap, Span, BytePos};
15 use syntax::parse::ParseSess;
17 use strings::string_buffer::StringBuffer;
21 use codemap::{LineRangeUtils, SpanUtils};
23 use rewrite::{Rewrite, RewriteContext};
24 use comment::rewrite_comment;
25 use macros::{rewrite_macro, MacroPosition};
26 use items::{rewrite_static, rewrite_associated_type, rewrite_associated_impl_type,
27 rewrite_type_alias, format_impl, format_trait};
29 fn is_use_item(item: &ast::Item) -> bool {
31 ast::ItemKind::Use(_) => true,
36 fn item_bound(item: &ast::Item) -> Span {
39 .map(|attr| attr.span)
40 .fold(item.span, |bound, span| {
42 lo: cmp::min(bound.lo, span.lo),
43 hi: cmp::max(bound.hi, span.hi),
44 expn_id: span.expn_id,
49 pub struct FmtVisitor<'a> {
50 pub parse_session: &'a ParseSess,
51 pub codemap: &'a CodeMap,
52 pub buffer: StringBuffer,
53 pub last_pos: BytePos,
54 // FIXME: use an RAII util or closure for indenting
55 pub block_indent: Indent,
56 pub config: &'a Config,
60 impl<'a> FmtVisitor<'a> {
61 fn visit_stmt(&mut self, stmt: &ast::Stmt) {
62 debug!("visit_stmt: {:?} {:?}",
63 self.codemap.lookup_char_pos(stmt.span.lo),
64 self.codemap.lookup_char_pos(stmt.span.hi));
66 // FIXME(#434): Move this check to somewhere more central, eg Rewrite.
69 .intersects(&self.codemap.lookup_line_range(stmt.span)) {
74 ast::StmtKind::Item(ref item) => {
75 self.visit_item(item);
77 ast::StmtKind::Local(..) |
78 ast::StmtKind::Expr(..) |
79 ast::StmtKind::Semi(..) => {
81 stmt.rewrite(&self.get_context(),
82 Shape::indented(self.block_indent, self.config));
83 self.push_rewrite(stmt.span, rewrite);
85 ast::StmtKind::Mac(ref mac) => {
86 let (ref mac, _macro_style, _) = **mac;
87 self.visit_mac(mac, None, MacroPosition::Statement);
88 self.format_missing(stmt.span.hi);
93 pub fn visit_block(&mut self, b: &ast::Block) {
94 debug!("visit_block: {:?} {:?}",
95 self.codemap.lookup_char_pos(b.span.lo),
96 self.codemap.lookup_char_pos(b.span.hi));
98 // Check if this block has braces.
99 let snippet = self.snippet(b.span);
100 let has_braces = snippet.starts_with('{') || snippet.starts_with("unsafe");
101 let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
103 self.last_pos = self.last_pos + brace_compensation;
104 self.block_indent = self.block_indent.block_indent(self.config);
105 self.buffer.push_str("{");
107 for stmt in &b.stmts {
108 self.visit_stmt(stmt)
111 if !b.stmts.is_empty() {
112 if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
113 if utils::semicolon_for_expr(expr) {
114 self.buffer.push_str(";");
119 // FIXME: we should compress any newlines here to just one
120 self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation);
122 self.last_pos = source!(self, b.span).hi;
125 // FIXME: this is a terrible hack to indent the comments between the last
126 // item in the block and the closing brace to the block's level.
127 // The closing brace itself, however, should be indented at a shallower
129 fn close_block(&mut self) {
130 let total_len = self.buffer.len;
131 let chars_too_many = if self.config.hard_tabs() {
134 self.config.tab_spaces()
136 self.buffer.truncate(total_len - chars_too_many);
137 self.buffer.push_str("}");
138 self.block_indent = self.block_indent.block_unindent(self.config);
141 // Note that this only gets called for function definitions. Required methods
142 // on traits do not get handled here.
143 fn visit_fn(&mut self,
148 defaultness: ast::Defaultness) {
149 let indent = self.block_indent;
151 let rewrite = match fk {
152 visit::FnKind::ItemFn(ident, generics, unsafety, constness, abi, vis, b) => {
154 self.rewrite_fn(indent,
163 codemap::mk_sp(s.lo, b.span.lo),
166 visit::FnKind::Method(ident, sig, vis, b) => {
168 self.rewrite_fn(indent,
176 vis.unwrap_or(&ast::Visibility::Inherited),
177 codemap::mk_sp(s.lo, b.span.lo),
180 visit::FnKind::Closure(_) => unreachable!(),
183 if let Some(fn_str) = rewrite {
184 self.format_missing_with_indent(source!(self, s).lo);
185 self.buffer.push_str(&fn_str);
186 if let Some(c) = fn_str.chars().last() {
188 self.last_pos = source!(self, block.span).hi;
193 self.format_missing(source!(self, block.span).lo);
196 self.last_pos = source!(self, block.span).lo;
197 self.visit_block(block)
200 pub fn visit_item(&mut self, item: &ast::Item) {
201 // This is where we bail out if there is a skip attribute. This is only
202 // complex in the module case. It is complex because the module could be
203 // in a separate file and there might be attributes in both files, but
204 // the AST lumps them all together.
206 ast::ItemKind::Mod(ref m) => {
207 let outer_file = self.codemap.lookup_char_pos(item.span.lo).file;
208 let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file;
209 if outer_file.name == inner_file.name {
210 // Module is inline, in this case we treat modules like any
212 if self.visit_attrs(&item.attrs) {
213 self.push_rewrite(item.span, None);
216 } else if utils::contains_skip(&item.attrs) {
217 // Module is not inline, but should be skipped.
220 // Module is not inline and should not be skipped. We want
221 // to process only the attributes in the current file.
222 let attrs = item.attrs
225 let attr_file = self.codemap.lookup_char_pos(a.span.lo).file;
226 if attr_file.name == outer_file.name {
232 .collect::<Vec<_>>();
233 // Assert because if we should skip it should be caught by
235 assert!(!self.visit_attrs(&attrs));
239 if self.visit_attrs(&item.attrs) {
240 self.push_rewrite(item.span, None);
247 ast::ItemKind::Use(ref vp) => {
248 self.format_import(&item.vis, vp, item.span);
250 ast::ItemKind::Impl(..) => {
251 self.format_missing_with_indent(source!(self, item.span).lo);
252 if let Some(impl_str) = format_impl(&self.get_context(), item, self.block_indent) {
253 self.buffer.push_str(&impl_str);
254 self.last_pos = source!(self, item.span).hi;
257 ast::ItemKind::Trait(..) => {
258 self.format_missing_with_indent(item.span.lo);
259 if let Some(trait_str) = format_trait(&self.get_context(),
262 self.buffer.push_str(&trait_str);
263 self.last_pos = source!(self, item.span).hi;
266 ast::ItemKind::ExternCrate(_) => {
267 self.format_missing_with_indent(source!(self, item.span).lo);
268 let new_str = self.snippet(item.span);
269 self.buffer.push_str(&new_str);
270 self.last_pos = source!(self, item.span).hi;
272 ast::ItemKind::Struct(ref def, ref generics) => {
274 let indent = self.block_indent;
275 let context = self.get_context();
276 ::items::format_struct(&context,
285 .map(|s| match *def {
286 ast::VariantData::Tuple(..) => s + ";",
290 self.push_rewrite(item.span, rewrite);
292 ast::ItemKind::Enum(ref def, ref generics) => {
293 self.format_missing_with_indent(source!(self, item.span).lo);
294 self.visit_enum(item.ident, &item.vis, def, generics, item.span);
295 self.last_pos = source!(self, item.span).hi;
297 ast::ItemKind::Mod(ref module) => {
298 self.format_missing_with_indent(source!(self, item.span).lo);
299 self.format_mod(module, &item.vis, item.span, item.ident);
301 ast::ItemKind::Mac(ref mac) => {
302 self.visit_mac(mac, Some(item.ident), MacroPosition::Item);
304 ast::ItemKind::ForeignMod(ref foreign_mod) => {
305 self.format_missing_with_indent(source!(self, item.span).lo);
306 self.format_foreign_mod(foreign_mod, item.span);
308 ast::ItemKind::Static(ref ty, mutability, ref expr) => {
309 let rewrite = rewrite_static("static",
316 &self.get_context());
317 self.push_rewrite(item.span, rewrite);
319 ast::ItemKind::Const(ref ty, ref expr) => {
320 let rewrite = rewrite_static("const",
324 ast::Mutability::Immutable,
327 &self.get_context());
328 self.push_rewrite(item.span, rewrite);
330 ast::ItemKind::DefaultImpl(..) => {
331 // FIXME(#78): format impl definitions.
333 ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
334 self.visit_fn(visit::FnKind::ItemFn(item.ident,
344 ast::Defaultness::Final)
346 ast::ItemKind::Ty(ref ty, ref generics) => {
347 let rewrite = rewrite_type_alias(&self.get_context(),
354 self.push_rewrite(item.span, rewrite);
356 ast::ItemKind::Union(..) => {
357 // FIXME(#1157): format union definitions.
362 pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
363 if self.visit_attrs(&ti.attrs) {
364 self.push_rewrite(ti.span, None);
369 ast::TraitItemKind::Const(ref ty, ref expr_opt) => {
370 let rewrite = rewrite_static("const",
371 &ast::Visibility::Inherited,
374 ast::Mutability::Immutable,
377 &self.get_context());
378 self.push_rewrite(ti.span, rewrite);
380 ast::TraitItemKind::Method(ref sig, None) => {
381 let indent = self.block_indent;
382 let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
383 self.push_rewrite(ti.span, rewrite);
385 ast::TraitItemKind::Method(ref sig, Some(ref body)) => {
386 self.visit_fn(visit::FnKind::Method(ti.ident, sig, None, body),
390 ast::Defaultness::Final);
392 ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => {
393 let rewrite = rewrite_associated_type(ti.ident,
394 type_default.as_ref(),
395 Some(type_param_bounds),
398 self.push_rewrite(ti.span, rewrite);
400 ast::TraitItemKind::Macro(ref mac) => {
401 self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
406 pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
407 if self.visit_attrs(&ii.attrs) {
408 self.push_rewrite(ii.span, None);
413 ast::ImplItemKind::Method(ref sig, ref body) => {
414 self.visit_fn(visit::FnKind::Method(ii.ident, sig, Some(&ii.vis), body),
420 ast::ImplItemKind::Const(ref ty, ref expr) => {
421 let rewrite = rewrite_static("const",
425 ast::Mutability::Immutable,
428 &self.get_context());
429 self.push_rewrite(ii.span, rewrite);
431 ast::ImplItemKind::Type(ref ty) => {
432 let rewrite = rewrite_associated_impl_type(ii.ident,
438 self.push_rewrite(ii.span, rewrite);
440 ast::ImplItemKind::Macro(ref mac) => {
441 self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
446 fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPosition) {
448 let shape = Shape::indented(self.block_indent, self.config)
451 let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
452 self.push_rewrite(mac.span, rewrite);
455 fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
456 self.format_missing_with_indent(source!(self, span).lo);
457 self.failed = match rewrite {
458 Some(ref s) if s.rewrite(&self.get_context(),
459 Shape::indented(self.block_indent, self.config))
464 let result = rewrite.unwrap_or_else(|| self.snippet(span));
465 self.buffer.push_str(&result);
466 self.last_pos = source!(self, span).hi;
469 pub fn from_codemap(parse_session: &'a ParseSess, config: &'a Config) -> FmtVisitor<'a> {
471 parse_session: parse_session,
472 codemap: parse_session.codemap(),
473 buffer: StringBuffer::new(),
474 last_pos: BytePos(0),
475 block_indent: Indent::empty(),
481 pub fn snippet(&self, span: Span) -> String {
482 match self.codemap.span_to_snippet(span) {
485 println!("Couldn't make snippet for span {:?}->{:?}",
486 self.codemap.lookup_char_pos(span.lo),
487 self.codemap.lookup_char_pos(span.hi));
493 // Returns true if we should skip the following item.
494 pub fn visit_attrs(&mut self, attrs: &[ast::Attribute]) -> bool {
495 if utils::contains_skip(attrs) {
499 let outers: Vec<_> = attrs
501 .filter(|a| a.style == ast::AttrStyle::Outer)
504 if outers.is_empty() {
508 let first = &outers[0];
509 self.format_missing_with_indent(source!(self, first.span).lo);
512 .rewrite(&self.get_context(),
513 Shape::indented(self.block_indent, self.config))
515 self.buffer.push_str(&rewrite);
516 let last = outers.last().unwrap();
517 self.last_pos = source!(self, last.span).hi;
521 fn walk_mod_items(&mut self, m: &ast::Mod) {
522 let mut items_left: &[ptr::P<ast::Item>] = &m.items;
523 while !items_left.is_empty() {
524 // If the next item is a `use` declaration, then extract it and any subsequent `use`s
525 // to be potentially reordered within `format_imports`. Otherwise, just format the
526 // next item for output.
527 if self.config.reorder_imports() && is_use_item(&*items_left[0]) {
528 let reorder_imports_in_group = self.config.reorder_imports_in_group();
529 let mut last = self.codemap.lookup_line_range(item_bound(&items_left[0]));
530 let use_item_length = items_left
533 is_use_item(&***ppi) &&
534 (!reorder_imports_in_group ||
536 let current = self.codemap.lookup_line_range(item_bound(&ppi));
537 let in_same_group = current.lo < last.hi + 2;
543 let (use_items, rest) = items_left.split_at(use_item_length);
544 self.format_imports(use_items);
547 // `unwrap()` is safe here because we know `items_left`
548 // has elements from the loop condition
549 let (item, rest) = items_left.split_first().unwrap();
550 self.visit_item(item);
556 fn format_mod(&mut self, m: &ast::Mod, vis: &ast::Visibility, s: Span, ident: ast::Ident) {
557 // Decide whether this is an inline mod or an external mod.
558 let local_file_name = self.codemap.span_to_filename(s);
559 let inner_span = source!(self, m.inner);
560 let is_internal = !(inner_span.lo.0 == 0 && inner_span.hi.0 == 0) &&
561 local_file_name == self.codemap.span_to_filename(inner_span);
563 self.buffer.push_str(&*utils::format_visibility(vis));
564 self.buffer.push_str("mod ");
565 self.buffer.push_str(&ident.to_string());
568 self.buffer.push_str(" {");
569 // Hackery to account for the closing }.
570 let mod_lo = self.codemap.span_after(source!(self, s), "{");
572 self.snippet(codemap::mk_sp(mod_lo, source!(self, m.inner).hi - BytePos(1)));
573 let body_snippet = body_snippet.trim();
574 if body_snippet.is_empty() {
575 self.buffer.push_str("}");
577 self.last_pos = mod_lo;
578 self.block_indent = self.block_indent.block_indent(self.config);
579 self.walk_mod_items(m);
580 self.format_missing_with_indent(source!(self, m.inner).hi - BytePos(1));
583 self.last_pos = source!(self, m.inner).hi;
585 self.buffer.push_str(";");
586 self.last_pos = source!(self, s).hi;
590 pub fn format_separate_mod(&mut self, m: &ast::Mod) {
591 let filemap = self.codemap.lookup_char_pos(m.inner.lo).file;
592 self.last_pos = filemap.start_pos;
593 self.block_indent = Indent::empty();
594 self.walk_mod_items(m);
595 self.format_missing_with_indent(filemap.end_pos);
598 pub fn get_context(&self) -> RewriteContext {
600 parse_session: self.parse_session,
601 codemap: self.codemap,
608 impl<'a> Rewrite for [ast::Attribute] {
609 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
610 let mut result = String::new();
614 let indent = shape.indent.to_string(context.config);
616 for (i, a) in self.iter().enumerate() {
617 let mut a_str = context.snippet(a.span);
619 // Write comments and blank lines between attributes.
621 let comment = context.snippet(codemap::mk_sp(self[i - 1].span.hi, a.span.lo));
622 // This particular horror show is to preserve line breaks in between doc
623 // comments. An alternative would be to force such line breaks to start
624 // with the usual doc comment token.
625 let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1;
626 let comment = comment.trim();
627 if !comment.is_empty() {
629 try_opt!(rewrite_comment(comment,
631 Shape::legacy(context.config.comment_width() -
632 shape.indent.width(),
635 result.push_str(&indent);
636 result.push_str(&comment);
638 } else if multi_line {
641 result.push_str(&indent);
644 if a_str.starts_with("//") {
645 a_str = try_opt!(rewrite_comment(&a_str,
647 Shape::legacy(context.config.comment_width() -
648 shape.indent.width(),
653 // Write the attribute itself.
654 result.push_str(&a_str);
656 if i < self.len() - 1 {