]> git.lizzy.rs Git - rust.git/commitdiff
Rewrite module declarations
authorMarcus Klaas <mail@marcusklaas.nl>
Fri, 20 Nov 2015 21:44:15 +0000 (22:44 +0100)
committerMarcus Klaas <mail@marcusklaas.nl>
Sat, 21 Nov 2015 20:06:29 +0000 (21:06 +0100)
src/visitor.rs
tests/source/mod-1.rs
tests/source/mod-2.rs
tests/target/mod-1.rs
tests/target/mod-2.rs

index fa51d70a853fa52b310a5b385fe1e3ffb312c62a..bf6e99e17c336374a611594dce1d2e10b696120b 100644 (file)
@@ -101,10 +101,15 @@ pub fn visit_block(&mut self, b: &ast::Block) {
 
         // 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 @@ pub fn visit_block(&mut self, b: &ast::Block) {
         };
         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 @@ fn visit_item(&mut self, item: &ast::Item) {
             }
             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 @@ fn walk_mod_items(&mut self, m: &ast::Mod) {
         }
     }
 
-    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;
         }
     }
 
index 5eb73cb5d4ca5087fbce555ede908b2e6a35f0a1..427a355b6bee0920517137ee772e2a1ee289aedd 100644 (file)
@@ -16,7 +16,7 @@ mod qux {
 
 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 @@ pub fn freopen(filename: *const c_char,
                    file: *mut FILE)
                    -> *mut FILE{}
 }
+
+  mod    y    { // sup boooooiiii
+   }
index c4a148a616ec98c74fd5170c9851a5a71f48ab91..00b70e972cd45390aa1fc1c2571a57d7af27ec44 100644 (file)
@@ -1,4 +1,4 @@
 // Some nested mods
 
-mod nestedmod;
-mod no_new_line_beginning;
+ mod   nestedmod  ;
+pub  mod no_new_line_beginning;
index 02ce208ed4ea6c987f0d63383ecbc68b3b3a81c9..6fe2e65c565d2319b9671ba93ec1cab2aefb4de7 100644 (file)
@@ -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 @@ pub fn freopen(filename: *const c_char,
                    -> *mut FILE {
     }
 }
+
+mod y {
+    // sup boooooiiii
+}
index c4a148a616ec98c74fd5170c9851a5a71f48ab91..426c266cecf311c76c1157aec5b526b7699dd48a 100644 (file)
@@ -1,4 +1,4 @@
 // Some nested mods
 
 mod nestedmod;
-mod no_new_line_beginning;
+pub mod no_new_line_beginning;