Skip to content

Don't wrap comments that are part of a table #4214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions rustfmt-core/rustfmt-lib/src/formatting/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,7 @@ impl<'a> CommentRewrite<'a> {
if self.fmt.config.wrap_comments()
&& unicode_str_width(line) > self.fmt.shape.width
&& !has_url(line)
&& !is_table_item(line)
{
match rewrite_string(line, &self.fmt, self.max_width) {
Some(ref s) => {
Expand Down Expand Up @@ -867,6 +868,18 @@ fn has_url(s: &str) -> bool {
s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
}

/// 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 rustfmt-core/rustfmt-lib/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;