diff --git a/src/chains.rs b/src/chains.rs index 4f395df0487..e5d2a43840b 100644 --- a/src/chains.rs +++ b/src/chains.rs @@ -34,8 +34,11 @@ pub fn rewrite_chain(orig_expr: &ast::Expr, } let parent_rewrite = try_opt!(expr.rewrite(context, width, offset)); + let total_width = rewrites.iter().fold(0, |a, b| a + b.len()) + parent_rewrite.len(); + let fits_single_line = total_width <= width && rewrites.iter().all(|s| !s.contains('\n')); - if rewrites.len() == 1 { + if rewrites.len() == 1 && !fits_single_line && + (is_continuable(expr) || parent_rewrite.len() <= context.config.tab_spaces) { let extra_offset = extra_offset(&parent_rewrite, offset); let offset = offset + extra_offset; let max_width = try_opt!(width.checked_sub(extra_offset)); @@ -47,9 +50,7 @@ pub fn rewrite_chain(orig_expr: &ast::Expr, return Some(format!("{}{}", parent_rewrite, try_opt!(rerewrite))); } - let total_width = rewrites.iter().fold(0, |a, b| a + b.len()) + parent_rewrite.len(); - - let connector = if total_width <= width && rewrites.iter().all(|s| !s.contains('\n')) { + let connector = if fits_single_line { String::new() } else { format!("\n{}", make_indent(indent)) diff --git a/src/comment.rs b/src/comment.rs index 313510f02b7..290fc62e15a 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -145,18 +145,6 @@ pub fn find_comment_end(s: &str) -> Option { } } -#[test] -fn comment_end() { - assert_eq!(Some(6), find_comment_end("// hi\n")); - assert_eq!(Some(9), find_comment_end("/* sup */ ")); - assert_eq!(Some(9), find_comment_end("/*/**/ */ ")); - assert_eq!(Some(6), find_comment_end("/*/ */ weird!")); - assert_eq!(None, find_comment_end("/* hi /* test */")); - assert_eq!(None, find_comment_end("// hi /* test */")); - assert_eq!(Some(9), find_comment_end("// hi /*\n.")); -} - - /// Returns true if text contains any comment. pub fn contains_comment(text: &str) -> bool { CharClasses::new(text.chars()).any(|(kind, _)| kind == CodeCharKind::Comment) @@ -173,21 +161,6 @@ pub fn uncommented(text: &str) -> String { .collect() } -#[test] -fn test_uncommented() { - assert_eq!(&uncommented("abc/*...*/"), "abc"); - assert_eq!(&uncommented("// .... /* \n../* /* *** / */ */a/* // */c\n"), "..ac\n"); - assert_eq!(&uncommented("abc \" /* */\" qsdf"), "abc \" /* */\" qsdf"); -} - -#[test] -fn test_contains_comment() { - assert_eq!(contains_comment("abc"), false); - assert_eq!(contains_comment("abc // qsdf"), true); - assert_eq!(contains_comment("abc /* kqsdf"), true); - assert_eq!(contains_comment("abc \" /* */\" qsdf"), false); -} - struct CharClasses where T: Iterator, T::Item: RichChar diff --git a/tests/source/chains.rs b/tests/source/chains.rs new file mode 100644 index 00000000000..7e4bda0671e --- /dev/null +++ b/tests/source/chains.rs @@ -0,0 +1,33 @@ +// Test chain formatting. + +fn main() { + let a = b.c + .d + .1 + .foo(|x| x + 1); + + bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccc + .ddddddddddddddddddddddddddd(); + + bbbbbbbbbbbbbbbbbbb.ccccccccccccccccccccccccccccccccccccc.ddddddddddddddddddddddddddd.eeeeeeee(); + + x() + .y(|| match cond() { true => (), false => () }); + + loong_func() + .quux(move || if true { + 1 + } else { + 2 + }); + + let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum = xxxxxxx + .map(|x| x + 5) + .map(|x| x / 2) + .fold(0, |acc, x| acc + x); + + aaaaaaaaaaaaaaaa.map(|x| { + x += 1; + x + }).filter(some_mod::some_filter) +} diff --git a/tests/system.rs b/tests/system.rs index 9cfe9101986..fb877993daf 100644 --- a/tests/system.rs +++ b/tests/system.rs @@ -121,7 +121,8 @@ pub fn idempotent_check(filename: String) -> Result<(), HashMap (), + false => (), + } + }); + + loong_func() + .quux(move || { + if true { + 1 + } else { + 2 + } + }); + + let suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuum = xxxxxxx.map(|x| x + 5) + .map(|x| x / 2) + .fold(0, |acc, x| acc + x); + + aaaaaaaaaaaaaaaa + .map(|x| { + x += 1; + x + }) + .filter(some_mod::some_filter) +}