about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAyaz Hafiz <ayaz.hafiz.1@gmail.com>2020-05-30 09:36:44 -0700
committerCaleb Cartwright <calebcartwright@users.noreply.github.com>2023-01-29 16:47:06 -0600
commit846662cdb36b5e0663d2be533f5e4178705c982b (patch)
tree97268b2a5b6e46e39e8660858ce2ff0507aadb44
parentfb1a223eba3f31242f266b4936a8fe956b9d0e08 (diff)
downloadrust-846662cdb36b5e0663d2be533f5e4178705c982b.tar.gz
rust-846662cdb36b5e0663d2be533f5e4178705c982b.zip
Don't wrap comments that are part of a table
Closes #4210
-rw-r--r--src/comment.rs15
-rw-r--r--tests/target/issue-4210.rs15
2 files changed, 29 insertions, 1 deletions
diff --git a/src/comment.rs b/src/comment.rs
index 0677fdc2b89..7167783872f 100644
--- a/src/comment.rs
+++ b/src/comment.rs
@@ -806,7 +806,8 @@ impl<'a> CommentRewrite<'a> {
         let should_wrap_comment = self.fmt.config.wrap_comments()
             && !is_markdown_header_doc_comment
             && unicode_str_width(line) > self.fmt.shape.width
-            && !has_url(line);
+            && !has_url(line)
+            && !is_table_item(line);
 
         if should_wrap_comment {
             match rewrite_string(line, &self.fmt, self.max_width) {
@@ -941,6 +942,18 @@ fn has_url(s: &str) -> bool {
         || REFERENCE_LINK_URL.is_match(s)
 }
 
+/// Returns true if the given string may be part of a Markdown talble.
+fn is_table_item(mut s: &str) -> bool {
+    // This function may return false positive, but should get its job done in most cases (i.e.
+    // markdown tables with two column delimiters).
+    s = s.trim_start();
+    return s.starts_with('|')
+        && match s.rfind('|') {
+            Some(0) | None => false,
+            _ => true,
+        };
+}
+
 /// Given the span, rewrite the missing comment inside it if available.
 /// Note that the given span must only include comments (or leading/trailing whitespaces).
 pub(crate) fn rewrite_missing_comment(
diff --git a/tests/target/issue-4210.rs b/tests/target/issue-4210.rs
new file mode 100644
index 00000000000..e96ba1b3fd0
--- /dev/null
+++ b/tests/target/issue-4210.rs
@@ -0,0 +1,15 @@
+// rustfmt-wrap_comments: true
+
+/// Table that is > 80 symbols:
+///
+/// | table | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
+/// |-------|-----------------------------------------------------------------------------|
+/// | val   | x                                                                           |
+pub struct Item;
+
+/// Table that is > 80 symbols:
+///
+/// | table | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+/// |-------|-----------------------------------------------------------------------------
+/// | val   | x                                                                          
+pub struct Item;