Merge pull request #2667 from tspiteri/width-heuristics

Do not scale WidthHeuristics when max_width less than 100
This commit is contained in:
Nick Cameron 2018-05-04 11:46:28 +12:00 committed by GitHub
commit 4a57e79469
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 81 additions and 4 deletions

View File

@ -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(),
&_ => (),
}

View File

@ -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,

View File

@ -0,0 +1,11 @@
// rustfmt-max_width: 80
fn foo(e: Enum) {
match e {
Enum::Var {
element1,
element2,
} => {
return;
}
}
}

View 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;
}
}
}

View File

@ -0,0 +1,8 @@
// rustfmt-max_width: 80
fn foo(e: Enum) {
match e {
Enum::Var { element1, element2 } => {
return;
}
}
}

View 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;
}
}
}