]> git.lizzy.rs Git - rust.git/commitdiff
Use block indent style against tuple type
authortopecongiro <seuchida@gmail.com>
Wed, 14 Jun 2017 00:29:39 +0000 (09:29 +0900)
committertopecongiro <seuchida@gmail.com>
Wed, 14 Jun 2017 00:29:39 +0000 (09:29 +0900)
src/expr.rs
src/types.rs

index 1e92b5b9368e5b26956635e26b90b1fd2d9fca1b..10e25bbc8c50e6af321ecf2739d08fde547ff372 100644 (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 @@ pub fn rewrite_tuple_type<'a, I>(
     }
 }
 
-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
+    }
+}
index 5e57ce790a75f2f69778bd7b24770ae71ec67cf3..f084aecaee64f19d174b1a9f6507c0d2bd53ba82 100644 (file)
@@ -23,7 +23,7 @@
 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 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                         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)
             }