Merge pull request #3109 from scampi/issue-3038

force a newline after the `if` condition if there is a different indentation level
This commit is contained in:
Nick Cameron 2018-10-18 19:56:17 +13:00 committed by GitHub
commit e633f2b3f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 93 additions and 2 deletions

View File

@ -801,6 +801,20 @@ impl<'a> ControlFlow<'a> {
}
}
/// Returns true if the last line of pat_str has leading whitespace and it is wider than the
/// shape's indent.
fn last_line_offsetted(start_column: usize, pat_str: &str) -> bool {
let mut leading_whitespaces = 0;
for c in pat_str.chars().rev() {
match c {
'\n' => break,
_ if c.is_whitespace() => leading_whitespaces += 1,
_ => leading_whitespaces = 0,
}
}
leading_whitespaces > start_column
}
impl<'a> ControlFlow<'a> {
fn rewrite_pat_expr(
&self,
@ -885,7 +899,8 @@ impl<'a> ControlFlow<'a> {
.saturating_sub(constr_shape.used_width() + offset + brace_overhead);
let force_newline_brace = (pat_expr_string.contains('\n')
|| pat_expr_string.len() > one_line_budget)
&& !last_line_extendable(&pat_expr_string);
&& (!last_line_extendable(&pat_expr_string)
|| last_line_offsetted(shape.used_width(), &pat_expr_string));
// Try to format if-else on single line.
if self.allow_single_line
@ -1977,3 +1992,29 @@ pub fn is_method_call(expr: &ast::Expr) -> bool {
_ => false,
}
}
#[cfg(test)]
mod test {
use super::last_line_offsetted;
#[test]
fn test_last_line_offsetted() {
let lines = "one\n two";
assert_eq!(last_line_offsetted(2, lines), true);
assert_eq!(last_line_offsetted(4, lines), false);
assert_eq!(last_line_offsetted(6, lines), false);
let lines = "one two";
assert_eq!(last_line_offsetted(2, lines), false);
assert_eq!(last_line_offsetted(0, lines), false);
let lines = "\ntwo";
assert_eq!(last_line_offsetted(2, lines), false);
assert_eq!(last_line_offsetted(0, lines), false);
let lines = "one\n two three";
assert_eq!(last_line_offsetted(2, lines), true);
let lines = "one\n two three";
assert_eq!(last_line_offsetted(2, lines), false);
}
}

View File

@ -0,0 +1,20 @@
impl HTMLTableElement {
fn func() {
if number_of_row_elements == 0 {
if let Some(last_tbody) = node.rev_children()
.filter_map(DomRoot::downcast::<Element>)
.find(|n| n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")) {
last_tbody.upcast::<Node>().AppendChild(new_row.upcast::<Node>())
.expect("InsertRow failed to append first row.");
}
}
if number_of_row_elements == 0 {
if let Some(last_tbody) = node
.find(|n| n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")) {
last_tbody.upcast::<Node>().AppendChild(new_row.upcast::<Node>())
.expect("InsertRow failed to append first row.");
}
}
}
}

View File

@ -27,7 +27,8 @@ fn foo() {
.map(String::as_ref)
.unwrap_or("")
.is_empty()
}) {
})
{
do_something();
}
}

View File

@ -0,0 +1,29 @@
impl HTMLTableElement {
fn func() {
if number_of_row_elements == 0 {
if let Some(last_tbody) = node
.rev_children()
.filter_map(DomRoot::downcast::<Element>)
.find(|n| {
n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")
})
{
last_tbody
.upcast::<Node>()
.AppendChild(new_row.upcast::<Node>())
.expect("InsertRow failed to append first row.");
}
}
if number_of_row_elements == 0 {
if let Some(last_tbody) = node.find(|n| {
n.is::<HTMLTableSectionElement>() && n.local_name() == &local_name!("tbody")
}) {
last_tbody
.upcast::<Node>()
.AppendChild(new_row.upcast::<Node>())
.expect("InsertRow failed to append first row.");
}
}
}
}