use syntax::symbol::keywords;
use config::{IndentStyle, TypeDensity};
-use expr::{rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ToExpr};
+use expr::{rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix};
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use macros::{rewrite_macro, MacroPosition};
use overflow;
for segment in iter {
// Indicates a global path, shouldn't be rendered.
- if segment.ident.name == keywords::CrateRoot.name() {
+ if segment.ident.name == keywords::PathRoot.name() {
continue;
}
if first {
}
#[derive(Debug)]
-enum SegmentParam<'a> {
+pub enum SegmentParam<'a> {
LifeTime(&'a ast::Lifetime),
Type(&'a ast::Ty),
Binding(&'a ast::TypeBinding),
}
}
-impl<'a> ToExpr for SegmentParam<'a> {
- fn to_expr(&self) -> Option<&ast::Expr> {
- None
- }
-
- fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
- match *self {
- SegmentParam::Type(ty) => ty.can_be_overflowed(context, len),
- _ => false,
- }
- }
-}
-
impl<'a> Rewrite for SegmentParam<'a> {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
match *self {
.chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x)))
.collect::<Vec<_>>();
- let separator = if path_context == PathContext::Expr {
+ let force_separator =
+ context.inside_macro() && context.snippet(data.span).starts_with("::");
+ let separator = if path_context == PathContext::Expr || force_separator {
"::"
} else {
""
let generics_str = overflow::rewrite_with_angle_brackets(
context,
"",
- ¶m_list.iter().map(|e| &*e).collect::<Vec<_>>(),
+ param_list.iter(),
shape,
mk_sp(*span_lo, span_hi),
)?;
{
debug!("format_function_type {:#?}", shape);
+ let ty_shape = match context.config.indent_style() {
+ // 4 = " -> "
+ IndentStyle::Block => shape.offset_left(4)?,
+ IndentStyle::Visual => shape.block_left(4)?,
+ };
+ let output = match *output {
+ FunctionRetTy::Ty(ref ty) => {
+ let type_str = ty.rewrite(context, ty_shape)?;
+ format!(" -> {}", type_str)
+ }
+ FunctionRetTy::Default(..) => String::new(),
+ };
+
// Code for handling variadics is somewhat duplicated for items, but they
// are different enough to need some serious refactoring to share code.
enum ArgumentKind<T>
None
};
- // 2 for ()
- let budget = shape.width.checked_sub(2)?;
- // 1 for (
- let offset = match context.config.indent_style() {
- IndentStyle::Block => {
- shape
- .block()
- .block_indent(context.config.tab_spaces())
- .indent
- }
- IndentStyle::Visual => shape.indent + 1,
+ let list_shape = if context.use_block_indent() {
+ Shape::indented(
+ shape.block().indent.block_indent(context.config),
+ context.config,
+ )
+ } else {
+ // 2 for ()
+ let budget = shape.width.checked_sub(2)?;
+ // 1 for (
+ let offset = shape.indent + 1;
+ Shape::legacy(budget, offset)
};
- let list_shape = Shape::legacy(budget, offset);
let list_lo = context.snippet_provider.span_after(span, "(");
let items = itemize_list(
context.snippet_provider,
let item_vec: Vec<_> = items.collect();
- let tactic = definitive_tactic(
- &*item_vec,
- ListTactic::HorizontalVertical,
- Separator::Comma,
- budget,
- );
+ // If the return type is multi-lined, then force to use multiple lines for
+ // arguments as well.
+ let tactic = if output.contains('\n') {
+ DefinitiveListTactic::Vertical
+ } else {
+ definitive_tactic(
+ &*item_vec,
+ ListTactic::HorizontalVertical,
+ Separator::Comma,
+ shape.width.saturating_sub(2 + output.len()),
+ )
+ };
let trailing_separator = if !context.use_block_indent() || variadic {
SeparatorTactic::Never
} else {
.preserve_newline(true);
let list_str = write_list(&item_vec, &fmt)?;
- let ty_shape = match context.config.indent_style() {
- // 4 = " -> "
- IndentStyle::Block => shape.offset_left(4)?,
- IndentStyle::Visual => shape.block_left(4)?,
- };
- let output = match *output {
- FunctionRetTy::Ty(ref ty) => {
- let type_str = ty.rewrite(context, ty_shape)?;
- format!(" -> {}", type_str)
- }
- FunctionRetTy::Default(..) => String::new(),
- };
-
- let args = if (!list_str.contains('\n') || list_str.is_empty()) && !output.contains('\n')
- || !context.use_block_indent()
- {
+ let args = if tactic == DefinitiveListTactic::Horizontal || !context.use_block_indent() {
format!("({})", list_str)
} else {
format!(
"({}{}{})",
- offset.to_string_with_newline(context.config),
+ list_shape.indent.to_string_with_newline(context.config),
list_str,
shape.block().indent.to_string_with_newline(context.config),
)
Some(format!(
"{}\n{}{}",
args,
- offset.to_string(context.config),
- output.trim_left()
+ list_shape.indent.to_string(context.config),
+ output.trim_start()
))
}
}
..
}) => {
let type_str = bounded_ty.rewrite(context, shape)?;
- let colon = type_bound_colon(context).trim_right();
+ let colon = type_bound_colon(context).trim_end();
let lhs = if let Some(lifetime_str) =
rewrite_lifetime_param(context, shape, bound_generic_params)
{
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
match *self {
ast::GenericBound::Trait(ref poly_trait_ref, trait_bound_modifier) => {
- match trait_bound_modifier {
+ let snippet = context.snippet(self.span());
+ let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
+ let rewrite = match trait_bound_modifier {
ast::TraitBoundModifier::None => poly_trait_ref.rewrite(context, shape),
- ast::TraitBoundModifier::Maybe => {
- let rw = poly_trait_ref.rewrite(context, shape.offset_left(1)?)?;
- Some(format!("?{}", rw))
- }
- }
+ ast::TraitBoundModifier::Maybe => poly_trait_ref
+ .rewrite(context, shape.offset_left(1)?)
+ .map(|s| format!("?{}", s)),
+ };
+ rewrite.map(|s| if has_paren { format!("({})", s) } else { s })
}
ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),
}
return Some(String::new());
}
- let span = mk_sp(self.get(0)?.span().lo(), self.last()?.span().hi());
- let has_paren = context.snippet(span).starts_with('(');
- let bounds_shape = if has_paren {
- shape.offset_left(1)?.sub_width(1)?
- } else {
- shape
- };
- join_bounds(context, bounds_shape, self, true).map(|s| {
- if has_paren {
- format!("({})", s)
- } else {
- s
- }
- })
+ join_bounds(context, shape, self, true)
}
}
ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
.map(|ty_str| format!("[{}]", ty_str))
}
- ast::TyKind::Tup(ref items) => rewrite_tuple(
- context,
- &::utils::ptr_vec_to_ref_vec(items),
- self.span,
- shape,
- ),
+ ast::TyKind::Tup(ref items) => {
+ rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
+ }
ast::TyKind::Path(ref q_self, ref path) => {
rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
}
items: &[ast::GenericBound],
need_indent: bool,
) -> Option<String> {
+ debug_assert!(!items.is_empty());
+
// Try to join types in a single line
let joiner = match context.config.type_punctuation_density() {
TypeDensity::Compressed => "+",
.filter(|p| match p.kind {
ast::GenericParamKind::Lifetime => true,
_ => false,
- }).map(|lt| lt.rewrite(context, shape))
+ })
+ .map(|lt| lt.rewrite(context, shape))
.collect::<Option<Vec<_>>>()?
.join(", ");
if result.is_empty() {