Skip to content

Commit f73b015

Browse files
authored
Don't wrap comments that are part of a table (#4214)
* Don't wrap comments that are part of a table Closes #4210 * fixup! Don't wrap comments that are part of a table
2 parents c6c9c7f + 172aca4 commit f73b015

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

+13
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,18 @@ 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(mut s: &str) -> bool {
873+
// This function may return false positive, but should get its job done in most cases (i.e.
874+
// markdown tables with two column delimiters).
875+
s = s.trim_start();
876+
return s.starts_with('|')
877+
&& match s.rfind('|') {
878+
Some(0) | None => false,
879+
_ => true,
880+
};
881+
}
882+
870883
/// Given the span, rewrite the missing comment inside it if available.
871884
/// Note that the given span must only include comments (or leading/trailing whitespaces).
872885
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)