Skip to content

Commit

Permalink
refactor: apply heuristic config changes in lib
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Apr 22, 2021
1 parent ea1611c commit 1bcc1f8
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/attr.rs
Expand Up @@ -300,7 +300,7 @@ impl Rewrite for ast::MetaItem {
// 1 = "]"
shape.sub_width(1)?,
self.span,
context.config.width_heuristics().attr_fn_like_width,
context.config.attr_fn_like_width(),
Some(if has_trailing_comma {
SeparatorTactic::Always
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/chains.rs
Expand Up @@ -577,7 +577,7 @@ impl<'a> ChainFormatterShared<'a> {
let one_line_budget = if self.child_count == 1 {
shape.width
} else {
min(shape.width, context.config.width_heuristics().chain_width)
min(shape.width, context.config.chain_width())
}
.saturating_sub(almost_total);

Expand Down
19 changes: 4 additions & 15 deletions src/expr.rs
Expand Up @@ -899,22 +899,11 @@ impl<'a> ControlFlow<'a> {
|| last_line_offsetted(shape.used_width(), &pat_expr_string));

// Try to format if-else on single line.
if self.allow_single_line
&& context
.config
.width_heuristics()
.single_line_if_else_max_width
> 0
{
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);

if let Some(cond_str) = trial {
if cond_str.len()
<= context
.config
.width_heuristics()
.single_line_if_else_max_width
{
if cond_str.len() <= context.config.single_line_if_else_max_width() {
return Some((cond_str, 0));
}
}
Expand Down Expand Up @@ -1246,7 +1235,7 @@ pub(crate) fn rewrite_call(
args.iter(),
shape,
span,
context.config.width_heuristics().fn_call_width,
context.config.fn_call_width(),
choose_separator_tactic(context, span),
)
}
Expand Down Expand Up @@ -1785,7 +1774,7 @@ pub(crate) fn rewrite_tuple<'a, T: 'a + IntoOverflowableItem<'a>>(
items,
shape,
span,
context.config.width_heuristics().fn_call_width,
context.config.fn_call_width(),
force_tactic,
)
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/items.rs
Expand Up @@ -505,8 +505,8 @@ impl<'a> FmtVisitor<'a> {
)
.collect()
};
let mut items: Vec<_> =
itemize_list_with(self.config.width_heuristics().struct_variant_width);
let mut items: Vec<_> = itemize_list_with(self.config.struct_variant_width());

// 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_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains('\n'));
Expand Down Expand Up @@ -1479,7 +1479,7 @@ fn format_tuple_struct(
fields.iter(),
shape,
span,
context.config.width_heuristics().fn_call_width,
context.config.fn_call_width(),
None,
)?;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lists.rs
Expand Up @@ -863,7 +863,7 @@ pub(crate) fn struct_lit_shape(
};
let shape_width = shape.width.checked_sub(prefix_width + suffix_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))
} else {
Some((None, v_shape))
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Expand Up @@ -382,7 +382,7 @@ fn rewrite_macro_inner(
arg_vec.iter(),
shape,
mac.span(),
context.config.width_heuristics().fn_call_width,
context.config.fn_call_width(),
if trailing_comma {
Some(SeparatorTactic::Always)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/overflow.rs
Expand Up @@ -318,7 +318,7 @@ pub(crate) fn rewrite_with_square_brackets<'a, T: 'a + IntoOverflowableItem<'a>>
span,
lhs,
rhs,
context.config.width_heuristics().array_width,
context.config.array_width(),
force_separator_tactic,
Some(("[", "]")),
)
Expand Down
26 changes: 26 additions & 0 deletions tests/target/issue_4049.rs
@@ -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");
}
}
}
}

0 comments on commit 1bcc1f8

Please sign in to comment.