Skip to content

Commit 87e9602

Browse files
davidBar-Oncalebcartwright
authored andcommitted
Enhancements per comments
1 parent ddd3b46 commit 87e9602

File tree

3 files changed

+82
-37
lines changed

3 files changed

+82
-37
lines changed

src/formatting/expr.rs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ pub(crate) fn format_expr(
194194
ast::ExprKind::Path(ref qself, ref path) => {
195195
rewrite_path(context, PathContext::Expr, qself.as_ref(), path, shape)
196196
}
197-
ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
198-
rewrite_assignment(context, lhs, rhs, None, shape)
197+
ast::ExprKind::Assign(ref lhs, ref rhs, op_span) => {
198+
rewrite_assignment(context, lhs, rhs, op_span, shape)
199199
}
200200
ast::ExprKind::AssignOp(ref op, ref lhs, ref rhs) => {
201-
rewrite_assignment(context, lhs, rhs, Some(op), shape)
201+
rewrite_assignment(context, lhs, rhs, op.span, shape)
202202
}
203203
ast::ExprKind::Continue(ref opt_label) => {
204204
let id_str = match *opt_label {
@@ -1895,25 +1895,24 @@ fn rewrite_assignment(
18951895
context: &RewriteContext<'_>,
18961896
lhs: &ast::Expr,
18971897
rhs: &ast::Expr,
1898-
op: Option<&ast::BinOp>,
1898+
op_span: Span,
18991899
shape: Shape,
19001900
) -> Option<String> {
1901-
let span_hi = rhs.span.lo();
1902-
let (operator_str, span_lo) = match op {
1903-
Some(op) => (context.snippet(op.span), op.span.hi()),
1904-
None => {
1905-
let lo = lhs.span.hi();
1906-
let offset = context.snippet(mk_sp(lo, span_hi)).find_uncommented("=")? + 1;
1907-
("=", lo + BytePos(offset as u32))
1908-
}
1909-
};
1910-
19111901
// 1 = space between lhs and operator.
1902+
let operator_str = context.snippet(op_span);
19121903
let lhs_shape = shape.sub_width(operator_str.len() + 1)?;
19131904
let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str);
19141905

1915-
let between_span = mk_sp(span_lo, span_hi);
1916-
rewrite_assign_rhs_with_span(context, lhs_str, rhs, shape, between_span)
1906+
let comments_span = mk_sp(op_span.hi(), rhs.span.lo());
1907+
rewrite_assign_rhs_with_comments(
1908+
context,
1909+
lhs_str,
1910+
rhs,
1911+
shape,
1912+
RhsTactics::Default,
1913+
comments_span,
1914+
true,
1915+
)
19171916
}
19181917

19191918
/// Controls where to put the rhs.
@@ -1939,26 +1938,6 @@ pub(crate) fn rewrite_assign_rhs<S: Into<String>, R: Rewrite>(
19391938
rewrite_assign_rhs_with(context, lhs, ex, shape, RhsTactics::Default)
19401939
}
19411940

1942-
// The left hand side must contain everything up to, and including, the
1943-
// assignment operator.
1944-
pub(crate) fn rewrite_assign_rhs_with_span<S: Into<String>, R: Rewrite>(
1945-
context: &RewriteContext<'_>,
1946-
lhs: S,
1947-
ex: &R,
1948-
shape: Shape,
1949-
between_span: Span,
1950-
) -> Option<String> {
1951-
rewrite_assign_rhs_with_comments(
1952-
context,
1953-
lhs,
1954-
ex,
1955-
shape,
1956-
RhsTactics::Default,
1957-
between_span,
1958-
true,
1959-
)
1960-
}
1961-
19621941
pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
19631942
context: &RewriteContext<'_>,
19641943
lhs: &str,

tests/source/lhs-to-rhs-between-comments/assignment.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ fn main () {
2121
let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first;
2222
var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second;
2323
}
24+
fn main () {
25+
let var = /* Block comment line 1
26+
* Block comment line 2
27+
* Block comment line 3 */ first;
28+
var = /* Block comment line 1
29+
* Block comment line 2
30+
* Block comment line 3 */ second;
31+
var = /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
32+
* Block comment line 2
33+
* Block comment line 3 */ third;
34+
var = /* Block comment line 1
35+
* Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
36+
* Block comment line 3 */ forth;
37+
}
2438

2539
// BinOp Assigment
2640
fn main () {
@@ -45,4 +59,18 @@ fn main () {
4559
let var = /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ first;
4660
var /= /* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */ second;
4761
}
48-
62+
fn main () {
63+
let var = /* Block comment line 1
64+
* Block comment line 2
65+
* Block comment line 3 */ first;
66+
var += /* Block comment line 1
67+
* Block comment line 2
68+
* Block comment line 3 */ second;
69+
var -= /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
70+
* Block comment line 2
71+
* Block comment line 3 */ third;
72+
var *= /* Block comment line 1
73+
* Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
74+
* Block comment line 3 */ forth;
75+
}
76+

tests/target/lhs-to-rhs-between-comments/assignment.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ fn main() {
2727
/* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */
2828
second;
2929
}
30+
fn main() {
31+
let var = /* Block comment line 1
32+
* Block comment line 2
33+
* Block comment line 3 */
34+
first;
35+
var = /* Block comment line 1
36+
* Block comment line 2
37+
* Block comment line 3 */
38+
second;
39+
var =
40+
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
41+
* Block comment line 2
42+
* Block comment line 3 */
43+
third;
44+
var = /* Block comment line 1
45+
* Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
46+
* Block comment line 3 */
47+
forth;
48+
}
3049

3150
// BinOp Assigment
3251
fn main() {
@@ -57,3 +76,22 @@ fn main() {
5776
/* Block comment longgggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg */
5877
second;
5978
}
79+
fn main() {
80+
let var = /* Block comment line 1
81+
* Block comment line 2
82+
* Block comment line 3 */
83+
first;
84+
var += /* Block comment line 1
85+
* Block comment line 2
86+
* Block comment line 3 */
87+
second;
88+
var -=
89+
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
90+
* Block comment line 2
91+
* Block comment line 3 */
92+
third;
93+
var *= /* Block comment line 1
94+
* Block comment line 2 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
95+
* Block comment line 3 */
96+
forth;
97+
}

0 commit comments

Comments
 (0)