Skip to content

Commit 846662c

Browse files
ayazhafizcalebcartwright
authored andcommitted
Don't wrap comments that are part of a table
Closes #4210
1 parent fb1a223 commit 846662c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Diff for: src/comment.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,8 @@ impl<'a> CommentRewrite<'a> {
806806
let should_wrap_comment = self.fmt.config.wrap_comments()
807807
&& !is_markdown_header_doc_comment
808808
&& unicode_str_width(line) > self.fmt.shape.width
809-
&& !has_url(line);
809+
&& !has_url(line)
810+
&& !is_table_item(line);
810811

811812
if should_wrap_comment {
812813
match rewrite_string(line, &self.fmt, self.max_width) {
@@ -941,6 +942,18 @@ fn has_url(s: &str) -> bool {
941942
|| REFERENCE_LINK_URL.is_match(s)
942943
}
943944

945+
/// Returns true if the given string may be part of a Markdown talble.
946+
fn is_table_item(mut s: &str) -> bool {
947+
// This function may return false positive, but should get its job done in most cases (i.e.
948+
// markdown tables with two column delimiters).
949+
s = s.trim_start();
950+
return s.starts_with('|')
951+
&& match s.rfind('|') {
952+
Some(0) | None => false,
953+
_ => true,
954+
};
955+
}
956+
944957
/// Given the span, rewrite the missing comment inside it if available.
945958
/// Note that the given span must only include comments (or leading/trailing whitespaces).
946959
pub(crate) fn rewrite_missing_comment(

Diff for: tests/target/issue-4210.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// rustfmt-wrap_comments: true
2+
3+
/// Table that is > 80 symbols:
4+
///
5+
/// | table | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
6+
/// |-------|-----------------------------------------------------------------------------|
7+
/// | val | x |
8+
pub struct Item;
9+
10+
/// Table that is > 80 symbols:
11+
///
12+
/// | table | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
13+
/// |-------|-----------------------------------------------------------------------------
14+
/// | val | x
15+
pub struct Item;

0 commit comments

Comments
 (0)