Add visit_static()
This commit is contained in:
parent
3b2b7f67ee
commit
082c001843
74
src/items.rs
74
src/items.rs
@ -411,6 +411,11 @@ fn single_line_fn(&self, fn_str: &str, block: &ast::Block) -> Option<String> {
|
||||
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) {
|
||||
let is_tuple = struct_parts.def.is_tuple();
|
||||
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> {
|
||||
prefix: &'a str,
|
||||
vis: &'a ast::Visibility,
|
||||
ident: ast::Ident,
|
||||
ty: &'a ast::Ty,
|
||||
mutability: ast::Mutability,
|
||||
expr_opt: Option<&'a ptr::P<ast::Expr>>,
|
||||
span: Span,
|
||||
}
|
||||
|
||||
impl<'a> StaticParts<'a> {
|
||||
pub fn new(
|
||||
vis: &'a ast::Visibility,
|
||||
ident: ast::Ident,
|
||||
ty: &'a ast::Ty,
|
||||
mutability: ast::Mutability,
|
||||
expr_opt: Option<&'a ptr::P<ast::Expr>>,
|
||||
) -> StaticParts<'a> {
|
||||
pub fn from_item(item: &'a ast::Item) -> Self {
|
||||
let (prefix, ty, mutability, expr) = match item.node {
|
||||
ast::ItemKind::Static(ref ty, mutability, ref expr) => ("static", ty, mutability, expr),
|
||||
ast::ItemKind::Const(ref ty, ref expr) => {
|
||||
("const", ty, ast::Mutability::Immutable, expr)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
StaticParts {
|
||||
vis,
|
||||
ident,
|
||||
ty,
|
||||
mutability,
|
||||
expr_opt,
|
||||
prefix: prefix,
|
||||
vis: &item.vis,
|
||||
ident: item.ident,
|
||||
ty: ty,
|
||||
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(
|
||||
prefix: &str,
|
||||
fn rewrite_static(
|
||||
context: &RewriteContext,
|
||||
static_parts: &StaticParts,
|
||||
offset: Indent,
|
||||
span: Span,
|
||||
context: &RewriteContext,
|
||||
) -> Option<String> {
|
||||
let StaticParts {
|
||||
prefix,
|
||||
vis,
|
||||
ident,
|
||||
ty,
|
||||
mutability,
|
||||
expr_opt,
|
||||
span,
|
||||
} = *static_parts;
|
||||
let colon = colon_spaces(
|
||||
context.config.space_before_type_annotation(),
|
||||
|
@ -24,7 +24,7 @@
|
||||
use comment::rewrite_comment;
|
||||
use config::{BraceStyle, Config};
|
||||
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,
|
||||
SeparatorTactic};
|
||||
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_foreign_mod(foreign_mod, item.span);
|
||||
}
|
||||
ast::ItemKind::Static(ref ty, mutability, ref expr) => {
|
||||
let rewrite = rewrite_static(
|
||||
"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::Static(..) | ast::ItemKind::Const(..) => {
|
||||
self.visit_static(&StaticParts::from_item(item));
|
||||
}
|
||||
ast::ItemKind::AutoImpl(..) => {
|
||||
// FIXME(#78): format impl definitions.
|
||||
@ -435,22 +412,7 @@ pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
|
||||
}
|
||||
|
||||
match ti.node {
|
||||
ast::TraitItemKind::Const(ref ty, ref expr_opt) => {
|
||||
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::Const(..) => self.visit_static(&StaticParts::from_trait_item(ti)),
|
||||
ast::TraitItemKind::Method(ref sig, None) => {
|
||||
let indent = self.block_indent;
|
||||
let rewrite =
|
||||
@ -502,22 +464,7 @@ pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
|
||||
Some(&ii.attrs),
|
||||
);
|
||||
}
|
||||
ast::ImplItemKind::Const(ref ty, ref expr) => {
|
||||
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::Const(..) => self.visit_static(&StaticParts::from_impl_item(ii)),
|
||||
ast::ImplItemKind::Type(ref ty) => {
|
||||
let rewrite = rewrite_associated_impl_type(
|
||||
ii.ident,
|
||||
|
Loading…
Reference in New Issue
Block a user