diff --git a/src/items.rs b/src/items.rs index 9de91307b27..9ba2985c2a0 100644 --- a/src/items.rs +++ b/src/items.rs @@ -739,6 +739,61 @@ fn format_tuple_struct(context: &RewriteContext, Some(result) } +pub fn rewrite_type_alias(context: &RewriteContext, + indent: Indent, + ident: ast::Ident, + ty: &ast::Ty, + generics: &ast::Generics, + vis: ast::Visibility, + span: Span) + -> Option { + let mut result = String::new(); + + result.push_str(&format_visibility(vis)); + result.push_str("type "); + result.push_str(&ident.to_string()); + + let generics_indent = indent + result.len(); + let generics_span = mk_sp(span_after(span, "type", context.codemap), ty.span.lo); + let generics_str = try_opt!(rewrite_generics(context, + generics, + indent, + generics_indent, + generics_span)); + + result.push_str(&generics_str); + result.push_str(" = "); + + let last_line_length = match generics_str.rfind("\n") { + Some(index) => " = ".len() + generics_str.len() - index, + None => result.len(), + }; + + let budget = try_opt!(context.config + .max_width + .checked_sub(indent.width() + last_line_length + ";".len())); + let type_indent = indent + last_line_length; + // Try to fit the type on the same line + let ty_str = try_opt!(ty.rewrite(context, budget, type_indent) + .or_else(|| { + // The line was to short try and put the type on the next line + + // Remove the space after '=' + result.pop(); + let type_indent = indent.block_indent(context.config); + result.push('\n'); + result.push_str(&type_indent.to_string(context.config)); + let budget = try_opt!(context.config + .max_width + .checked_sub(type_indent.width() + + ";".len())); + ty.rewrite(context, budget, type_indent) + })); + result.push_str(&ty_str); + result.push_str(";"); + Some(result) +} + impl Rewrite for ast::StructField { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option { if contains_skip(&self.node.attrs) { diff --git a/src/visitor.rs b/src/visitor.rs index 98a706adc41..7b694de4535 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -21,7 +21,7 @@ use config::Config; use rewrite::{Rewrite, RewriteContext}; use comment::rewrite_comment; use macros::rewrite_macro; -use items::{rewrite_static, format_impl}; +use items::{rewrite_static, rewrite_type_alias, format_impl}; pub struct FmtVisitor<'a> { pub parse_session: &'a ParseSess, @@ -299,8 +299,15 @@ impl<'a> FmtVisitor<'a> { item.span, item.id) } - ast::Item_::ItemTy(..) => { - // FIXME(#486): format type aliases. + ast::Item_::ItemTy(ref ty, ref generics) => { + let rewrite = rewrite_type_alias(&self.get_context(), + self.block_indent, + item.ident, + ty, + generics, + item.vis, + item.span); + self.push_rewrite(item.span, rewrite); } } } diff --git a/tests/source/type_alias.rs b/tests/source/type_alias.rs new file mode 100644 index 00000000000..3f8a6ccfe3e --- /dev/null +++ b/tests/source/type_alias.rs @@ -0,0 +1,16 @@ + +type PrivateTest<'a, I> = (Box + 'a>, Box + 'a>); + +pub type PublicTest<'a, I, O> = Result, Box + 'a>, Box + 'a>>; + +pub type LongGenericListTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B, C> = Option>; + +pub type Exactly100CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec; + +pub type Exactly101CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec; + +pub type CommentTest< /* Lifetime */ 'a + , + // Type + T + > = (); diff --git a/tests/target/type_alias.rs b/tests/target/type_alias.rs new file mode 100644 index 00000000000..a468f7920e3 --- /dev/null +++ b/tests/target/type_alias.rs @@ -0,0 +1,27 @@ + +type PrivateTest<'a, I> = (Box + 'a>, + Box + 'a>); + +pub type PublicTest<'a, I, O> = Result, + Box + 'a>, + Box + 'a>>; + +pub type LongGenericListTest<'a, + 'b, + 'c, + 'd, + LONGPARAMETERNAME, + LONGPARAMETERNAME, + LONGPARAMETERNAME, + A, + B, + C> = Option>; + +pub type Exactly100CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec; + +pub type Exactly101CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = + Vec; + +pub type CommentTest = ();