Merge pull request #2284 from topecongiro/trait-aliases

Format trait aliases
This commit is contained in:
Nick Cameron 2017-12-15 18:11:28 +13:00 committed by GitHub
commit 8db33ff0f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 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> { fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option<String> {
let header_str = format_header(p.prefix, p.ident, p.vis); let header_str = format_header(p.prefix, p.ident, p.vis);
let generics_str = if let Some(generics) = p.generics { 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}; CodeCharKind, CommentCodeSlices, FindUncommented};
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, format_trait_alias, rewrite_associated_impl_type,
rewrite_type_alias, FnSig, StaticParts, StructParts}; rewrite_associated_type, 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};
@ -373,9 +373,16 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
let rw = format_trait(&self.get_context(), item, self.block_indent); let rw = format_trait(&self.get_context(), item, self.block_indent);
self.push_rewrite(item.span, rw); self.push_rewrite(item.span, rw);
} }
ast::ItemKind::TraitAlias(..) => { ast::ItemKind::TraitAlias(ref generics, ref ty_param_bounds) => {
// FIXME: #2283. let shape = Shape::indented(self.block_indent, self.config);
self.push_rewrite(item.span, None); let rw = format_trait_alias(
&self.get_context(),
item.ident,
generics,
ty_param_bounds,
shape,
);
self.push_rewrite(item.span, rw);
} }
ast::ItemKind::ExternCrate(_) => { ast::ItemKind::ExternCrate(_) => {
let rw = rewrite_extern_crate(&self.get_context(), item); let rw = rewrite_extern_crate(&self.get_context(), item);

View File

@ -71,3 +71,19 @@ trait Foo {
type ItRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev; type ItRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
type IteRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev; type IteRev = <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
} }
// Trait aliases
trait FooBar =
Foo
+ Bar;
trait FooBar <A, B, C>=
Foo
+ Bar;
trait AAAAAAAAAAAAAAAAAA = BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDD;
trait AAAAAAAAAAAAAAAAAAA = BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDD;
trait AAAAAAAAAAAAAAAAAA = BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDDD;
trait AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<A, B, C, D, E> = FooBar;
trait AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<A, B, C, D, E> = FooBar;
#[rustfmt_skip]
trait FooBar = Foo
+ Bar;

View File

@ -99,3 +99,24 @@ trait Foo {
type IteRev = type IteRev =
<MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev; <MergingUntypedTimeSeries<SliceSeries<SliceWindow>> as UntypedTimeSeries>::IterRev;
} }
// Trait aliases
trait FooBar = Foo + Bar;
trait FooBar<A, B, C> = Foo + Bar;
trait AAAAAAAAAAAAAAAAAA = BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDD;
trait AAAAAAAAAAAAAAAAAAA =
BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDD;
trait AAAAAAAAAAAAAAAAAA =
BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDDD;
trait AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<A, B, C, D, E> =
FooBar;
trait AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA<
A,
B,
C,
D,
E,
> = FooBar;
#[rustfmt_skip]
trait FooBar = Foo
+ Bar;