diff --git a/src/visitor.rs b/src/visitor.rs index fa51d70a853..bf6e99e17c3 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -101,10 +101,15 @@ impl<'a> FmtVisitor<'a> { // TODO: we should compress any newlines here to just one self.format_missing_with_indent(b.span.hi - brace_compensation); - // FIXME: this is a terrible hack to indent the comments between the last - // item in the block and the closing brace to the block's level. - // The closing brace itself, however, should be indented at a shallower - // level. + self.close_block(); + self.last_pos = b.span.hi; + } + + // FIXME: this is a terrible hack to indent the comments between the last + // item in the block and the closing brace to the block's level. + // The closing brace itself, however, should be indented at a shallower + // level. + fn close_block(&mut self) { let total_len = self.buffer.len; let chars_too_many = if self.config.hard_tabs { 1 @@ -113,7 +118,6 @@ impl<'a> FmtVisitor<'a> { }; self.buffer.truncate(total_len - chars_too_many); self.buffer.push_str("}"); - self.last_pos = b.span.hi; self.block_indent = self.block_indent.block_unindent(self.config); } @@ -239,7 +243,7 @@ impl<'a> FmtVisitor<'a> { } ast::Item_::ItemMod(ref module) => { self.format_missing_with_indent(item.span.lo); - self.format_mod(module, item.span, item.ident); + self.format_mod(module, item.vis, item.span, item.ident); } ast::Item_::ItemMac(..) => { self.format_missing_with_indent(item.span.lo); @@ -433,23 +437,26 @@ impl<'a> FmtVisitor<'a> { } } - fn format_mod(&mut self, m: &ast::Mod, s: Span, ident: ast::Ident) { - debug!("FmtVisitor::format_mod: ident: {:?}, span: {:?}", ident, s); - + fn format_mod(&mut self, m: &ast::Mod, vis: ast::Visibility, s: Span, ident: ast::Ident) { // Decide whether this is an inline mod or an external mod. let local_file_name = self.codemap.span_to_filename(s); let is_internal = local_file_name == self.codemap.span_to_filename(m.inner); - // TODO: Should rewrite properly `mod X;` + self.buffer.push_str(utils::format_visibility(vis)); + self.buffer.push_str("mod "); + self.buffer.push_str(&ident.to_string()); if is_internal { + self.buffer.push_str(" {"); + self.last_pos = ::utils::span_after(s, "{", self.codemap); self.block_indent = self.block_indent.block_indent(self.config); self.walk_mod_items(m); - self.block_indent = self.block_indent.block_unindent(self.config); - self.format_missing_with_indent(m.inner.hi - BytePos(1)); - self.buffer.push_str("}"); + self.close_block(); self.last_pos = m.inner.hi; + } else { + self.buffer.push_str(";"); + self.last_pos = s.hi; } } diff --git a/tests/source/mod-1.rs b/tests/source/mod-1.rs index 5eb73cb5d4c..427a355b6be 100644 --- a/tests/source/mod-1.rs +++ b/tests/source/mod-1.rs @@ -16,7 +16,7 @@ mod foo { mod boxed { pub use std::boxed::{Box, HEAP}; } -mod x { +pub mod x { pub fn freopen(filename: *const c_char, mode: *const c_char, mode2: *const c_char, @@ -24,3 +24,6 @@ mod x { file: *mut FILE) -> *mut FILE{} } + + mod y { // sup boooooiiii + } diff --git a/tests/source/mod-2.rs b/tests/source/mod-2.rs index c4a148a616e..00b70e972cd 100644 --- a/tests/source/mod-2.rs +++ b/tests/source/mod-2.rs @@ -1,4 +1,4 @@ // Some nested mods -mod nestedmod; -mod no_new_line_beginning; + mod nestedmod ; +pub mod no_new_line_beginning; diff --git a/tests/target/mod-1.rs b/tests/target/mod-1.rs index 02ce208ed4e..6fe2e65c565 100644 --- a/tests/target/mod-1.rs +++ b/tests/target/mod-1.rs @@ -25,7 +25,7 @@ mod boxed { pub use std::boxed::{Box, HEAP}; } -mod x { +pub mod x { pub fn freopen(filename: *const c_char, mode: *const c_char, mode2: *const c_char, @@ -34,3 +34,7 @@ mod x { -> *mut FILE { } } + +mod y { + // sup boooooiiii +} diff --git a/tests/target/mod-2.rs b/tests/target/mod-2.rs index c4a148a616e..426c266cecf 100644 --- a/tests/target/mod-2.rs +++ b/tests/target/mod-2.rs @@ -1,4 +1,4 @@ // Some nested mods mod nestedmod; -mod no_new_line_beginning; +pub mod no_new_line_beginning;