From deee04a7385c1697f09f55d454d38bcbf429011a Mon Sep 17 00:00:00 2001 From: Ayaz Hafiz Date: Sat, 30 May 2020 09:36:44 -0700 Subject: [PATCH 1/4] Don't wrap comments that are part of a table Closes #4210 --- src/comment.rs | 15 ++++++++++++++- tests/target/issue-4210.rs | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/target/issue-4210.rs diff --git a/src/comment.rs b/src/comment.rs index 4d565afc1e0..47e2268f09b 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -804,7 +804,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) { @@ -939,6 +940,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; From 997994d5920d5b496bb9f341fa499c70b4ab4c76 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 10 Sep 2022 13:30:41 +0200 Subject: [PATCH 2/4] Fix typo --- src/comment.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/comment.rs b/src/comment.rs index 47e2268f09b..ca52d831f23 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -940,7 +940,7 @@ 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. +/// Returns true if the given string may be part of a Markdown table. 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). From 5116d19c765768f13074c228209191695cfb2c54 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 10 Sep 2022 13:31:08 +0200 Subject: [PATCH 3/4] Test that both table headers and values are not wrapped --- tests/target/issue-4210.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/target/issue-4210.rs b/tests/target/issue-4210.rs index e96ba1b3fd0..3fd96639082 100644 --- a/tests/target/issue-4210.rs +++ b/tests/target/issue-4210.rs @@ -7,9 +7,9 @@ /// | val | x | pub struct Item; -/// Table that is > 80 symbols: +/// Table value that is > 80 symbols: /// -/// | table | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx -/// |-------|----------------------------------------------------------------------------- -/// | val | x -pub struct Item; +/// | table | heading +/// |----------|----------------------------------------------------------------------------- +/// | long val | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +pub struct Item2; From 2a76b0b26631aec1dbd36101654460796b960f91 Mon Sep 17 00:00:00 2001 From: Wim Looman Date: Sat, 10 Sep 2022 13:31:26 +0200 Subject: [PATCH 4/4] Test that with all comment wrapping disabled table comments are not wrapped --- tests/target/issue-4210-disabled.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tests/target/issue-4210-disabled.rs diff --git a/tests/target/issue-4210-disabled.rs b/tests/target/issue-4210-disabled.rs new file mode 100644 index 00000000000..3ba87aab8c5 --- /dev/null +++ b/tests/target/issue-4210-disabled.rs @@ -0,0 +1,15 @@ +// rustfmt-wrap_comments: false + +/// Table that is > 80 symbols: +/// +/// | table | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | +/// |-------|-----------------------------------------------------------------------------| +/// | val | x | +pub struct Item; + +/// Table value that is > 80 symbols: +/// +/// | table | heading +/// |----------|----------------------------------------------------------------------------- +/// | long val | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +pub struct Item2;