Skip to content

Commit a544725

Browse files
committed
Changes per reviewer comments
1 parent 41989cc commit a544725

File tree

6 files changed

+54
-39
lines changed

6 files changed

+54
-39
lines changed

src/comment.rs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ pub(crate) fn is_last_comment_block(s: &str) -> bool {
160160
s.trim_end().ends_with("*/")
161161
}
162162

163-
/// Returns true if the first line of the passed string starts with a comment
164-
/// that end on that line.
165-
fn is_first_comment_ends_on_first_line(s: &str) -> bool {
163+
/// Returns true if the last comment in the first line of the passed comments block
164+
/// ends on that line.
165+
fn is_first_comment_line_ends_comment(s: &str) -> bool {
166166
if s.starts_with("/*") {
167-
s.lines().next().map_or(false, |l| l.contains("*/"))
167+
// FIXME: mixed block and line comments on the line are not be properlly formatted,
168+
// e.g. "/* Block comment /* // Line comment" at the end of the line.
169+
s.lines().next().map_or(false, |l| l.ends_with("*/"))
168170
} else if s.starts_with("//") {
169-
true
171+
true // Comment is "// comment"
170172
} else {
171173
false
172174
}
@@ -237,12 +239,11 @@ pub(crate) fn combine_strs_with_missing_comments(
237239
Cow::from("")
238240
} else {
239241
let one_line_width = last_line_width(prev_str) + first_line_width(&missing_comment) + 1;
240-
// First comment of a comments block can be in same line if ends in the first line
241-
// (and meets other conditions)
242+
// First comment in the comments block can be in same line if it ends in the first line.
242243
if prefer_same_line
243244
&& one_line_width <= shape.width
244245
&& (is_single_line(&missing_comment)
245-
|| is_first_comment_ends_on_first_line(&missing_comment))
246+
|| is_first_comment_line_ends_comment(&missing_comment))
246247
{
247248
Cow::from(" ")
248249
} else {
@@ -1019,32 +1020,32 @@ fn light_rewrite_comment(
10191020
config: &Config,
10201021
is_doc_comment: bool,
10211022
) -> String {
1022-
let lines: Vec<String> = orig
1023-
.lines()
1024-
.map(|l| {
1025-
// This is basically just l.trim(), but in the case that a line starts
1026-
// with `*` we want to leave one space before it, so it aligns with the
1027-
// `*` in `/*`.
1028-
let first_non_whitespace = l.find(|c| !char::is_whitespace(c));
1029-
let (blank, left_trimmed) = if let Some(fnw) = first_non_whitespace {
1030-
if l.as_bytes()[fnw] == b'*' {
1031-
// Ensure '*' is preceeded by blank and not by a tab.
1032-
(" ", &l[fnw..])
1033-
} else {
1034-
("", &l[fnw..])
1035-
}
1036-
} else {
1037-
("", "")
1038-
};
1039-
// Preserve markdown's double-space line break syntax in doc comment.
1040-
format!(
1041-
"{}{}",
1042-
blank,
1043-
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment),
1044-
)
1045-
})
1046-
.collect();
1047-
lines.join(&format!("\n{}", offset.to_string(config)))
1023+
let mut result = String::with_capacity(orig.len() * 2);
1024+
let mut lines = orig.lines().peekable();
1025+
1026+
// This is basically just l.trim(), but in the case that a line starts with `*` we want to leave
1027+
// one space before it, so it aligns with the `*` in `/*`.
1028+
while let Some(full_line) = lines.next() {
1029+
let line = full_line.trim_start();
1030+
1031+
if line.is_empty() {
1032+
continue;
1033+
}
1034+
1035+
if line.starts_with('*') {
1036+
result.push(' ');
1037+
}
1038+
1039+
// Preserve markdown's double-space line break syntax in doc comment.
1040+
let trimmed = trim_end_unless_two_whitespaces(line, is_doc_comment);
1041+
result.push_str(trimmed);
1042+
1043+
let is_last = lines.peek().is_none();
1044+
if !is_last {
1045+
result.push_str(&offset.to_string_with_newline(config))
1046+
}
1047+
}
1048+
result
10481049
}
10491050

10501051
/// Trims comment characters and possibly a single space from the left of a string.

src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,8 @@ fn rewrite_assignment(
18961896
op_span: Span,
18971897
shape: Shape,
18981898
) -> Option<String> {
1899-
// 1 = space between lhs and operator.
19001899
let operator_str = context.snippet(op_span);
1900+
// 1 = space between lhs and operator.
19011901
let lhs_shape = shape.sub_width(operator_str.len() + 1)?;
19021902
let lhs_str = format!("{} {}", lhs.rewrite(context, lhs_shape)?, operator_str);
19031903

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ var = /* Block comment single line */
2727
// Line comment
2828
/* Block comment line 1
2929
* Block comment line 2 */ third;
30+
var = /* Block comment single line 1 */
31+
/* Block comment single line 2*/ forth;
32+
var = /* Block comment single line 1 */
33+
/* Block comment single line 2*/
34+
fifth;
3035
var = /* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
3136
* Block comment line 2 */
3237
// Line comment
33-
forth;
38+
sixth;
3439
var = // Line comment
3540
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
36-
* Block comment line 2 */ fifth;
41+
* Block comment line 2 */ seventh;
3742
}
3843

3944
// BinOp Assignemnt - Multiline Block comments

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var = second;
66
fn main () {
77
let var = /* Block comment */ first;
88
var = /* Block comment */ second;
9+
var = /* Block comment */
10+
third;
911
}
1012
fn main () {
1113
let var = // Line comment

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,21 @@ fn main() {
3737
/* Block comment line 1
3838
* Block comment line 2 */
3939
third;
40+
var = /* Block comment single line 1 */
41+
/* Block comment single line 2*/
42+
forth;
43+
var = /* Block comment single line 1 */
44+
/* Block comment single line 2*/
45+
fifth;
4046
var =
4147
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
4248
* Block comment line 2 */
4349
// Line comment
44-
forth;
50+
sixth;
4551
var = // Line comment
4652
/* Block comment line 1 longggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
4753
* Block comment line 2 */
48-
fifth;
54+
seventh;
4955
}
5056

5157
// BinOp Assignemnt - Multiline Block comments

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn main() {
66
fn main() {
77
let var = /* Block comment */ first;
88
var = /* Block comment */ second;
9+
var = /* Block comment */ third;
910
}
1011
fn main() {
1112
let var = // Line comment

0 commit comments

Comments
 (0)