Skip to content

Commit

Permalink
Don't wrap comments that are part of a table
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz authored and calebcartwright committed Jan 29, 2023
1 parent fb1a223 commit 846662c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions tests/target/issue-4210.rs
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 846662c

Please sign in to comment.