refactor: apply heuristic config changes in lib

This commit is contained in:
Caleb Cartwright 2020-02-22 22:07:50 -06:00 committed by Caleb Cartwright
parent ea1611c06e
commit 1bcc1f8df5
8 changed files with 38 additions and 23 deletions

View File

@ -300,7 +300,7 @@ impl Rewrite for ast::MetaItem {
// 1 = "]" // 1 = "]"
shape.sub_width(1)?, shape.sub_width(1)?,
self.span, self.span,
context.config.width_heuristics().attr_fn_like_width, context.config.attr_fn_like_width(),
Some(if has_trailing_comma { Some(if has_trailing_comma {
SeparatorTactic::Always SeparatorTactic::Always
} else { } else {

View File

@ -577,7 +577,7 @@ impl<'a> ChainFormatterShared<'a> {
let one_line_budget = if self.child_count == 1 { let one_line_budget = if self.child_count == 1 {
shape.width shape.width
} else { } else {
min(shape.width, context.config.width_heuristics().chain_width) min(shape.width, context.config.chain_width())
} }
.saturating_sub(almost_total); .saturating_sub(almost_total);

View File

@ -899,22 +899,11 @@ impl<'a> ControlFlow<'a> {
|| last_line_offsetted(shape.used_width(), &pat_expr_string)); || last_line_offsetted(shape.used_width(), &pat_expr_string));
// Try to format if-else on single line. // Try to format if-else on single line.
if self.allow_single_line if self.allow_single_line && context.config.single_line_if_else_max_width() > 0 {
&& context
.config
.width_heuristics()
.single_line_if_else_max_width
> 0
{
let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width); let trial = self.rewrite_single_line(&pat_expr_string, context, shape.width);
if let Some(cond_str) = trial { if let Some(cond_str) = trial {
if cond_str.len() if cond_str.len() <= context.config.single_line_if_else_max_width() {
<= context
.config
.width_heuristics()
.single_line_if_else_max_width
{
return Some((cond_str, 0)); return Some((cond_str, 0));
} }
} }
@ -1246,7 +1235,7 @@ pub(crate) fn rewrite_call(
args.iter(), args.iter(),
shape, shape,
span, span,
context.config.width_heuristics().fn_call_width, context.config.fn_call_width(),
choose_separator_tactic(context, span), choose_separator_tactic(context, span),
) )
} }
@ -1785,7 +1774,7 @@ pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
items, items,
shape, shape,
span, span,
context.config.width_heuristics().fn_call_width, context.config.fn_call_width(),
force_tactic, force_tactic,
) )
} else { } else {

View File

@ -505,8 +505,8 @@ impl<'a> FmtVisitor<'a> {
) )
.collect() .collect()
}; };
let mut items: Vec<_> = let mut items: Vec<_> = itemize_list_with(self.config.struct_variant_width());
itemize_list_with(self.config.width_heuristics().struct_variant_width);
// If one of the variants use multiple lines, use multi-lined formatting for all variants. // If one of the variants use multiple lines, use multi-lined formatting for all variants.
let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains('\n')); let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains('\n'));
let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains('\n')); let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains('\n'));
@ -1479,7 +1479,7 @@ fn format_tuple_struct(
fields.iter(), fields.iter(),
shape, shape,
span, span,
context.config.width_heuristics().fn_call_width, context.config.fn_call_width(),
None, None,
)?; )?;
} }

View File

@ -863,7 +863,7 @@ pub(crate) fn struct_lit_shape(
}; };
let shape_width = shape.width.checked_sub(prefix_width + suffix_width); let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
if let Some(w) = shape_width { if let Some(w) = shape_width {
let shape_width = cmp::min(w, context.config.width_heuristics().struct_lit_width); let shape_width = cmp::min(w, context.config.struct_lit_width());
Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape)) Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
} else { } else {
Some((None, v_shape)) Some((None, v_shape))

View File

@ -382,7 +382,7 @@ fn rewrite_macro_inner(
arg_vec.iter(), arg_vec.iter(),
shape, shape,
mac.span(), mac.span(),
context.config.width_heuristics().fn_call_width, context.config.fn_call_width(),
if trailing_comma { if trailing_comma {
Some(SeparatorTactic::Always) Some(SeparatorTactic::Always)
} else { } else {

View File

@ -318,7 +318,7 @@ pub(crate) fn rewrite_with_square_brackets<'a, T: 'a + IntoOverflowableItem<'a>>
span, span,
lhs, lhs,
rhs, rhs,
context.config.width_heuristics().array_width, context.config.array_width(),
force_separator_tactic, force_separator_tactic,
Some(("[", "]")), Some(("[", "]")),
) )

View File

@ -0,0 +1,26 @@
// rustfmt-max_width: 110
// rustfmt-use_small_heuristics: Max
// rustfmt-hard_tabs: true
// rustfmt-use_field_init_shorthand: true
// rustfmt-overflow_delimited_expr: true
// https://github.com/rust-lang/rustfmt/issues/4049
fn foo() {
{
{
if let Some(MpcEv::PlayDrum(pitch, vel)) =
// self.mpc.handle_input(e, /*btn_ctrl_down,*/ tx_launch_to_daw, state_view)
self.mpc.handle_input(e, &mut MyBorrowedState { tx_launch_to_daw, state_view })
{
println!("bar");
}
if let Some(e) =
// self.note_input.handle_input(e, /*btn_ctrl_down,*/ tx_launch_to_daw, state_view)
self.note_input.handle_input(e, &mut MyBorrowedState { tx_launch_to_daw, state_view })
{
println!("baz");
}
}
}
}