]> git.lizzy.rs Git - rust.git/blobdiff - src/types.rs
Remove BlockIndentStyle::Inherit
[rust.git] / src / types.rs
index cce487cb2ea8fd96fa83de5daf91f10981b862fa..0162ac851f3833b73d5de3ce1e15842edbb88f36 100644 (file)
@@ -66,9 +66,9 @@ pub fn rewrite_path(context: &RewriteContext,
                 result.push_str("::");
             }
 
-            let extra_offset = extra_offset(&result, shape.indent);
+            let extra_offset = extra_offset(&result, shape);
             // 3 = ">::".len()
-            let budget = try_opt!(shape.width.checked_sub(extra_offset + 3));
+            let shape = try_opt!(try_opt!(shape.shrink_left(extra_offset)).sub_width(3));
 
             result = try_opt!(rewrite_path_segments(PathContext::Type,
                                                     result,
@@ -76,8 +76,7 @@ pub fn rewrite_path(context: &RewriteContext,
                                                     span_lo,
                                                     path.span.hi,
                                                     context,
-                                                    Shape::legacy(budget,
-                                                                  shape.indent + extra_offset)));
+                                                    shape));
         }
 
         if context.config.spaces_within_angle_brackets {
@@ -88,15 +87,13 @@ pub fn rewrite_path(context: &RewriteContext,
         span_lo = qself.ty.span.hi + BytePos(1);
     }
 
-    let extra_offset = extra_offset(&result, shape.indent);
-    let budget = try_opt!(shape.width.checked_sub(extra_offset));
     rewrite_path_segments(path_context,
                           result,
                           path.segments.iter().skip(skip_count),
                           span_lo,
                           path.span.hi,
                           context,
-                          Shape::legacy(budget, shape.indent + extra_offset))
+                          shape)
 }
 
 fn rewrite_path_segments<'a, I>(path_context: PathContext,
@@ -110,6 +107,7 @@ fn rewrite_path_segments<'a, I>(path_context: PathContext,
     where I: Iterator<Item = &'a ast::PathSegment>
 {
     let mut first = true;
+    let shape = shape.visual_indent(0);
 
     for segment in iter {
         // Indicates a global path, shouldn't be rendered.
@@ -122,15 +120,14 @@ fn rewrite_path_segments<'a, I>(path_context: PathContext,
             buffer.push_str("::");
         }
 
-        let extra_offset = extra_offset(&buffer, shape.indent);
-        let remaining_width = try_opt!(shape.width.checked_sub(extra_offset));
-        let new_offset = shape.indent + extra_offset;
+        let extra_offset = extra_offset(&buffer, shape);
+        let new_shape = try_opt!(shape.shrink_left(extra_offset));
         let segment_string = try_opt!(rewrite_segment(path_context,
                                                       segment,
                                                       &mut span_lo,
                                                       span_hi,
                                                       context,
-                                                      Shape::legacy(remaining_width, new_offset)));
+                                                      new_shape));
 
         buffer.push_str(&segment_string);
     }
@@ -161,10 +158,15 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
             SegmentParam::Type(ty) => ty.rewrite(context, shape),
             SegmentParam::Binding(binding) => {
-                let mut result = format!("{} = ", binding.ident);
+                let mut result = match context.config.type_punctuation_density {
+                    TypeDensity::Wide => format!("{} = ", binding.ident),
+                    TypeDensity::Compressed => format!("{}=", binding.ident),
+                };
                 let budget = try_opt!(shape.width.checked_sub(result.len()));
-                let rewrite = try_opt!(binding.ty
-                    .rewrite(context, Shape::legacy(budget, shape.indent + result.len())));
+                let rewrite = try_opt!(binding.ty.rewrite(context,
+                                                          Shape::legacy(budget,
+                                                                        shape.indent +
+                                                                        result.len())));
                 result.push_str(&rewrite);
                 Some(result)
             }
@@ -190,8 +192,7 @@ fn rewrite_segment(path_context: PathContext,
                    shape: Shape)
                    -> Option<String> {
     let ident_len = segment.identifier.to_string().len();
-    let width = try_opt!(shape.width.checked_sub(ident_len));
-    let offset = shape.indent + ident_len;
+    let shape = try_opt!(shape.shrink_left(ident_len));
 
     let params = if let Some(ref params) = segment.parameters {
         match **params {
@@ -205,7 +206,10 @@ fn rewrite_segment(path_context: PathContext,
                     .chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x)))
                     .collect::<Vec<_>>();
 
-                let next_span_lo = param_list.last().unwrap().get_span().hi + BytePos(1);
+                let next_span_lo = param_list.last()
+                    .unwrap()
+                    .get_span()
+                    .hi + BytePos(1);
                 let list_lo = context.codemap.span_after(codemap::mk_sp(*span_lo, span_hi), "<");
                 let separator = if path_context == PathContext::Expr {
                     "::"
@@ -216,24 +220,19 @@ fn rewrite_segment(path_context: PathContext,
                 // 1 for <
                 let extra_offset = 1 + separator.len();
                 // 1 for >
-                let list_width = try_opt!(width.checked_sub(extra_offset + 1));
+                // TODO bad visual indent
+                let list_shape = try_opt!(try_opt!(shape.shrink_left(extra_offset)).sub_width(1))
+                    .visual_indent(0);
 
                 let items = itemize_list(context.codemap,
                                          param_list.into_iter(),
                                          ">",
                                          |param| param.get_span().lo,
                                          |param| param.get_span().hi,
-                                         |seg| {
-                                             seg.rewrite(context,
-                                                         Shape::legacy(list_width,
-                                                                       offset + extra_offset))
-                                         },
+                                         |seg| seg.rewrite(context, list_shape),
                                          list_lo,
                                          span_hi);
-                let list_str = try_opt!(format_item_list(items,
-                                                         Shape::legacy(list_width,
-                                                                       offset + extra_offset),
-                                                         context.config));
+                let list_str = try_opt!(format_item_list(items, list_shape, context.config));
 
                 // Update position of last bracket.
                 *span_lo = next_span_lo;
@@ -254,7 +253,7 @@ fn rewrite_segment(path_context: PathContext,
                                               false,
                                               data.span,
                                               context,
-                                              Shape::legacy(width, offset)))
+                                              shape))
             }
             _ => String::new(),
         }
@@ -339,10 +338,10 @@ enum ArgumentKind<T>
     };
 
     Some(if context.config.spaces_within_parens {
-        format!("( {} ){}{}", list_str, infix, output)
-    } else {
-        format!("({}){}{}", list_str, infix, output)
-    })
+             format!("( {} ){}{}", list_str, infix, output)
+         } else {
+             format!("({}){}{}", list_str, infix, output)
+         })
 }
 
 fn type_bound_colon(context: &RewriteContext) -> &'static str {
@@ -419,7 +418,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 let used_width = 3 + lhs_ty_str.len();
                 let budget = try_opt!(shape.width.checked_sub(used_width));
                 let rhs_ty_str = try_opt!(rhs_ty.rewrite(context,
-                                            Shape::legacy(budget, shape.indent + used_width)));
+                                                         Shape::legacy(budget,
+                                                                       shape.indent + used_width)));
                 format!("{} = {}", lhs_ty_str, rhs_ty_str)
             }
         };
@@ -446,9 +446,8 @@ fn rewrite_bounded_lifetime<'b, I>(lt: &ast::Lifetime,
     if bounds.len() == 0 {
         Some(result)
     } else {
-        let appendix: Vec<_> = try_opt!(bounds.into_iter()
-            .map(|b| b.rewrite(context, shape))
-            .collect());
+        let appendix: Vec<_> =
+            try_opt!(bounds.into_iter().map(|b| b.rewrite(context, shape)).collect());
         let colon = type_bound_colon(context);
         let result = format!("{}{}{}", result, colon, appendix.join(" + "));
         wrap_str(result, context.config.max_width, shape)
@@ -486,9 +485,7 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             TypeDensity::Compressed => "+",
             TypeDensity::Wide => " + ",
         };
-        let strs: Vec<_> = try_opt!(self.iter()
-            .map(|b| b.rewrite(context, shape))
-            .collect());
+        let strs: Vec<_> = try_opt!(self.iter().map(|b| b.rewrite(context, shape)).collect());
         wrap_str(strs.join(joiner), context.config.max_width, shape)
     }
 }
@@ -506,11 +503,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 result.push_str(" ");
             }
 
-            let bounds: String = try_opt!(self.bounds
-                .iter()
-                .map(|ty_bound| ty_bound.rewrite(context, shape))
-                .intersperse(Some(" + ".to_string()))
-                .collect());
+            let bounds: String =
+                try_opt!(self.bounds
+                             .iter()
+                             .map(|ty_bound| ty_bound.rewrite(context, shape))
+                             .intersperse(Some(" + ".to_string()))
+                             .collect());
 
             result.push_str(&bounds);
         }
@@ -522,8 +520,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
             };
             result.push_str(eq_str);
             let budget = try_opt!(shape.width.checked_sub(result.len()));
-            let rewrite =
-                try_opt!(def.rewrite(context, Shape::legacy(budget, shape.indent + result.len())));
+            let rewrite = try_opt!(def.rewrite(context,
+                                               Shape::legacy(budget, shape.indent + result.len())));
             result.push_str(&rewrite);
         }
 
@@ -535,23 +533,24 @@ impl Rewrite for ast::PolyTraitRef {
     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         if !self.bound_lifetimes.is_empty() {
             let lifetime_str: String = try_opt!(self.bound_lifetimes
-                .iter()
-                .map(|lt| lt.rewrite(context, shape))
-                .intersperse(Some(", ".to_string()))
-                .collect());
+                                                    .iter()
+                                                    .map(|lt| lt.rewrite(context, shape))
+                                                    .intersperse(Some(", ".to_string()))
+                                                    .collect());
 
             // 6 is "for<> ".len()
             let extra_offset = lifetime_str.len() + 6;
             let max_path_width = try_opt!(shape.width.checked_sub(extra_offset));
-            let path_str = try_opt!(self.trait_ref
-                .rewrite(context,
-                         Shape::legacy(max_path_width, shape.indent + extra_offset)));
+            let path_str = try_opt!(self.trait_ref.rewrite(context,
+                                                           Shape::legacy(max_path_width,
+                                                                         shape.indent +
+                                                                         extra_offset)));
 
             Some(if context.config.spaces_within_angle_brackets && lifetime_str.len() > 0 {
-                format!("for< {} > {}", lifetime_str, path_str)
-            } else {
-                format!("for<{}> {}", lifetime_str, path_str)
-            })
+                     format!("for< {} > {}", lifetime_str, path_str)
+                 } else {
+                     format!("for<{}> {}", lifetime_str, path_str)
+                 })
         } else {
             self.trait_ref.rewrite(context, shape)
         }
@@ -580,31 +579,31 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 let mut_str = format_mutability(mt.mutbl);
                 let mut_len = mut_str.len();
                 Some(match *lifetime {
-                    Some(ref lifetime) => {
-                        let lt_budget = try_opt!(shape.width.checked_sub(2 + mut_len));
-                        let lt_str = try_opt!(lifetime.rewrite(context,
-                                                               Shape::legacy(lt_budget,
-                                                                             shape.indent + 2 +
-                                                                             mut_len)));
-                        let lt_len = lt_str.len();
-                        let budget = try_opt!(shape.width.checked_sub(2 + mut_len + lt_len));
-                        format!("&{} {}{}",
-                                lt_str,
-                                mut_str,
-                                try_opt!(mt.ty
-                                    .rewrite(context,
-                                             Shape::legacy(budget,
-                                                           shape.indent + 2 + mut_len + lt_len))))
-                    }
-                    None => {
-                        let budget = try_opt!(shape.width.checked_sub(1 + mut_len));
-                        format!("&{}{}",
-                                mut_str,
-                                try_opt!(mt.ty.rewrite(context,
-                                                       Shape::legacy(budget,
-                                                                     shape.indent + 1 + mut_len))))
-                    }
-                })
+                         Some(ref lifetime) => {
+                    let lt_budget = try_opt!(shape.width.checked_sub(2 + mut_len));
+                    let lt_str = try_opt!(lifetime.rewrite(context,
+                                                           Shape::legacy(lt_budget,
+                                                                         shape.indent + 2 +
+                                                                         mut_len)));
+                    let lt_len = lt_str.len();
+                    let budget = try_opt!(shape.width.checked_sub(2 + mut_len + lt_len));
+                    format!("&{} {}{}",
+                            lt_str,
+                            mut_str,
+                            try_opt!(mt.ty.rewrite(context,
+                                                   Shape::legacy(budget,
+                                                                 shape.indent + 2 + mut_len +
+                                                                 lt_len))))
+                }
+                         None => {
+                    let budget = try_opt!(shape.width.checked_sub(1 + mut_len));
+                    format!("&{}{}",
+                            mut_str,
+                            try_opt!(mt.ty.rewrite(context,
+                                                   Shape::legacy(budget,
+                                                                 shape.indent + 1 + mut_len))))
+                }
+                     })
             }
             // FIXME: we drop any comments here, even though it's a silly place to put
             // comments.
@@ -612,10 +611,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 let budget = try_opt!(shape.width.checked_sub(2));
                 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
                     .map(|ty_str| if context.config.spaces_within_parens {
-                        format!("( {} )", ty_str)
-                    } else {
-                        format!("({})", ty_str)
-                    })
+                             format!("( {} )", ty_str)
+                         } else {
+                             format!("({})", ty_str)
+                         })
             }
             ast::TyKind::Slice(ref ty) => {
                 let budget = if context.config.spaces_within_square_brackets {
@@ -625,10 +624,10 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 };
                 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
                     .map(|ty_str| if context.config.spaces_within_square_brackets {
-                        format!("[ {} ]", ty_str)
-                    } else {
-                        format!("[{}]", ty_str)
-                    })
+                             format!("[ {} ]", ty_str)
+                         } else {
+                             format!("[{}]", ty_str)
+                         })
             }
             ast::TyKind::Tup(ref items) => {
                 rewrite_tuple(context, items.iter().map(|x| &**x), self.span, shape)
@@ -674,13 +673,13 @@ fn rewrite_bare_fn(bare_fn: &ast::BareFnTy,
         // This doesn't work out so nicely for mutliline situation with lots of
         // rightward drift. If that is a problem, we could use the list stuff.
         result.push_str(&try_opt!(bare_fn.lifetimes
-            .iter()
-            .map(|l| {
-                l.rewrite(context,
-                          Shape::legacy(try_opt!(shape.width.checked_sub(6)), shape.indent + 4))
-            })
-            .intersperse(Some(", ".to_string()))
-            .collect::<Option<String>>()));
+                                      .iter()
+                                      .map(|l| {
+                                               l.rewrite(context,
+                      Shape::legacy(try_opt!(shape.width.checked_sub(6)), shape.indent + 4))
+                                           })
+                                      .intersperse(Some(", ".to_string()))
+                                      .collect::<Option<String>>()));
         result.push_str("> ");
     }