]> git.lizzy.rs Git - rust.git/commitdiff
Factor out rewrite_extern_crate()
authortopecongiro <seuchida@gmail.com>
Wed, 6 Sep 2017 09:44:33 +0000 (18:44 +0900)
committertopecongiro <seuchida@gmail.com>
Wed, 6 Sep 2017 09:44:33 +0000 (18:44 +0900)
src/visitor.rs

index 1fe8655309f3804df8e7e158c29aa72023b4c751..2de2f6865df1625b3537507325d3bdf740328749 100644 (file)
@@ -367,17 +367,8 @@ pub fn visit_item(&mut self, item: &ast::Item) {
                 }
             }
             ast::ItemKind::ExternCrate(_) => {
-                self.format_missing_with_indent(source!(self, item.span).lo());
-                let new_str = self.snippet(item.span);
-                if contains_comment(&new_str) {
-                    self.buffer.push_str(&new_str)
-                } else {
-                    let no_whitespace =
-                        &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
-                    self.buffer
-                        .push_str(&Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"));
-                }
-                self.last_pos = source!(self, item.span).hi();
+                let rw = rewrite_extern_crate(&self.get_context(), item);
+                self.push_rewrite(item.span, rw);
             }
             ast::ItemKind::Struct(ref def, ref generics) => {
                 let rewrite = {
@@ -1046,3 +1037,15 @@ fn get_derive_args(context: &RewriteContext, attr: &ast::Attribute) -> Option<Ve
         _ => None,
     })
 }
+
+// Rewrite `extern crate foo;` WITHOUT attributes.
+pub fn rewrite_extern_crate(context: &RewriteContext, item: &ast::Item) -> Option<String> {
+    assert!(is_extern_crate(item));
+    let new_str = context.snippet(item.span);
+    Some(if contains_comment(&new_str) {
+        new_str
+    } else {
+        let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
+        String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
+    })
+}