Add visit_static()

This commit is contained in:
topecongiro 2017-11-05 21:59:33 +09:00
parent 3b2b7f67ee
commit 082c001843
2 changed files with 63 additions and 74 deletions

View File

@ -411,6 +411,11 @@ fn single_line_fn(&self, fn_str: &str, block: &ast::Block) -> Option<String> {
None None
} }
pub fn visit_static(&mut self, static_parts: &StaticParts) {
let rewrite = rewrite_static(&self.get_context(), static_parts, self.block_indent);
self.push_rewrite(static_parts.span, rewrite);
}
pub fn visit_struct(&mut self, struct_parts: &StructParts) { pub fn visit_struct(&mut self, struct_parts: &StructParts) {
let is_tuple = struct_parts.def.is_tuple(); let is_tuple = struct_parts.def.is_tuple();
let rewrite = format_struct(&self.get_context(), struct_parts, self.block_indent, None) let rewrite = format_struct(&self.get_context(), struct_parts, self.block_indent, None)
@ -1480,44 +1485,81 @@ pub fn rewrite_struct_field(
} }
pub struct StaticParts<'a> { pub struct StaticParts<'a> {
prefix: &'a str,
vis: &'a ast::Visibility, vis: &'a ast::Visibility,
ident: ast::Ident, ident: ast::Ident,
ty: &'a ast::Ty, ty: &'a ast::Ty,
mutability: ast::Mutability, mutability: ast::Mutability,
expr_opt: Option<&'a ptr::P<ast::Expr>>, expr_opt: Option<&'a ptr::P<ast::Expr>>,
span: Span,
} }
impl<'a> StaticParts<'a> { impl<'a> StaticParts<'a> {
pub fn new( pub fn from_item(item: &'a ast::Item) -> Self {
vis: &'a ast::Visibility, let (prefix, ty, mutability, expr) = match item.node {
ident: ast::Ident, ast::ItemKind::Static(ref ty, mutability, ref expr) => ("static", ty, mutability, expr),
ty: &'a ast::Ty, ast::ItemKind::Const(ref ty, ref expr) => {
mutability: ast::Mutability, ("const", ty, ast::Mutability::Immutable, expr)
expr_opt: Option<&'a ptr::P<ast::Expr>>, }
) -> StaticParts<'a> { _ => unreachable!(),
};
StaticParts { StaticParts {
vis, prefix: prefix,
ident, vis: &item.vis,
ty, ident: item.ident,
mutability, ty: ty,
expr_opt, mutability: mutability,
expr_opt: Some(expr),
span: item.span,
}
}
pub fn from_trait_item(ti: &'a ast::TraitItem) -> Self {
let (ty, expr_opt) = match ti.node {
ast::TraitItemKind::Const(ref ty, ref expr_opt) => (ty, expr_opt),
_ => unreachable!(),
};
StaticParts {
prefix: "const",
vis: &ast::Visibility::Inherited,
ident: ti.ident,
ty: ty,
mutability: ast::Mutability::Immutable,
expr_opt: expr_opt.as_ref(),
span: ti.span,
}
}
pub fn from_impl_item(ii: &'a ast::ImplItem) -> Self {
let (ty, expr) = match ii.node {
ast::ImplItemKind::Const(ref ty, ref expr) => (ty, expr),
_ => unreachable!(),
};
StaticParts {
prefix: "const",
vis: &ii.vis,
ident: ii.ident,
ty: ty,
mutability: ast::Mutability::Immutable,
expr_opt: Some(expr),
span: ii.span,
} }
} }
} }
pub fn rewrite_static( fn rewrite_static(
prefix: &str, context: &RewriteContext,
static_parts: &StaticParts, static_parts: &StaticParts,
offset: Indent, offset: Indent,
span: Span,
context: &RewriteContext,
) -> Option<String> { ) -> Option<String> {
let StaticParts { let StaticParts {
prefix,
vis, vis,
ident, ident,
ty, ty,
mutability, mutability,
expr_opt, expr_opt,
span,
} = *static_parts; } = *static_parts;
let colon = colon_spaces( let colon = colon_spaces(
context.config.space_before_type_annotation(), context.config.space_before_type_annotation(),

View File

@ -24,7 +24,7 @@
use comment::rewrite_comment; use comment::rewrite_comment;
use config::{BraceStyle, Config}; use config::{BraceStyle, Config};
use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_associated_type, use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_associated_type,
rewrite_static, rewrite_type_alias, FnSig, StaticParts, StructParts}; rewrite_type_alias, FnSig, StaticParts, StructParts};
use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorPlace, use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorPlace,
SeparatorTactic}; SeparatorTactic};
use macros::{rewrite_macro, MacroPosition}; use macros::{rewrite_macro, MacroPosition};
@ -363,31 +363,8 @@ pub fn visit_item(&mut self, item: &ast::Item) {
self.format_missing_with_indent(source!(self, item.span).lo()); self.format_missing_with_indent(source!(self, item.span).lo());
self.format_foreign_mod(foreign_mod, item.span); self.format_foreign_mod(foreign_mod, item.span);
} }
ast::ItemKind::Static(ref ty, mutability, ref expr) => { ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
let rewrite = rewrite_static( self.visit_static(&StaticParts::from_item(item));
"static",
&StaticParts::new(&item.vis, item.ident, ty, mutability, Some(expr)),
self.block_indent,
item.span,
&self.get_context(),
);
self.push_rewrite(item.span, rewrite);
}
ast::ItemKind::Const(ref ty, ref expr) => {
let rewrite = rewrite_static(
"const",
&StaticParts::new(
&item.vis,
item.ident,
ty,
ast::Mutability::Immutable,
Some(expr),
),
self.block_indent,
item.span,
&self.get_context(),
);
self.push_rewrite(item.span, rewrite);
} }
ast::ItemKind::AutoImpl(..) => { ast::ItemKind::AutoImpl(..) => {
// FIXME(#78): format impl definitions. // FIXME(#78): format impl definitions.
@ -435,22 +412,7 @@ pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
} }
match ti.node { match ti.node {
ast::TraitItemKind::Const(ref ty, ref expr_opt) => { ast::TraitItemKind::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
let rewrite = rewrite_static(
"const",
&StaticParts::new(
&ast::Visibility::Inherited,
ti.ident,
ty,
ast::Mutability::Immutable,
expr_opt.as_ref(),
),
self.block_indent,
ti.span,
&self.get_context(),
);
self.push_rewrite(ti.span, rewrite);
}
ast::TraitItemKind::Method(ref sig, None) => { ast::TraitItemKind::Method(ref sig, None) => {
let indent = self.block_indent; let indent = self.block_indent;
let rewrite = let rewrite =
@ -502,22 +464,7 @@ pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
Some(&ii.attrs), Some(&ii.attrs),
); );
} }
ast::ImplItemKind::Const(ref ty, ref expr) => { ast::ImplItemKind::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
let rewrite = rewrite_static(
"const",
&StaticParts::new(
&ii.vis,
ii.ident,
ty,
ast::Mutability::Immutable,
Some(expr),
),
self.block_indent,
ii.span,
&self.get_context(),
);
self.push_rewrite(ii.span, rewrite);
}
ast::ImplItemKind::Type(ref ty) => { ast::ImplItemKind::Type(ref ty) => {
let rewrite = rewrite_associated_impl_type( let rewrite = rewrite_associated_impl_type(
ii.ident, ii.ident,