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,
span_lo,
path.span.hi,
context,
- Shape::legacy(budget,
- shape.indent + extra_offset)));
+ shape));
}
if context.config.spaces_within_angle_brackets {
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,
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.
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);
}
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)
}
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 {
.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 {
"::"
// 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;
false,
data.span,
context,
- Shape::legacy(width, offset)))
+ shape))
}
_ => String::new(),
}
};
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 {
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)
}
};
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)
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)
}
}
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);
}
};
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);
}
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)
}
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.
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 {
};
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)
// 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("> ");
}