Skip to content

Commit ca9d47c

Browse files
committed
Don't wrap comments that are part of a table
Closes #4210
1 parent e254753 commit ca9d47c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Diff for: rustfmt-core/rustfmt-lib/src/formatting/comment.rs

+7
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ impl<'a> CommentRewrite<'a> {
738738
if self.fmt.config.wrap_comments()
739739
&& unicode_str_width(line) > self.fmt.shape.width
740740
&& !has_url(line)
741+
&& !is_table_item(line)
741742
{
742743
match rewrite_string(line, &self.fmt, self.max_width) {
743744
Some(ref s) => {
@@ -867,6 +868,12 @@ fn has_url(s: &str) -> bool {
867868
s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
868869
}
869870

871+
/// Returns true if the given string may be part of a Markdown talble.
872+
fn is_table_item(s: &str) -> bool {
873+
// This function may return false positive, but should get its job done in most cases.
874+
s.trim_start().starts_with('|')
875+
}
876+
870877
/// Given the span, rewrite the missing comment inside it if available.
871878
/// Note that the given span must only include comments (or leading/trailing whitespaces).
872879
pub(crate) fn rewrite_missing_comment(

Diff for: rustfmt-core/rustfmt-lib/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)