Format trait aliases

This commit is contained in:
topecongiro 2017-12-15 13:47:52 +09:00
parent 4f7cbc3aad
commit 3a98b5a5be
2 changed files with 28 additions and 5 deletions

View file

@ -1087,6 +1087,22 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
}
}
pub fn format_trait_alias(
context: &RewriteContext,
ident: ast::Ident,
generics: &ast::Generics,
ty_param_bounds: &ast::TyParamBounds,
shape: Shape,
) -> Option<String> {
let alias = ident.name.as_str();
// 6 = "trait ", 2 = " ="
let g_shape = shape.offset_left(6 + alias.len())?.sub_width(2)?;
let generics_str = rewrite_generics(context, generics, g_shape, generics.span)?;
let lhs = format!("trait {}{} =", alias, generics_str);
// 1 = ";"
rewrite_assign_rhs(context, lhs, ty_param_bounds, shape.sub_width(1)?).map(|s| s + ";")
}
fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option<String> {
let header_str = format_header(p.prefix, p.ident, p.vis);
let generics_str = if let Some(generics) = p.generics {

View file

@ -22,8 +22,8 @@ use comment::{combine_strs_with_missing_comments, contains_comment, remove_trail
CodeCharKind, CommentCodeSlices, FindUncommented};
use comment::rewrite_comment;
use config::{BraceStyle, Config};
use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_associated_type,
rewrite_type_alias, FnSig, StaticParts, StructParts};
use items::{format_impl, format_trait, format_trait_alias, rewrite_associated_impl_type,
rewrite_associated_type, rewrite_type_alias, FnSig, StaticParts, StructParts};
use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorPlace,
SeparatorTactic};
use macros::{rewrite_macro, MacroPosition};
@ -373,9 +373,16 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let rw = format_trait(&self.get_context(), item, self.block_indent);
self.push_rewrite(item.span, rw);
}
ast::ItemKind::TraitAlias(..) => {
// FIXME: #2283.
self.push_rewrite(item.span, None);
ast::ItemKind::TraitAlias(ref generics, ref ty_param_bounds) => {
let shape = Shape::indented(self.block_indent, self.config);
let rw = format_trait_alias(
&self.get_context(),
item.ident,
generics,
ty_param_bounds,
shape,
);
self.push_rewrite(item.span, rw);
}
ast::ItemKind::ExternCrate(_) => {
let rw = rewrite_extern_crate(&self.get_context(), item);