From a78da55db01a6982bd869b4713bb84aee23fb6be Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Tue, 3 May 2022 00:36:24 +0800 Subject: [PATCH] don't break on consecutive punctuations --- src/string.rs | 14 ++++++++++---- tests/source/issue-5260.rs | 4 ++++ tests/target/issue-5260.rs | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/string.rs b/src/string.rs index 78b72a50cb2..305ababa58a 100644 --- a/src/string.rs +++ b/src/string.rs @@ -343,15 +343,21 @@ fn is_valid_linebreak(input: &[&str], pos: usize) -> bool { return true; } let is_punctuation = is_punctuation(input[pos]); - if is_punctuation && !is_part_of_type(input, pos) { + if is_punctuation && !is_double_punctuation(input, pos) { return true; } false } -fn is_part_of_type(input: &[&str], pos: usize) -> bool { - input.get(pos..=pos + 1) == Some(&[":", ":"]) - || input.get(pos.saturating_sub(1)..=pos) == Some(&[":", ":"]) +fn is_double_punctuation(input: &[&str], pos: usize) -> bool { + input + .get(pos..=pos + 1) + .map(|slice| slice.iter().all(|cha| is_punctuation(cha))) + .unwrap_or(false) + || input + .get(pos.saturating_sub(1)..=pos) + .map(|slice| slice.iter().all(|cha| is_punctuation(cha))) + .unwrap_or(false) } fn is_new_line(grapheme: &str) -> bool { diff --git a/tests/source/issue-5260.rs b/tests/source/issue-5260.rs index c0606817290..5b1647b079e 100644 --- a/tests/source/issue-5260.rs +++ b/tests/source/issue-5260.rs @@ -12,3 +12,7 @@ fn documented_with_verylongtype() { // on the type when `wrap_comments = true` } +/// So many stars ************************************************************************ +fn documented_with_many_stars() { + // # don't break consecutive punctuations +} diff --git a/tests/target/issue-5260.rs b/tests/target/issue-5260.rs index 171f6fa51b7..1c1521dcb0d 100644 --- a/tests/target/issue-5260.rs +++ b/tests/target/issue-5260.rs @@ -11,3 +11,9 @@ fn documented_with_verylongtype() { // # We're using a long type link, rustfmt should not break line // on the type when `wrap_comments = true` } + +/// So many stars +/// ************************************************************************ +fn documented_with_many_stars() { + // # don't break consecutive punctuations +}