Skip to content

Commit d5a38ba

Browse files
davidBar-Onytmimi
authored andcommitted
Backport: Assignment indentation after one line comment (#4550)
* Fix #4502 issue - assignment indentation after one line comment * Enhancement by using rewrite_assign_rhs_with_comments() * Changes per comments received
1 parent b558a74 commit d5a38ba

File tree

4 files changed

+48
-49
lines changed

4 files changed

+48
-49
lines changed

src/items.rs

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -119,62 +119,51 @@ impl Rewrite for ast::Local {
119119
mk_sp(self.pat.span.hi(), self.span.hi())
120120
};
121121

122-
if let Some(offset) = context.snippet(base_span).find_uncommented("=") {
123-
let base_span_lo = base_span.lo();
122+
let offset = context.snippet(base_span).find_uncommented("=")?;
123+
let base_span_lo = base_span.lo();
124124

125-
let assign_lo = base_span_lo + BytePos(offset as u32);
126-
let comment_start_pos = if let Some(ref ty) = self.ty {
127-
ty.span.hi()
128-
} else {
129-
self.pat.span.hi()
130-
};
131-
let comment_before_assign =
132-
context.snippet(mk_sp(comment_start_pos, assign_lo)).trim();
125+
let assign_lo = base_span_lo + BytePos(offset as u32);
126+
let comment_start_pos = if let Some(ref ty) = self.ty {
127+
ty.span.hi()
128+
} else {
129+
self.pat.span.hi()
130+
};
131+
let comment_before_assign = context.snippet(mk_sp(comment_start_pos, assign_lo)).trim();
133132

134-
let assign_hi = base_span_lo + BytePos((offset + 1) as u32);
135-
let rhs_span_lo = init.span.lo();
136-
let comment_end_pos = if init.attrs.is_empty() {
133+
let assign_hi = base_span_lo + BytePos((offset + 1) as u32);
134+
let rhs_span_lo = init.span.lo();
135+
let comment_end_pos = if init.attrs.is_empty() {
136+
rhs_span_lo
137+
} else {
138+
let attr_span_lo = init.attrs.first().unwrap().span.lo();
139+
// for the case using block
140+
// ex. let x = { #![my_attr]do_something(); }
141+
if rhs_span_lo < attr_span_lo {
137142
rhs_span_lo
138143
} else {
139-
let attr_span_lo = init.attrs.first().unwrap().span.lo();
140-
// for the case using block
141-
// ex. let x = { #![my_attr]do_something(); }
142-
if rhs_span_lo < attr_span_lo {
143-
rhs_span_lo
144-
} else {
145-
attr_span_lo
146-
}
147-
};
148-
let comment_after_assign =
149-
context.snippet(mk_sp(assign_hi, comment_end_pos)).trim();
150-
151-
if !comment_before_assign.is_empty() {
152-
let new_indent_str = &pat_shape
153-
.block_indent(0)
154-
.to_string_with_newline(context.config);
155-
result = format!("{}{}{}", comment_before_assign, new_indent_str, result);
144+
attr_span_lo
156145
}
146+
};
157147

158-
if !comment_after_assign.is_empty() {
159-
let new_indent_str =
160-
&shape.block_indent(0).to_string_with_newline(context.config);
161-
result.push_str(new_indent_str);
162-
result.push_str(comment_after_assign);
163-
result.push_str(new_indent_str);
164-
}
148+
if !comment_before_assign.is_empty() {
149+
let new_indent_str = &pat_shape
150+
.block_indent(0)
151+
.to_string_with_newline(context.config);
152+
result = format!("{}{}{}", comment_before_assign, new_indent_str, result);
165153
}
166154

167155
// 1 = trailing semicolon;
168156
let nested_shape = shape.sub_width(1)?;
169-
170-
result = rewrite_assign_rhs(
157+
result = rewrite_assign_rhs_with_comments(
171158
context,
172159
result,
173160
init,
174-
&RhsAssignKind::Expr(&init.kind, init.span),
175161
nested_shape,
162+
&RhsAssignKind::Expr(&init.kind, init.span),
163+
RhsTactics::Default,
164+
mk_sp(assign_hi, comment_end_pos),
165+
true,
176166
)?;
177-
// todo else
178167
}
179168

180169
result.push(';');

tests/source/issue-4502.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let after =
3+
// after_comment
4+
1.0;
5+
}

tests/target/issue-3851.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ fn main() {
55
};
66

77
let after =
8-
// after_comment
9-
if true {
10-
1.0
11-
};
8+
// after_comment
9+
if true {
10+
1.0
11+
};
1212

1313
// before_comment
1414
let both =
15-
// after_comment
16-
if true {
17-
1.0
18-
};
15+
// after_comment
16+
if true {
17+
1.0
18+
};
1919
}

tests/target/issue-4502.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let after =
3+
// after_comment
4+
1.0;
5+
}

0 commit comments

Comments
 (0)