Move items-related stuffs to item mod from visitor mod

Move `rewrite_extern_crate`, is_mod_decl`, `is_use_item` and `is_extern_crate`.
This commit is contained in:
topecongiro 2018-02-23 22:37:10 +09:00
parent a8852fd787
commit ecfb9c9526
3 changed files with 41 additions and 40 deletions

View File

@ -14,6 +14,7 @@ use std::borrow::Cow;
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,
}
}

View File

@ -23,13 +23,13 @@ use attr::filter_inline_attrs;
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;

View File

@ -15,39 +15,17 @@ use syntax::parse::ParseSess;
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 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
}
}
}
// 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, ";"))
})
}