Merge pull request #1666 from topecongiro/tuple-type

Block indent tuple type when fn_call_style is Block
This commit is contained in:
Nick Cameron 2017-06-15 10:01:00 +12:00 committed by GitHub
commit 2dde4547fc
7 changed files with 113 additions and 70 deletions

View file

@ -9,7 +9,6 @@
// except according to those terms.
use std::cmp::{Ordering, min};
use std::ops::Deref;
use std::iter::ExactSizeIterator;
use std::fmt::Write;
@ -1833,14 +1832,17 @@ pub fn rewrite_call(
rewrite_call_inner(context, &callee, args, span, shape, false).ok()
}
fn rewrite_call_inner(
fn rewrite_call_inner<'a, T>(
context: &RewriteContext,
callee_str: &str,
args: &[ptr::P<ast::Expr>],
args: &[ptr::P<T>],
span: Span,
shape: Shape,
force_trailing_comma: bool,
) -> Result<String, Ordering> {
) -> Result<String, Ordering>
where
T: Rewrite + Spanned + ToExpr + 'a,
{
// 2 = `( `, 1 = `(`
let paren_overhead = if context.config.spaces_within_parens() {
2
@ -1924,22 +1926,25 @@ fn need_block_indent(s: &str, shape: Shape) -> bool {
})
}
fn rewrite_call_args(
fn rewrite_call_args<'a, T>(
context: &RewriteContext,
args: &[ptr::P<ast::Expr>],
args: &[ptr::P<T>],
span: Span,
shape: Shape,
one_line_width: usize,
force_trailing_comma: bool,
) -> Option<(bool, String)> {
) -> Option<(bool, String)>
where
T: Rewrite + Spanned + ToExpr + 'a,
{
let mut item_context = context.clone();
item_context.inside_macro = false;
let items = itemize_list(
context.codemap,
args.iter(),
")",
|item| item.span.lo,
|item| item.span.hi,
|item| item.span().lo,
|item| item.span().hi,
|item| item.rewrite(&item_context, shape),
span.lo,
span.hi,
@ -1949,7 +1954,14 @@ fn rewrite_call_args(
// Try letting the last argument overflow to the next line with block
// indentation. If its first line fits on one line with the other arguments,
// we format the function arguments horizontally.
let tactic = try_overflow_last_arg(&item_context, &mut item_vec, args, shape, one_line_width);
let args = args.iter().filter_map(|e| e.to_expr()).collect::<Vec<_>>();
let tactic = try_overflow_last_arg(
&item_context,
&mut item_vec,
&args[..],
shape,
one_line_width,
);
let fmt = ListFormatting {
tactic: tactic,
@ -1974,7 +1986,7 @@ fn rewrite_call_args(
fn try_overflow_last_arg(
context: &RewriteContext,
item_vec: &mut Vec<ListItem>,
args: &[ptr::P<ast::Expr>],
args: &[&ast::Expr],
shape: Shape,
one_line_width: usize,
) -> DefinitiveListTactic {
@ -1991,7 +2003,7 @@ fn try_overflow_last_arg(
last_arg_shape(&context, &item_vec, shape).map_or((None, None), |arg_shape| {
rewrite_last_arg_with_overflow(
&context,
&args[args.len() - 1],
args[args.len() - 1],
&mut item_vec[args.len() - 1],
arg_shape,
)
@ -2040,7 +2052,7 @@ fn last_arg_shape(context: &RewriteContext, items: &Vec<ListItem>, shape: Shape)
fn rewrite_last_arg_with_overflow(
context: &RewriteContext,
last_arg: &ptr::P<ast::Expr>,
last_arg: &ast::Expr,
last_item: &mut ListItem,
shape: Shape,
) -> (Option<String>, Option<String>) {
@ -2056,7 +2068,7 @@ fn rewrite_last_arg_with_overflow(
}
}
fn can_be_overflowed(context: &RewriteContext, args: &[ptr::P<ast::Expr>]) -> bool {
fn can_be_overflowed(context: &RewriteContext, args: &[&ast::Expr]) -> bool {
args.last().map_or(false, |x| {
can_be_overflowed_expr(context, &x, args.len())
})
@ -2344,19 +2356,18 @@ fn shape_from_fn_call_style(
}
}
pub fn rewrite_tuple_type<'a, I>(
fn rewrite_tuple_in_visual_indent_style<'a, T>(
context: &RewriteContext,
mut items: I,
items: &[ptr::P<T>],
span: Span,
shape: Shape,
) -> Option<String>
where
I: ExactSizeIterator,
<I as Iterator>::Item: Deref,
<I::Item as Deref>::Target: Rewrite + Spanned + 'a,
T: Rewrite + Spanned + ToExpr + 'a,
{
let mut items = items.iter();
// In case of length 1, need a trailing comma
debug!("rewrite_tuple_type {:?}", shape);
debug!("rewrite_tuple_in_visual_indent_style {:?}", shape);
if items.len() == 1 {
// 3 = "(" + ",)"
let nested_shape = try_opt!(shape.sub_width(3)).visual_indent(1);
@ -2392,28 +2403,29 @@ where
}
}
pub fn rewrite_tuple(
pub fn rewrite_tuple<'a, T>(
context: &RewriteContext,
items: &[ptr::P<ast::Expr>],
items: &[ptr::P<T>],
span: Span,
shape: Shape,
) -> Option<String> {
) -> Option<String>
where
T: Rewrite + Spanned + ToExpr + 'a,
{
debug!("rewrite_tuple {:?}", shape);
// Use old `rewrite_tuple`
if context.config.fn_call_style() == IndentStyle::Visual {
return rewrite_tuple_type(context, items.iter().map(|x| &**x), span, shape);
if context.use_block_indent() {
// We use the same rule as funcation call for rewriting tuple.
rewrite_call_inner(
context,
&String::new(),
items,
span,
shape,
items.len() == 1,
).ok()
} else {
rewrite_tuple_in_visual_indent_style(context, items, span, shape)
}
// We use the same rule as funcation call for rewriting tuple.
// 1 = ","
rewrite_call_inner(
context,
&String::new(),
items,
span,
shape,
items.len() == 1,
).ok()
}
pub fn rewrite_unary_prefix<R: Rewrite>(
@ -2574,3 +2586,19 @@ fn rewrite_expr_addrof(
};
rewrite_unary_prefix(context, operator_str, expr, shape)
}
pub trait ToExpr {
fn to_expr(&self) -> Option<&ast::Expr>;
}
impl ToExpr for ast::Expr {
fn to_expr(&self) -> Option<&ast::Expr> {
Some(self)
}
}
impl ToExpr for ast::Ty {
fn to_expr(&self) -> Option<&ast::Expr> {
None
}
}

View file

@ -23,7 +23,7 @@ use items::{format_generics_item_list, generics_shape_from_config};
use lists::{itemize_list, format_fn_args};
use rewrite::{Rewrite, RewriteContext};
use utils::{extra_offset, format_mutability, colon_spaces, wrap_str, mk_sp, last_line_width};
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple_type};
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
use config::{Style, TypeDensity};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
@ -689,9 +689,7 @@ impl Rewrite for ast::Ty {
format!("[{}]", ty_str)
})
}
ast::TyKind::Tup(ref items) => {
rewrite_tuple_type(context, items.iter().map(|x| &**x), self.span, shape)
}
ast::TyKind::Tup(ref items) => rewrite_tuple(context, items, self.span, shape),
ast::TyKind::Path(ref q_self, ref path) => {
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
}

View file

@ -36,10 +36,12 @@ extern "C" {
// #1652
fn deconstruct(
foo: Bar,
) -> (SocketAddr,
Header,
Method,
RequestUri,
HttpVersion,
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) {
) -> (
SocketAddr,
Header,
Method,
RequestUri,
HttpVersion,
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
) {
}

View file

@ -87,7 +87,12 @@ where
}
fn foo()
-> (Loooooooooooooooooooooong, Reeeeeeeeeeeeeeeeeeeeeeeeturn, iiiiiiiiis, Looooooooooooooooong)
-> (
Loooooooooooooooooooooong,
Reeeeeeeeeeeeeeeeeeeeeeeeturn,
iiiiiiiiis,
Looooooooooooooooong,
)
{
foo();
}
@ -127,10 +132,12 @@ fn foo<L: Loooooooooooooooooooong, G: Geeeeeeeeeeneric, I: iiiiiiiiis, L: Looooo
}
fn foo()
-> (Looooooooooooooooooooooooooong,
Reeeeeeeeeeeeeeeeeeeeeeeeeeeeeturn,
iiiiiiiiiiiiiis,
Loooooooooooooooooooooong)
-> (
Looooooooooooooooooooooooooong,
Reeeeeeeeeeeeeeeeeeeeeeeeeeeeeturn,
iiiiiiiiiiiiiis,
Loooooooooooooooooooooong,
)
{
foo();
}

View file

@ -152,23 +152,27 @@ fn main() {
}
fn deconstruct()
-> (SocketAddr,
Method,
Headers,
RequestUri,
HttpVersion,
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA)
-> (
SocketAddr,
Method,
Headers,
RequestUri,
HttpVersion,
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
)
{
}
fn deconstruct(
foo: Bar,
) -> (SocketAddr,
Method,
Headers,
RequestUri,
HttpVersion,
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA) {
) -> (
SocketAddr,
Method,
Headers,
RequestUri,
HttpVersion,
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
) {
}
#[rustfmt_skip]

View file

@ -2,10 +2,12 @@
fn types() {
let x: [Vec<_>] = [];
let y: *mut [SomeType; konst_funk()] = expr();
let z: (// #digits
usize,
// exp
i16) = funk();
let z: (
// #digits
usize,
// exp
i16,
) = funk();
let z: (usize /* #digits */, i16 /* exp */) = funk();
}

View file

@ -1,7 +1,9 @@
// rustfmt-normalize_comments: true
type PrivateTest<'a, I> = (Box<Parser<Input = I, Output = char> + 'a>,
Box<Parser<Input = I, Output = char> + 'a>);
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>,