Merge pull request #1487 from topecongiro/issue1470

Use block indent when visual indent exceeds max_width
This commit is contained in:
Nick Cameron 2017-05-02 10:45:23 +12:00 committed by GitHub
commit 7fb288f0b3
3 changed files with 63 additions and 3 deletions

View File

@ -2103,13 +2103,15 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
let new_offset = shape.indent.block_indent(context.config);
let max_width = try_opt!((shape.width + shape.indent.width())
.checked_sub(new_offset.width()));
let new_rhs = ex.rewrite(context, Shape::legacy(max_width, new_offset));
let new_shape = Shape::legacy(max_width, new_offset);
let new_rhs = ex.rewrite(context, new_shape);
// FIXME: DRY!
match (rhs, new_rhs) {
(Some(ref orig_rhs), Some(ref replacement_rhs))
if count_line_breaks(orig_rhs) >
count_line_breaks(replacement_rhs) + 1 => {
if count_line_breaks(orig_rhs) > count_line_breaks(replacement_rhs) + 1 ||
(orig_rhs.rewrite(context, shape).is_none() &&
replacement_rhs.rewrite(context, new_shape).is_some()) => {
result.push_str(&format!("\n{}", new_offset.to_string(context.config)));
result.push_str(replacement_rhs);
}

29
tests/source/large_vec.rs Normal file
View File

@ -0,0 +1,29 @@
// See #1470.
impl Environment {
pub fn new_root() -> Rc<RefCell<Environment>> {
let mut env = Environment::new();
let builtin_functions = &[("println",
Function::NativeVoid(CallSign {
num_params: 0,
variadic: true,
param_types: vec![],
},
native_println)),
("run_http_server",
Function::NativeVoid(CallSign {
num_params: 1,
variadic: false,
param_types:
vec![Some(ConstraintType::Function)],
},
native_run_http_server)),
("len",
Function::NativeReturning(CallSign {
num_params: 1,
variadic: false,
param_types: vec![None],
},
native_len))];
}
}

29
tests/target/large_vec.rs Normal file
View File

@ -0,0 +1,29 @@
// See #1470.
impl Environment {
pub fn new_root() -> Rc<RefCell<Environment>> {
let mut env = Environment::new();
let builtin_functions =
&[("println",
Function::NativeVoid(CallSign {
num_params: 0,
variadic: true,
param_types: vec![],
},
native_println)),
("run_http_server",
Function::NativeVoid(CallSign {
num_params: 1,
variadic: false,
param_types: vec![Some(ConstraintType::Function)],
},
native_run_http_server)),
("len",
Function::NativeReturning(CallSign {
num_params: 1,
variadic: false,
param_types: vec![None],
},
native_len))];
}
}