semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
colon_spaces};
use visitor::FmtVisitor;
-use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle};
+use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
use types::{rewrite_path, PathContext};
use items::{span_lo_for_arg, span_hi_for_arg};
.max_width
.checked_sub(shape.used_width() + prefix.len() +
infix.len()));
- let rhs_shape = try_opt!(shape.sub_width(suffix.len() + prefix.len()))
- .visual_indent(prefix.len());
+ let rhs_shape = match context.config.control_style {
+ Style::Default => {
+ try_opt!(shape.sub_width(suffix.len() + prefix.len())).visual_indent(prefix.len())
+ }
+ Style::Rfc => try_opt!(shape.block_left(context.config.tab_spaces)),
+ };
let rhs_result = try_opt!(rhs.rewrite(context, rhs_shape));
let lhs_result = try_opt!(lhs.rewrite(context,
// We need braces, but we might still prefer a one-liner.
let stmt = &block.stmts[0];
// 4 = braces and spaces.
- let mut rewrite = stmt.rewrite(context, try_opt!(body_shape.sub_width(4)));
-
- // Checks if rewrite succeeded and fits on a single line.
- rewrite = and_one_line(rewrite);
-
- if let Some(rewrite) = rewrite {
- return Some(format!("{} {{ {} }}", prefix, rewrite));
+ if let Some(body_shape) = body_shape.sub_width(4) {
+ // Checks if rewrite succeeded and fits on a single line.
+ if let Some(rewrite) = and_one_line(stmt.rewrite(context, body_shape)) {
+ return Some(format!("{} {{ {} }}", prefix, rewrite));
+ }
}
}
// The body of the closure is big enough to be block indented, that
// means we must re-format.
- let block_shape = Shape {
- width: context.config.max_width - shape.block().indent.width(),
- ..shape.block()
- };
+ let block_shape = shape.block().with_max_width(context.config);
let block_str = try_opt!(block.rewrite(&context, block_shape));
Some(format!("{} {}",
prefix,
let pat_expr_string = match self.cond {
Some(cond) => {
- let mut cond_shape = try_opt!(constr_shape.shrink_left(add_offset));
+ let mut cond_shape = match context.config.control_style {
+ Style::Default => try_opt!(constr_shape.shrink_left(add_offset)),
+ Style::Rfc => constr_shape,
+ };
if context.config.control_brace_style != ControlBraceStyle::AlwaysNextLine {
// 2 = " {".len()
cond_shape = try_opt!(cond_shape.sub_width(2));
None => String::new(),
};
+ let force_newline_brace = context.config.control_style == Style::Rfc &&
+ pat_expr_string.contains('\n');
+
// Try to format if-else on single line.
if self.allow_single_line && context.config.single_line_if_else_max_width > 0 {
let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width);
&shape.indent.block_only().to_string(context.config);
let block_sep = if self.cond.is_none() && between_kwd_cond_comment.is_some() {
""
- } else if context.config.control_brace_style ==
- ControlBraceStyle::AlwaysNextLine {
+ } else if context.config.control_brace_style == ControlBraceStyle::AlwaysNextLine ||
+ force_newline_brace {
alt_block_sep.as_str()
} else {
" "
result.push('\n');
result.push_str(&arm_indent_str);
- let arm_str = arm.rewrite(&context,
- Shape {
- width: context.config.max_width - arm_shape.indent.width(),
- ..arm_shape
- });
+ let arm_str = arm.rewrite(&context, arm_shape.with_max_width(context.config));
if let Some(ref arm_str) = arm_str {
result.push_str(arm_str);
} else {
let pats_str = try_opt!(write_list(items, &fmt));
let guard_shape = if pats_str.contains('\n') {
- Shape {
- width: context.config.max_width - shape.indent.width(),
- ..shape
- }
+ shape.with_max_width(context.config)
} else {
shape
};
connector: &str,
shape: Shape)
-> Option<String> {
- debug!("rewrite_pat_expr {:?} {:?}", shape, pat);
+ debug!("rewrite_pat_expr {:?} {:?} {:?}", shape, pat, expr);
let mut result = match pat {
Some(pat) => {
let matcher = if matcher.is_empty() {
tactic: tactic,
separator: ",",
trailing_separator: if force_no_trailing_comma ||
- context.config.fn_call_style == IndentStyle::Visual {
+ context.config.fn_call_style == IndentStyle::Visual ||
+ args.len() <= 1 {
SeparatorTactic::Never
} else {
context.config.trailing_comma
let prefix = format!("{}static {}{}: ", vis, mut_str, item.ident);
let offset = self.block_indent + prefix.len();
// 1 = ;
- let width = self.config.max_width - offset.width() - 1;
- let rewrite = ty.rewrite(&self.get_context(), Shape::legacy(width, offset));
+ let shape = Shape::indented(offset, self.config).sub_width(1).unwrap();
+ let rewrite = ty.rewrite(&self.get_context(), shape);
match rewrite {
Some(result) => {
let suffix = if semicolon_for_expr(e) { ";" } else { "" };
e.rewrite(&self.get_context(),
- Shape::legacy(self.config.max_width -
- self.block_indent.width(),
- self.block_indent))
+ Shape::indented(self.block_indent, self.config))
.map(|s| s + suffix)
.or_else(|| Some(self.snippet(e.span)))
}
None => {
stmt.rewrite(&self.get_context(),
- Shape::legacy(self.config.max_width -
- self.block_indent.width(),
- self.block_indent))
+ Shape::indented(self.block_indent, self.config))
}
}
} else {
body_lo,
body_hi);
- let budget = self.config.max_width - self.block_indent.width() - 2;
+ let shape = Shape::indented(self.block_indent, self.config)
+ .sub_width(2)
+ .unwrap();
let fmt = ListFormatting {
tactic: DefinitiveListTactic::Vertical,
separator: ",",
trailing_separator: self.config.trailing_comma,
- shape: Shape::legacy(budget, self.block_indent),
+ shape: shape,
ends_with_newline: true,
config: self.config,
};
.node
.attrs
.rewrite(&self.get_context(),
- Shape::legacy(self.config.max_width -
- indent.width(),
- indent)));
+ Shape::indented(indent, self.config)));
if !result.is_empty() {
result.push('\n');
result.push_str(&indent.to_string(self.config));
wrap_str(tag,
self.config.max_width,
- Shape::legacy(self.config.max_width - indent.width(), indent))
+ Shape::indented(indent, self.config))
}
};
None => self_ty.span.lo,
};
let generics_indent = offset + last_line_width(&result);
- let shape = try_opt!(generics_shape(context.config, generics_indent));
+ let shape = Shape::indented(generics_indent, context.config);
let generics_str = try_opt!(rewrite_generics(context, generics, shape, mk_sp(lo, hi)));
result.push_str(&generics_str);
// Can't fit the self type on what's left of the line, so start a new one.
let indent = offset.block_indent(context.config);
result.push_str(&format!("\n{}", indent.to_string(context.config)));
- let budget = try_opt!(context.config.max_width.checked_sub(indent.width()));
- result.push_str(&*try_opt!(self_ty.rewrite(context, Shape::legacy(budget, indent))));
+ result.push_str(&*try_opt!(self_ty.rewrite(context,
+ Shape::indented(indent, context.config))));
Some(result)
} else {
unreachable!();
let body_lo = context.codemap.span_after(item.span, "{");
let generics_indent = offset + last_line_width(&result);
- let shape = try_opt!(generics_shape(context.config, generics_indent));
+ let shape = Shape::indented(generics_indent, context.config);
let generics_str =
try_opt!(rewrite_generics(context, generics, shape, mk_sp(item.span.lo, body_lo)));
result.push_str(&generics_str);
let where_clause_str = match generics {
Some(generics) => {
let generics_indent = offset + last_line_width(&header_str);
- let shape = try_opt!(generics_shape(context.config, generics_indent));
+ let shape = Shape::indented(generics_indent, context.config);
let generics_str =
try_opt!(rewrite_generics(context, generics, shape, mk_sp(span.lo, body_lo)));
result.push_str(&generics_str);
let generics_indent = indent + result.len();
let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
- let shape = try_opt!(try_opt!(generics_shape(context.config, generics_indent))
+ let shape = try_opt!(Shape::indented(generics_indent, context.config)
.sub_width(" =".len()));
let generics_str = try_opt!(rewrite_generics(context, generics, shape, generics_span));
let name = self.ident;
let vis = format_visibility(&self.vis);
- let mut attr_str = try_opt!(self.attrs
- .rewrite(context,
- Shape::legacy(context.config.max_width -
- shape.indent.width(),
- shape.indent)));
+ let mut attr_str =
+ try_opt!(self.attrs
+ .rewrite(context, Shape::indented(shape.indent, context.config)));
if !attr_str.is_empty() {
attr_str.push('\n');
attr_str.push_str(&shape.indent.to_string(context.config));
let type_offset = shape.indent.block_indent(context.config);
let rewrite_type_in_next_line = || {
- let budget = try_opt!(context.config.max_width.checked_sub(type_offset.width()));
- self.ty.rewrite(context, Shape::legacy(budget, type_offset))
+ self.ty
+ .rewrite(context, Shape::indented(type_offset, context.config))
};
let last_line_width = last_line_width(&result) + type_annotation_spacing.1.len();
// Generics.
let generics_indent = indent + last_line_width(&result);
let generics_span = mk_sp(span.lo, span_for_return(&fd.output).lo);
- let shape = try_opt!(generics_shape(context.config, generics_indent));
+ let shape = Shape::indented(generics_indent, context.config);
let generics_str = try_opt!(rewrite_generics(context, generics, shape, generics_span));
result.push_str(&generics_str);
// Note that the width and indent don't really matter, we'll re-layout the
// return type later anyway.
let ret_str = try_opt!(fd.output
- .rewrite(&context,
- Shape::legacy(context.config.max_width -
- indent.width(),
- indent)));
+ .rewrite(&context, Shape::indented(indent, context.config)));
let multi_line_ret_str = ret_str.contains('\n');
let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
indent,
arg_indent,
args_span,
- fd.variadic));
+ fd.variadic,
+ generics_str.contains('\n')));
let multi_line_arg_str = arg_str.contains('\n');
if multi_line_ret_str || ret_should_indent {
// Now that we know the proper indent and width, we need to
// re-layout the return type.
- let budget = try_opt!(context.config.max_width.checked_sub(ret_indent.width()));
let ret_str = try_opt!(fd.output
- .rewrite(context, Shape::legacy(budget, ret_indent)));
+ .rewrite(context,
+ Shape::indented(ret_indent, context.config)));
result.push_str(&ret_str);
} else {
result.push_str(&ret_str);
}
}
- let budget = try_opt!(context.config.max_width.checked_sub(indent.block_indent));
let where_clause_str = try_opt!(rewrite_where_clause(context,
where_clause,
context.config.fn_brace_style,
- Shape::legacy(budget, indent),
+ Shape::indented(indent, context.config),
Density::Tall,
"{",
!has_braces,
indent: Indent,
arg_indent: Indent,
span: Span,
- variadic: bool)
+ variadic: bool,
+ generics_str_contains_newline: bool)
-> Option<String> {
let mut arg_item_strs =
try_opt!(args.iter()
}
let (indent, trailing_comma, end_with_newline) = match context.config.fn_args_layout {
+ IndentStyle::Block if !generics_str_contains_newline && arg_items.len() <= 1 => {
+ (indent.block_indent(context.config), SeparatorTactic::Never, true)
+ }
IndentStyle::Block => {
(indent.block_indent(context.config), SeparatorTactic::Vertical, true)
}
offset: Indent,
span: Span)
-> Option<String> {
- let shape = try_opt!(generics_shape(context.config, offset));
+ let shape = Shape::indented(offset, context.config);
let mut result = try_opt!(rewrite_generics(context, generics, shape, span));
if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
Some(result)
}
-
-fn generics_shape(config: &Config, indent: Indent) -> Option<Shape> {
- Some(Shape::legacy(try_opt!(config.max_width.checked_sub(indent.width())),
- indent))
-}