mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-23 04:14:28 +00:00
Merge pull request #2667 from tspiteri/width-heuristics
Do not scale WidthHeuristics when max_width less than 100
This commit is contained in:
commit
4a57e79469
@ -130,7 +130,7 @@ macro_rules! create_config {
|
||||
pub fn $i(&mut self, value: $ty) {
|
||||
(self.0).$i.2 = value;
|
||||
match stringify!($i) {
|
||||
"use_small_heuristics" => self.0.set_heuristics(),
|
||||
"max_width" | "use_small_heuristics" => self.0.set_heuristics(),
|
||||
"license_template_path" => self.0.set_license_template(),
|
||||
&_ => (),
|
||||
}
|
||||
@ -292,7 +292,7 @@ macro_rules! create_config {
|
||||
}
|
||||
|
||||
match key {
|
||||
"use_small_heuristics" => self.set_heuristics(),
|
||||
"max_width" | "use_small_heuristics" => self.set_heuristics(),
|
||||
"license_template_path" => self.set_license_template(),
|
||||
&_ => (),
|
||||
}
|
||||
|
@ -242,8 +242,14 @@ impl WidthHeuristics {
|
||||
|
||||
// scale the default WidthHeuristics according to max_width
|
||||
pub fn scaled(max_width: usize) -> WidthHeuristics {
|
||||
let mut max_width_ratio: f32 = max_width as f32 / 100.0; // 100 is the default width -> default ratio is 1
|
||||
max_width_ratio = (max_width_ratio * 10.0).round() / 10.0; // round to the closest 0.1
|
||||
const DEFAULT_MAX_WIDTH: usize = 100;
|
||||
let max_width_ratio = if max_width > DEFAULT_MAX_WIDTH {
|
||||
let ratio = max_width as f32 / DEFAULT_MAX_WIDTH as f32;
|
||||
// round to the closest 0.1
|
||||
(ratio * 10.0).round() / 10.0
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
WidthHeuristics {
|
||||
fn_call_width: (60.0 * max_width_ratio).round() as usize,
|
||||
struct_lit_width: (18.0 * max_width_ratio).round() as usize,
|
||||
|
11
tests/source/issue-2644.rs
Normal file
11
tests/source/issue-2644.rs
Normal file
@ -0,0 +1,11 @@
|
||||
// rustfmt-max_width: 80
|
||||
fn foo(e: Enum) {
|
||||
match e {
|
||||
Enum::Var {
|
||||
element1,
|
||||
element2,
|
||||
} => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
28
tests/source/width-heuristics.rs
Normal file
28
tests/source/width-heuristics.rs
Normal file
@ -0,0 +1,28 @@
|
||||
// rustfmt-max_width: 120
|
||||
|
||||
// elems on multiple lines for max_width 100, but same line for max_width 120
|
||||
fn foo(e: Enum) {
|
||||
match e {
|
||||
Enum::Var {
|
||||
elem1,
|
||||
elem2,
|
||||
elem3,
|
||||
} => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// elems not on same line for either max_width 100 or 120
|
||||
fn bar(e: Enum) {
|
||||
match e {
|
||||
Enum::Var {
|
||||
elem1,
|
||||
elem2,
|
||||
elem3,
|
||||
elem4,
|
||||
} => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
8
tests/target/issue-2644.rs
Normal file
8
tests/target/issue-2644.rs
Normal file
@ -0,0 +1,8 @@
|
||||
// rustfmt-max_width: 80
|
||||
fn foo(e: Enum) {
|
||||
match e {
|
||||
Enum::Var { element1, element2 } => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
24
tests/target/width-heuristics.rs
Normal file
24
tests/target/width-heuristics.rs
Normal file
@ -0,0 +1,24 @@
|
||||
// rustfmt-max_width: 120
|
||||
|
||||
// elems on multiple lines for max_width 100, but same line for max_width 120
|
||||
fn foo(e: Enum) {
|
||||
match e {
|
||||
Enum::Var { elem1, elem2, elem3 } => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// elems not on same line for either max_width 100 or 120
|
||||
fn bar(e: Enum) {
|
||||
match e {
|
||||
Enum::Var {
|
||||
elem1,
|
||||
elem2,
|
||||
elem3,
|
||||
elem4,
|
||||
} => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user