]> git.lizzy.rs Git - rust.git/commitdiff
lists: Detect block comment by starting from the end.
authorEmilio Cobos Álvarez <emilio@crisal.io>
Sat, 22 Sep 2018 10:03:33 +0000 (12:03 +0200)
committerEmilio Cobos Álvarez <emilio@crisal.io>
Sat, 22 Sep 2018 10:16:38 +0000 (12:16 +0200)
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 #3025

src/lists.rs
tests/source/expr-block.rs
tests/target/expr-block.rs

index f1b01ea8055f84046cc77e72365a8b8994f4a42c..57f778896d3002020c89db427060b20fe99e77bc 100644 (file)
@@ -566,14 +566,9 @@ pub struct ListItems<'a, I, F1, F2, F3>
 
 pub fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
     let trimmed_pre_snippet = pre_snippet.trim();
+    let has_block_comment = trimmed_pre_snippet.ends_with("*/");
     let has_single_line_comment = trimmed_pre_snippet.starts_with("//");
-    let has_block_comment = trimmed_pre_snippet.starts_with("/*");
-    if has_single_line_comment {
-        (
-            Some(trimmed_pre_snippet.to_owned()),
-            ListItemCommentStyle::DifferentLine,
-        )
-    } else if has_block_comment {
+    if has_block_comment {
         let comment_end = pre_snippet.chars().rev().position(|c| c == '/').unwrap();
         if pre_snippet
             .chars()
@@ -591,6 +586,11 @@ pub fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommen
                 ListItemCommentStyle::SameLine,
             )
         }
+    } else if has_single_line_comment {
+        (
+            Some(trimmed_pre_snippet.to_owned()),
+            ListItemCommentStyle::DifferentLine,
+        )
     } else {
         (None, ListItemCommentStyle::None)
     }
index 48cc1fd72ae8819b011a702e3ac5234c1287ae59..a3e6100b7f222aefc48da4a7f1399867076690c3 100644 (file)
@@ -280,6 +280,21 @@ fn issue_1862() {
     )
 }
 
+fn issue_3025() {
+    foo(
+        // This describes the argument below.
+        /* bar = */ None ,
+        // This describes the argument below.
+        something_something,
+        // This describes the argument below. */
+        None ,
+        // This describes the argument below.
+        /* This comment waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay too long to be kept on the same line */ None ,
+        // This describes the argument below.
+        /* com */ this_last_arg_is_tooooooooooooooooooooooooooooooooo_long_to_be_kept_with_the_pre_comment ,
+    )
+}
+
 fn issue_1878() {
     let channel: &str = seq.next_element()?.ok_or_else(|| de::Error::invalid_length(2, &self))?;
 }
index 3d452cbde39b55605b2c770fcf50fe399edaeddc..c5770065087337c0ef82f776df485611b7a0cd9d 100644 (file)
@@ -281,6 +281,23 @@ fn issue_1862() {
     )
 }
 
+fn issue_3025() {
+    foo(
+        // This describes the argument below.
+        /* bar = */ None,
+        // This describes the argument below.
+        something_something,
+        // This describes the argument below. */
+        None,
+        // This describes the argument below.
+        /* This comment waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay too long to be kept on the same line */
+        None,
+        // This describes the argument below.
+        /* com */
+        this_last_arg_is_tooooooooooooooooooooooooooooooooo_long_to_be_kept_with_the_pre_comment,
+    )
+}
+
 fn issue_1878() {
     let channel: &str = seq
         .next_element()?