]> git.lizzy.rs Git - rust.git/commitdiff
Merge pull request #528 from marcusklaas/chain-idempotence
authorNick Cameron <nrc@ncameron.org>
Fri, 23 Oct 2015 22:14:24 +0000 (11:14 +1300)
committerNick Cameron <nrc@ncameron.org>
Fri, 23 Oct 2015 22:14:24 +0000 (11:14 +1300)
Restore idempotence for chain formatting

1  2 
src/chains.rs

diff --combined src/chains.rs
index 4716f545b3f58be2df2617731fd2a5ebe4763cf8,ebe7aacf7cc8095ca2d3dd068eb9ede0045d4706..f004a5798c2a24ec245ddecdd446a8612f1ddf92
@@@ -72,19 -72,21 +72,19 @@@ pub fn rewrite_chain(mut expr: &ast::Ex
                                              .collect::<Option<Vec<_>>>());
  
      // Total of all items excluding the last.
 -    let almost_total = rewrites.split_last()
 -                               .unwrap()
 -                               .1
 -                               .iter()
 -                               .fold(0, |a, b| a + first_line_width(b)) +
 +    let almost_total = rewrites[..rewrites.len() - 1]
 +                           .iter()
 +                           .fold(0, |a, b| a + first_line_width(b)) +
                         parent_rewrite.len();
      let total_width = almost_total + first_line_width(rewrites.last().unwrap());
      let veto_single_line = if context.config.take_source_hints && subexpr_list.len() > 1 {
          // Look at the source code. Unless all chain elements start on the same
          // line, we won't consider putting them on a single line either.
-         let first_line_no = context.codemap.lookup_char_pos(subexpr_list[0].span.lo).line;
+         let last_span = context.snippet(mk_sp(subexpr_list[1].span.hi, total_span.hi));
+         let first_span = context.snippet(subexpr_list[1].span);
+         let last_iter = last_span.chars().take_while(|c| c.is_whitespace());
  
-         subexpr_list[1..]
-             .iter()
-             .any(|ex| context.codemap.lookup_char_pos(ex.span.hi).line != first_line_no)
+         first_span.chars().chain(last_iter).any(|c| c == '\n')
      } else {
          false
      };
@@@ -93,9 -95,7 +93,9 @@@
                             match subexpr_list[0].node {
          ast::Expr_::ExprMethodCall(ref method_name, ref types, ref expressions)
              if context.config.chains_overflow_last => {
 -            let (last, init) = rewrites.split_last_mut().unwrap();
 +            let len = rewrites.len();
 +            let (init, last) = rewrites.split_at_mut(len - 1);
 +            let last = &mut last[0];
  
              if init.iter().all(|s| !s.contains('\n')) && total_width <= width {
                  let last_rewrite = width.checked_sub(almost_total)