parent
68bc387925
commit
c61d6d4c3a
55
src/items.rs
55
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<String> {
|
||||
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<String> {
|
||||
if contains_skip(&self.node.attrs) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
16
tests/source/type_alias.rs
Normal file
16
tests/source/type_alias.rs
Normal file
@ -0,0 +1,16 @@
|
||||
|
||||
type PrivateTest<'a, I> = (Box<Parser<Input=I, Output=char> + 'a>, Box<Parser<Input=I, Output=char> + 'a>);
|
||||
|
||||
pub type PublicTest<'a, I, O> = Result<Vec<MyLongType>, Box<Parser<Input=I, Output=char> + 'a>, Box<Parser<Input=I, Output=char> + 'a>>;
|
||||
|
||||
pub type LongGenericListTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B, C> = Option<Vec<MyType>>;
|
||||
|
||||
pub type Exactly100CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec<i32>;
|
||||
|
||||
pub type Exactly101CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec<Test>;
|
||||
|
||||
pub type CommentTest< /* Lifetime */ 'a
|
||||
,
|
||||
// Type
|
||||
T
|
||||
> = ();
|
27
tests/target/type_alias.rs
Normal file
27
tests/target/type_alias.rs
Normal file
@ -0,0 +1,27 @@
|
||||
|
||||
type PrivateTest<'a, I> = (Box<Parser<Input = I, Output = char> + 'a>,
|
||||
Box<Parser<Input = I, Output = char> + 'a>);
|
||||
|
||||
pub type PublicTest<'a, I, O> = Result<Vec<MyLongType>,
|
||||
Box<Parser<Input = I, Output = char> + 'a>,
|
||||
Box<Parser<Input = I, Output = char> + 'a>>;
|
||||
|
||||
pub type LongGenericListTest<'a,
|
||||
'b,
|
||||
'c,
|
||||
'd,
|
||||
LONGPARAMETERNAME,
|
||||
LONGPARAMETERNAME,
|
||||
LONGPARAMETERNAME,
|
||||
A,
|
||||
B,
|
||||
C> = Option<Vec<MyType>>;
|
||||
|
||||
pub type Exactly100CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> = Vec<i32>;
|
||||
|
||||
pub type Exactly101CharsTest<'a, 'b, 'c, 'd, LONGPARAMETERNAME, LONGPARAMETERNAME, A, B> =
|
||||
Vec<Test>;
|
||||
|
||||
pub type CommentTest<// Lifetime
|
||||
'a, // Type
|
||||
T> = ();
|
Loading…
x
Reference in New Issue
Block a user