Skip to content

Commit 81ad114

Browse files
authored
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 ce9ded5 commit 81ad114

File tree

5 files changed

+50
-50
lines changed

5 files changed

+50
-50
lines changed

src/formatting/items.rs

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::formatting::{
1818
FindUncommented,
1919
},
2020
expr::{
21-
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_expr,
22-
rewrite_assign_rhs_with, rewrite_assign_rhs_with_comments, RhsTactics,
21+
is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
22+
rewrite_assign_rhs_with_comments, RhsTactics,
2323
},
2424
lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator},
2525
macros::{rewrite_macro, MacroPosition},
@@ -121,61 +121,50 @@ impl Rewrite for ast::Local {
121121
mk_sp(self.pat.span.hi(), self.span.hi())
122122
};
123123

124-
if let Some(offset) = context.snippet(base_span).find_uncommented("=") {
125-
let base_span_lo = base_span.lo();
124+
let offset = context.snippet(base_span).find_uncommented("=")?;
125+
let base_span_lo = base_span.lo();
126126

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

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

160-
if !comment_after_assign.is_empty() {
161-
let new_indent_str =
162-
&shape.block_indent(0).to_string_with_newline(context.config);
163-
result.push_str(new_indent_str);
164-
result.push_str(comment_after_assign);
165-
result.push_str(new_indent_str);
166-
}
150+
if !comment_before_assign.is_empty() {
151+
let new_indent_str = &pat_shape
152+
.block_indent(0)
153+
.to_string_with_newline(context.config);
154+
result = format!("{}{}{}", comment_before_assign, new_indent_str, result);
167155
}
168156

169157
// 1 = trailing semicolon;
170158
let nested_shape = shape.sub_width(1)?;
171-
let rhs = rewrite_assign_rhs_expr(
159+
result = rewrite_assign_rhs_with_comments(
172160
context,
173161
&result,
174162
&**ex,
175163
nested_shape,
176164
RhsTactics::Default,
165+
mk_sp(assign_hi, comment_end_pos),
166+
true,
177167
)?;
178-
result = result + &rhs;
179168
}
180169

181170
result.push(';');

tests/source/issue-3851.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ fn main() {
1919
if true {
2020
1.0
2121
};
22-
}
22+
}
23+

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)