Skip to content

Commit deee04a

Browse files
ayazhafizNemo157
authored andcommitted
Don't wrap comments that are part of a table
Closes rust-lang#4210
1 parent 7432422 commit deee04a

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/comment.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,8 @@ impl<'a> CommentRewrite<'a> {
804804
let should_wrap_comment = self.fmt.config.wrap_comments()
805805
&& !is_markdown_header_doc_comment
806806
&& unicode_str_width(line) > self.fmt.shape.width
807-
&& !has_url(line);
807+
&& !has_url(line)
808+
&& !is_table_item(line);
808809

809810
if should_wrap_comment {
810811
match rewrite_string(line, &self.fmt, self.max_width) {
@@ -939,6 +940,18 @@ fn has_url(s: &str) -> bool {
939940
|| REFERENCE_LINK_URL.is_match(s)
940941
}
941942

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

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)