]> git.lizzy.rs Git - rust.git/commitdiff
Move items-related stuffs to item mod from visitor mod
authortopecongiro <seuchida@gmail.com>
Fri, 23 Feb 2018 13:37:10 +0000 (22:37 +0900)
committertopecongiro <seuchida@gmail.com>
Fri, 23 Feb 2018 13:37:10 +0000 (22:37 +0900)
Move `rewrite_extern_crate`, is_mod_decl`, `is_use_item` and `is_extern_crate`.

rustfmt-core/src/items.rs
rustfmt-core/src/reorder.rs
rustfmt-core/src/visitor.rs

index add3cdc0d79f6256a6a49c314541208d06c808f2..3503546c0102d881e2900e80c491ec2401016608 100644 (file)
@@ -14,6 +14,7 @@
 use std::cmp::min;
 
 use config::lists::*;
+use regex::Regex;
 use syntax::{abi, ast, ptr, symbol};
 use syntax::ast::{CrateSugar, ImplItem};
 use syntax::codemap::{BytePos, Span};
@@ -2854,3 +2855,37 @@ pub fn rewrite_mod(item: &ast::Item) -> String {
     result.push(';');
     result
 }
+
+/// 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.to_owned()
+    } else {
+        let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
+        String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
+    })
+}
+
+/// Returns true for `mod foo;`, false for `mod foo { .. }`.
+pub fn is_mod_decl(item: &ast::Item) -> bool {
+    match item.node {
+        ast::ItemKind::Mod(ref m) => m.inner.hi() != item.span.hi(),
+        _ => false,
+    }
+}
+
+pub fn is_use_item(item: &ast::Item) -> bool {
+    match item.node {
+        ast::ItemKind::Use(_) => true,
+        _ => false,
+    }
+}
+
+pub fn is_extern_crate(item: &ast::Item) -> bool {
+    match item.node {
+        ast::ItemKind::ExternCrate(..) => true,
+        _ => false,
+    }
+}
index 82bf18f6b5e1e5d63b744d1da18747d631f297af..d4bc36fc83f3fbfb605d9649230c72956f2809c9 100644 (file)
 use codemap::LineRangeUtils;
 use comment::combine_strs_with_missing_comments;
 use imports::{path_to_imported_ident, rewrite_import};
-use items::rewrite_mod;
+use items::{rewrite_extern_crate, rewrite_mod};
 use lists::{itemize_list, write_list, ListFormatting};
 use rewrite::{Rewrite, RewriteContext};
 use shape::Shape;
 use spanned::Spanned;
 use utils::mk_sp;
-use visitor::{rewrite_extern_crate, FmtVisitor};
+use visitor::FmtVisitor;
 
 use std::cmp::Ordering;
 
index 4dd121c719fdc1e3e0a3fc9b5771b99db56dff70..35102f58609063d1afab46d31a249f27e473992a 100644 (file)
 
 use attr::*;
 use codemap::{LineRangeUtils, SpanUtils};
-use comment::{contains_comment, CodeCharKind, CommentCodeSlices, FindUncommented};
+use comment::{CodeCharKind, CommentCodeSlices, FindUncommented};
 use config::{BraceStyle, Config};
-use items::{format_impl, format_trait, format_trait_alias, rewrite_associated_impl_type,
-            rewrite_associated_type, rewrite_type_alias, FnSig, StaticParts, StructParts};
+use items::{format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
+            rewrite_associated_impl_type, rewrite_associated_type, rewrite_extern_crate,
+            rewrite_type_alias, FnSig, StaticParts, StructParts};
 use macros::{rewrite_macro, rewrite_macro_def, MacroPosition};
-use regex::Regex;
 use rewrite::{Rewrite, RewriteContext};
 use shape::{Indent, Shape};
 use spanned::Spanned;
 use utils::{self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec};
 
-/// Returns true for `mod foo;`, false for `mod foo { .. }`.
-fn is_mod_decl(item: &ast::Item) -> bool {
-    match item.node {
-        ast::ItemKind::Mod(ref m) => m.inner.hi() != item.span.hi(),
-        _ => false,
-    }
-}
-
-fn is_use_item(item: &ast::Item) -> bool {
-    match item.node {
-        ast::ItemKind::Use(_) => true,
-        _ => false,
-    }
-}
-
-fn is_extern_crate(item: &ast::Item) -> bool {
-    match item.node {
-        ast::ItemKind::ExternCrate(..) => true,
-        _ => false,
-    }
-}
-
 /// Creates a string slice corresponding to the specified span.
 pub struct SnippetProvider<'a> {
     /// A pointer to the content of the file we are formatting.
@@ -720,15 +698,3 @@ pub fn get_context(&self) -> RewriteContext {
         }
     }
 }
-
-// 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.to_owned()
-    } else {
-        let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
-        String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
-    })
-}