Merge pull request #1808 from topecongiro/issue-831
Allow block-like rhs expression to stay on the same line with lhs
This commit is contained in:
commit
b559a0f2f2
36
src/expr.rs
36
src/expr.rs
@ -377,9 +377,9 @@ where
|
||||
// Note that this is non-conservative, but its just to see if it's even
|
||||
// worth trying to put everything on one line.
|
||||
let rhs_shape = try_opt!(shape.sub_width(suffix.len()));
|
||||
let rhs_result = rhs.rewrite(context, rhs_shape);
|
||||
let rhs_orig_result = rhs.rewrite(context, rhs_shape);
|
||||
|
||||
if let Some(rhs_result) = rhs_result {
|
||||
if let Some(ref rhs_result) = rhs_orig_result {
|
||||
// This is needed in case of line break not caused by a
|
||||
// shortage of space, but by end-of-line comments, for example.
|
||||
if !rhs_result.contains('\n') {
|
||||
@ -419,6 +419,7 @@ where
|
||||
// We have to use multiple lines.
|
||||
|
||||
// Re-evaluate the rhs because we have more space now:
|
||||
let sep = if infix.ends_with(' ') { " " } else { "" };
|
||||
let infix = infix.trim_right();
|
||||
let rhs_shape = match context.config.control_style() {
|
||||
Style::Legacy => {
|
||||
@ -439,12 +440,24 @@ where
|
||||
width: try_opt!(context.config.max_width().checked_sub(lhs_overhead)),
|
||||
..shape
|
||||
};
|
||||
let lhs_result = try_opt!(lhs.rewrite(context, lhs_shape));
|
||||
let lhs_result = try_opt!(
|
||||
lhs.rewrite(context, lhs_shape)
|
||||
.map(|lhs_str| format!("{}{}{}", prefix, lhs_str, infix))
|
||||
);
|
||||
if let Some(ref rhs_str) = rhs_orig_result {
|
||||
if rhs_str.lines().count() <= rhs_result.lines().count() &&
|
||||
rhs_str
|
||||
.lines()
|
||||
.next()
|
||||
.map_or(false, |first_line| first_line.ends_with('{')) &&
|
||||
last_line_width(&lhs_result) + sep.len() + first_line_width(rhs_str) <= shape.width
|
||||
{
|
||||
return Some(format!("{}{}{}{}", lhs_result, sep, rhs_str, suffix));
|
||||
}
|
||||
}
|
||||
Some(format!(
|
||||
"{}{}{}\n{}{}{}",
|
||||
prefix,
|
||||
"{}\n{}{}{}",
|
||||
lhs_result,
|
||||
infix,
|
||||
rhs_shape.indent.to_string(context.config),
|
||||
rhs_result,
|
||||
suffix
|
||||
@ -2886,12 +2899,11 @@ pub fn rewrite_assign_rhs<S: Into<String>>(
|
||||
shape: Shape,
|
||||
) -> Option<String> {
|
||||
let lhs = lhs.into();
|
||||
let last_line_width = last_line_width(&lhs) -
|
||||
if lhs.contains('\n') {
|
||||
shape.indent.width()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let last_line_width = last_line_width(&lhs) - if lhs.contains('\n') {
|
||||
shape.indent.width()
|
||||
} else {
|
||||
0
|
||||
};
|
||||
// 1 = space between operator and rhs.
|
||||
let orig_shape = try_opt!(shape.offset_left(last_line_width + 1));
|
||||
let rhs = try_opt!(choose_rhs(
|
||||
|
11
src/items.rs
11
src/items.rs
@ -829,12 +829,11 @@ fn rewrite_trait_ref(
|
||||
result_len: usize,
|
||||
) -> Option<String> {
|
||||
// 1 = space between generics and trait_ref
|
||||
let used_space = 1 + polarity_str.len() +
|
||||
if generics_str.contains('\n') {
|
||||
last_line_width(&generics_str)
|
||||
} else {
|
||||
result_len + generics_str.len()
|
||||
};
|
||||
let used_space = 1 + polarity_str.len() + if generics_str.contains('\n') {
|
||||
last_line_width(&generics_str)
|
||||
} else {
|
||||
result_len + generics_str.len()
|
||||
};
|
||||
let shape = Shape::indented(offset + used_space, context.config);
|
||||
if let Some(trait_ref_str) = trait_ref.rewrite(context, shape) {
|
||||
if !(retry && trait_ref_str.contains('\n')) {
|
||||
|
@ -638,14 +638,12 @@ impl<'a> FmtVisitor<'a> {
|
||||
let use_item_length = items_left
|
||||
.iter()
|
||||
.take_while(|ppi| {
|
||||
is_use_item(&***ppi) &&
|
||||
(!reorder_imports_in_group ||
|
||||
{
|
||||
let current = self.codemap.lookup_line_range(item_bound(&ppi));
|
||||
let in_same_group = current.lo < last.hi + 2;
|
||||
last = current;
|
||||
in_same_group
|
||||
})
|
||||
is_use_item(&***ppi) && (!reorder_imports_in_group || {
|
||||
let current = self.codemap.lookup_line_range(item_bound(&ppi));
|
||||
let in_same_group = current.lo < last.hi + 2;
|
||||
last = current;
|
||||
in_same_group
|
||||
})
|
||||
})
|
||||
.count();
|
||||
let (use_items, rest) = items_left.split_at(use_item_length);
|
||||
|
@ -118,12 +118,11 @@ fn floaters() {
|
||||
})
|
||||
.quux();
|
||||
|
||||
a +
|
||||
match x {
|
||||
true => "yay!",
|
||||
false => "boo!",
|
||||
}
|
||||
.bar()
|
||||
a + match x {
|
||||
true => "yay!",
|
||||
false => "boo!",
|
||||
}
|
||||
.bar()
|
||||
}
|
||||
|
||||
fn is_replaced_content() -> bool {
|
||||
|
@ -114,11 +114,10 @@ fn floaters() {
|
||||
})
|
||||
.quux();
|
||||
|
||||
a +
|
||||
match x {
|
||||
true => "yay!",
|
||||
false => "boo!",
|
||||
}.bar()
|
||||
a + match x {
|
||||
true => "yay!",
|
||||
false => "boo!",
|
||||
}.bar()
|
||||
}
|
||||
|
||||
fn is_replaced_content() -> bool {
|
||||
|
9
tests/target/issue-831.rs
Normal file
9
tests/target/issue-831.rs
Normal file
@ -0,0 +1,9 @@
|
||||
fn main() {
|
||||
let y = a.iter().any(|x| {
|
||||
println!("a");
|
||||
}) || b.iter().any(|x| {
|
||||
println!("b");
|
||||
}) || c.iter().any(|x| {
|
||||
println!("c");
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user