Skip to content

Commit c65ba14

Browse files
PSeitzcalebcartwright
authored andcommitted
Fixes #5260
Fixes #5260 by checking if it is part of a type '::'
1 parent 3cc1f5e commit c65ba14

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

src/comment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl<'a> CommentRewrite<'a> {
796796
// 1) wrap_comments = true is configured
797797
// 2) The comment is not the start of a markdown header doc comment
798798
// 3) The comment width exceeds the shape's width
799-
// 4) No URLS were found in the commnet
799+
// 4) No URLS were found in the comment
800800
let should_wrap_comment = self.fmt.config.wrap_comments()
801801
&& !is_markdown_header_doc_comment
802802
&& unicode_str_width(line) > self.fmt.shape.width

src/string.rs

+38-7
Original file line numberDiff line numberDiff line change
@@ -315,27 +315,45 @@ fn break_string(max_width: usize, trim_end: bool, line_end: &str, input: &[&str]
315315
// Found a whitespace and what is on its left side is big enough.
316316
Some(index) if index >= MIN_STRING => break_at(index),
317317
// No whitespace found, try looking for a punctuation instead
318-
_ => match input[0..max_width_index_in_input]
319-
.iter()
320-
.rposition(|grapheme| is_punctuation(grapheme))
318+
_ => match (0..max_width_index_in_input)
319+
.rev()
320+
.skip_while(|pos| !is_valid_linebreak(input, *pos))
321+
.next()
321322
{
322323
// Found a punctuation and what is on its left side is big enough.
323324
Some(index) if index >= MIN_STRING => break_at(index),
324325
// Either no boundary character was found to the left of `input[max_chars]`, or the line
325326
// got too small. We try searching for a boundary character to the right.
326-
_ => match input[max_width_index_in_input..]
327-
.iter()
328-
.position(|grapheme| is_whitespace(grapheme) || is_punctuation(grapheme))
327+
_ => match (max_width_index_in_input..input.len())
328+
.skip_while(|pos| !is_valid_linebreak(input, *pos))
329+
.next()
329330
{
330331
// A boundary was found after the line limit
331-
Some(index) => break_at(max_width_index_in_input + index),
332+
Some(index) => break_at(index),
332333
// No boundary to the right, the input cannot be broken
333334
None => SnippetState::EndOfInput(input.concat()),
334335
},
335336
},
336337
}
337338
}
338339

340+
fn is_valid_linebreak(input: &[&str], pos: usize) -> bool {
341+
let is_whitespace = is_whitespace(input[pos]);
342+
if is_whitespace {
343+
return true;
344+
}
345+
let is_punctuation = is_punctuation(input[pos]);
346+
if is_punctuation && !is_part_of_type(input, pos) {
347+
return true;
348+
}
349+
false
350+
}
351+
352+
fn is_part_of_type(input: &[&str], pos: usize) -> bool {
353+
input.get(pos..=pos + 1) == Some(&[":", ":"])
354+
|| input.get(pos.saturating_sub(1)..=pos) == Some(&[":", ":"])
355+
}
356+
339357
fn is_new_line(grapheme: &str) -> bool {
340358
let bytes = grapheme.as_bytes();
341359
bytes.starts_with(b"\n") || bytes.starts_with(b"\r\n")
@@ -369,6 +387,19 @@ mod test {
369387
rewrite_string("eq_", &fmt, 2);
370388
}
371389

390+
#[test]
391+
fn line_break_at_valid_points_test() {
392+
let string = "[TheName](Dont::break::my::type::That::would::be::very::nice) break here";
393+
let graphemes = UnicodeSegmentation::graphemes(&*string, false).collect::<Vec<&str>>();
394+
assert_eq!(
395+
break_string(20, false, "", &graphemes[..]),
396+
SnippetState::LineEnd(
397+
"[TheName](Dont::break::my::type::That::would::be::very::nice) ".to_string(),
398+
62
399+
)
400+
);
401+
}
402+
372403
#[test]
373404
fn should_break_on_whitespace() {
374405
let string = "Placerat felis. Mauris porta ante sagittis purus.";

tests/source/issue-5260.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-wrap_comments: true
2+
3+
/// [MyType](VeryLongPathToMyType::NoLineBreak::Here::Okay::ThatWouldBeNice::Thanks)
4+
fn documented_with_longtype() {
5+
// # We're using a long type link, rustfmt should not break line
6+
// on the type when `wrap_comments = true`
7+
}
8+
9+
/// VeryLongPathToMyType::JustMyType::But::VeryVery::Long::NoLineBreak::Here::Okay::ThatWouldBeNice::Thanks
10+
fn documented_with_verylongtype() {
11+
// # We're using a long type link, rustfmt should not break line
12+
// on the type when `wrap_comments = true`
13+
}
14+

tests/target/issue-5260.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// rustfmt-wrap_comments: true
2+
3+
/// [MyType](VeryLongPathToMyType::NoLineBreak::Here::Okay::ThatWouldBeNice::Thanks)
4+
fn documented_with_longtype() {
5+
// # We're using a long type link, rustfmt should not break line
6+
// on the type when `wrap_comments = true`
7+
}
8+
9+
/// VeryLongPathToMyType::JustMyType::But::VeryVery::Long::NoLineBreak::Here::Okay::ThatWouldBeNice::Thanks
10+
fn documented_with_verylongtype() {
11+
// # We're using a long type link, rustfmt should not break line
12+
// on the type when `wrap_comments = true`
13+
}

0 commit comments

Comments
 (0)