Skip to content

Commit 66c15e4

Browse files
committed
lists: Detect block comment by starting from the end.
The issue with the current code is that comments are collapsed, so comments like the one from the test end up in a string like: ``` "// this is a single line comment\n/* block = */" ``` I chose to fix it by detecting whether we're in a block comment starting from the end instead, and tested a single-line comment ended in `*/` just for sanity, ensuring line breaks are not removed in that case, which would break the formatting. The right fix eventually is probably to lex the comments properly, but this does the work for now, I guess :) Fixes rust-lang#3025
1 parent 4568c1a commit 66c15e4

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

src/lists.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -566,14 +566,9 @@ where
566566

567567
pub fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
568568
let trimmed_pre_snippet = pre_snippet.trim();
569+
let has_block_comment = trimmed_pre_snippet.ends_with("*/");
569570
let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
570-
let has_block_comment = trimmed_pre_snippet.starts_with("/*");
571-
if has_single_line_comment {
572-
(
573-
Some(trimmed_pre_snippet.to_owned()),
574-
ListItemCommentStyle::DifferentLine,
575-
)
576-
} else if has_block_comment {
571+
if has_block_comment {
577572
let comment_end = pre_snippet.chars().rev().position(|c| c == '/').unwrap();
578573
if pre_snippet
579574
.chars()
@@ -591,6 +586,11 @@ pub fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommen
591586
ListItemCommentStyle::SameLine,
592587
)
593588
}
589+
} else if has_single_line_comment {
590+
(
591+
Some(trimmed_pre_snippet.to_owned()),
592+
ListItemCommentStyle::DifferentLine,
593+
)
594594
} else {
595595
(None, ListItemCommentStyle::None)
596596
}

tests/source/expr-block.rs

+15
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,21 @@ fn issue_1862() {
280280
)
281281
}
282282

283+
fn issue_3025() {
284+
foo(
285+
// This describes the argument below.
286+
/* bar = */ None ,
287+
// This describes the argument below.
288+
something_something,
289+
// This describes the argument below. */
290+
None ,
291+
// This describes the argument below.
292+
/* This comment waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay too long to be kept on the same line */ None ,
293+
// This describes the argument below.
294+
/* com */ this_last_arg_is_tooooooooooooooooooooooooooooooooo_long_to_be_kept_with_the_pre_comment ,
295+
)
296+
}
297+
283298
fn issue_1878() {
284299
let channel: &str = seq.next_element()?.ok_or_else(|| de::Error::invalid_length(2, &self))?;
285300
}

tests/target/expr-block.rs

+17
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,23 @@ fn issue_1862() {
281281
)
282282
}
283283

284+
fn issue_3025() {
285+
foo(
286+
// This describes the argument below.
287+
/* bar = */ None,
288+
// This describes the argument below.
289+
something_something,
290+
// This describes the argument below. */
291+
None,
292+
// This describes the argument below.
293+
/* This comment waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay too long to be kept on the same line */
294+
None,
295+
// This describes the argument below.
296+
/* com */
297+
this_last_arg_is_tooooooooooooooooooooooooooooooooo_long_to_be_kept_with_the_pre_comment,
298+
)
299+
}
300+
284301
fn issue_1878() {
285302
let channel: &str = seq
286303
.next_element()?

0 commit comments

Comments
 (0)