From 0baeca5829b745f4fdd3f29a24e6413b8421ce02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Cassiers?= Date: Sun, 24 May 2015 19:57:13 +0200 Subject: [PATCH 1/4] Add rewrite for ExprParen --- src/expr.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/expr.rs b/src/expr.rs index c1cbf1756457..66a79a3a777c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -125,6 +125,23 @@ fn rewrite_call(&mut self, format!("{}({})", callee_str, args_str) } + fn rewrite_paren(&mut self, subexpr: &ast::Expr, width: usize, offset: usize) -> String { + debug!("rewrite_paren, width: {}, offset: {}", width, offset); + // 1 is for opening paren + let subexpr_str = self.rewrite_expr(subexpr, width-1, offset+1); + debug!("rewrite_paren, subexpr_str: `{}`", subexpr_str); + let mut lines = subexpr_str.rsplit('\n'); + let last_line_len = lines.next().unwrap().len(); + let last_line_offset = if lines.next().is_none() {offset+1} else {0}; + if width + offset - last_line_offset - last_line_len > 0 { + format!("({})", subexpr_str) + } else { + // FIXME That's correct unless we have width < 2. Return an Optrion for such cases ? + format!("({}\n{} )", subexpr_str, make_indent(offset)) + } + } + + pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String { match expr.node { ast::Expr_::ExprLit(ref l) => { @@ -140,6 +157,9 @@ pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> ast::Expr_::ExprCall(ref callee, ref args) => { return self.rewrite_call(callee, args, width, offset); } + ast::Expr_::ExprParen(ref subexpr) => { + return self.rewrite_paren(subexpr, width, offset); + } _ => {} } From 1db6fa0fe5d8ea9ddef2d5610c2485d9ec3a3cc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Cassiers?= Date: Sun, 24 May 2015 22:07:18 +0200 Subject: [PATCH 2/4] Add idem test for paren --- tests/idem/paren.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 tests/idem/paren.rs diff --git a/tests/idem/paren.rs b/tests/idem/paren.rs new file mode 100644 index 000000000000..f04068e3462e --- /dev/null +++ b/tests/idem/paren.rs @@ -0,0 +1,16 @@ +// Test parenthesis + +fn foo() { + let very_long_variable_name = (a + first + simple + test); + let very_long_variable_name = (write + something + here + to + fill + the + line + 12 + 34 + 567 + ); + let very_long_variable_name = (write + something + here + to + fill + the + line + 12 + 34 + 567 + + 78); + let very_long_variable_name = (write + something + here + to + fill + the + line + 12 + 34 + 567 + + 78 + fill + another + line + AAAA + BBBBBBB + CCCCCCCCCCCCCCCCC + ); + let very_long_variable_name = (write + something + here + to + fill + the + line + 12 + 34 + 567 + + 78 + fill + another + line + AAAA + BBBBBBB + CCCCCCCCCCCCCCC + + DDDDDDD + EEEEEE); + +} From c1fc693c5e5676f93a331a0498dc6ba36193cd66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Cassiers?= Date: Sun, 24 May 2015 23:35:40 +0200 Subject: [PATCH 3/4] syle correction --- src/expr.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index 66a79a3a777c..c242322a51d0 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -132,11 +132,14 @@ fn rewrite_paren(&mut self, subexpr: &ast::Expr, width: usize, offset: usize) -> debug!("rewrite_paren, subexpr_str: `{}`", subexpr_str); let mut lines = subexpr_str.rsplit('\n'); let last_line_len = lines.next().unwrap().len(); - let last_line_offset = if lines.next().is_none() {offset+1} else {0}; + let last_line_offset = match lines.next() { + None => offset+1, + Some(_) => 0, + }; if width + offset - last_line_offset - last_line_len > 0 { format!("({})", subexpr_str) } else { - // FIXME That's correct unless we have width < 2. Return an Optrion for such cases ? + // FIXME That's correct unless we have width < 2. Return an Option for such cases ? format!("({}\n{} )", subexpr_str, make_indent(offset)) } } From 09bd4a74e4dfa64fd1223662eef71bf6bd9c2f44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Cassiers?= Date: Sun, 24 May 2015 23:46:02 +0200 Subject: [PATCH 4/4] Avoid dangling ) --- src/expr.rs | 19 ++++--------------- tests/idem/paren.rs | 13 ++----------- 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/src/expr.rs b/src/expr.rs index c242322a51d0..79234eb7f137 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -127,24 +127,13 @@ fn rewrite_call(&mut self, fn rewrite_paren(&mut self, subexpr: &ast::Expr, width: usize, offset: usize) -> String { debug!("rewrite_paren, width: {}, offset: {}", width, offset); - // 1 is for opening paren - let subexpr_str = self.rewrite_expr(subexpr, width-1, offset+1); + // 1 is for opening paren, 2 is for opening+closing, we want to keep the closing + // paren on the same line as the subexpr + let subexpr_str = self.rewrite_expr(subexpr, width-2, offset+1); debug!("rewrite_paren, subexpr_str: `{}`", subexpr_str); - let mut lines = subexpr_str.rsplit('\n'); - let last_line_len = lines.next().unwrap().len(); - let last_line_offset = match lines.next() { - None => offset+1, - Some(_) => 0, - }; - if width + offset - last_line_offset - last_line_len > 0 { - format!("({})", subexpr_str) - } else { - // FIXME That's correct unless we have width < 2. Return an Option for such cases ? - format!("({}\n{} )", subexpr_str, make_indent(offset)) - } + format!("({})", subexpr_str) } - pub fn rewrite_expr(&mut self, expr: &ast::Expr, width: usize, offset: usize) -> String { match expr.node { ast::Expr_::ExprLit(ref l) => { diff --git a/tests/idem/paren.rs b/tests/idem/paren.rs index f04068e3462e..981698835758 100644 --- a/tests/idem/paren.rs +++ b/tests/idem/paren.rs @@ -2,15 +2,6 @@ fn foo() { let very_long_variable_name = (a + first + simple + test); - let very_long_variable_name = (write + something + here + to + fill + the + line + 12 + 34 + 567 - ); - let very_long_variable_name = (write + something + here + to + fill + the + line + 12 + 34 + 567 - + 78); - let very_long_variable_name = (write + something + here + to + fill + the + line + 12 + 34 + 567 - + 78 + fill + another + line + AAAA + BBBBBBB + CCCCCCCCCCCCCCCCC - ); - let very_long_variable_name = (write + something + here + to + fill + the + line + 12 + 34 + 567 - + 78 + fill + another + line + AAAA + BBBBBBB + CCCCCCCCCCCCCCC + - DDDDDDD + EEEEEE); - + let very_long_variable_name = (a + first + simple + test + AAAAAAAAAAAAA + BBBBBBBBBBBBBBBBBB + + b + c); }