Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 075aa90

Browse files
danieledapotopecongiro
authored andcommitted
try to fix comment bad wrapping (rust-lang#3099)
1 parent 51ddac3 commit 075aa90

File tree

6 files changed

+52
-27
lines changed

6 files changed

+52
-27
lines changed

src/comment.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,11 @@ fn rewrite_comment_inner(
579579
let item_fmt = ib.create_string_format(&fmt);
580580
result.push_str(&comment_line_separator);
581581
result.push_str(&ib.opener);
582-
match rewrite_string(&item_block_buffer.replace("\n", " "), &item_fmt) {
582+
match rewrite_string(
583+
&item_block_buffer.replace("\n", " "),
584+
&item_fmt,
585+
max_chars.saturating_sub(ib.indent),
586+
) {
583587
Some(s) => result.push_str(&join_block(
584588
&s,
585589
&format!("{}{}", &comment_line_separator, ib.line_start),
@@ -654,7 +658,7 @@ fn rewrite_comment_inner(
654658
}
655659

656660
if config.wrap_comments() && line.len() > fmt.shape.width && !has_url(line) {
657-
match rewrite_string(line, &fmt) {
661+
match rewrite_string(line, &fmt, max_chars) {
658662
Some(ref s) => {
659663
is_prev_line_multi_line = s.contains('\n');
660664
result.push_str(s);
@@ -665,7 +669,7 @@ fn rewrite_comment_inner(
665669
result.pop();
666670
result.push_str(&comment_line_separator);
667671
fmt.shape = Shape::legacy(max_chars, fmt_indent);
668-
match rewrite_string(line, &fmt) {
672+
match rewrite_string(line, &fmt, max_chars) {
669673
Some(ref s) => {
670674
is_prev_line_multi_line = s.contains('\n');
671675
result.push_str(s);
@@ -719,7 +723,11 @@ fn rewrite_comment_inner(
719723
let item_fmt = ib.create_string_format(&fmt);
720724
result.push_str(&comment_line_separator);
721725
result.push_str(&ib.opener);
722-
match rewrite_string(&item_block_buffer.replace("\n", " "), &item_fmt) {
726+
match rewrite_string(
727+
&item_block_buffer.replace("\n", " "),
728+
&item_fmt,
729+
max_chars.saturating_sub(ib.indent),
730+
) {
723731
Some(s) => result.push_str(&join_block(
724732
&s,
725733
&format!("{}{}", &comment_line_separator, ib.line_start),

src/expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt
12541254
rewrite_string(
12551255
str_lit,
12561256
&StringFormat::new(shape.visual_indent(0), context.config),
1257+
shape.width.saturating_sub(2),
12571258
)
12581259
}
12591260

src/string.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ impl<'a> StringFormat<'a> {
7070
}
7171
}
7272

73-
pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String> {
73+
pub fn rewrite_string<'a>(
74+
orig: &str,
75+
fmt: &StringFormat<'a>,
76+
newline_max_chars: usize,
77+
) -> Option<String> {
7478
let max_chars_with_indent = fmt.max_chars_with_indent()?;
7579
let max_chars_without_indent = fmt.max_chars_without_indent()?;
7680
let indent_with_newline = fmt.shape.indent.to_string_with_newline(fmt.config);
@@ -129,7 +133,7 @@ pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String>
129133
result.push_str(fmt.line_end);
130134
result.push_str(&indent_with_newline);
131135
result.push_str(fmt.line_start);
132-
cur_max_chars = max_chars_with_indent;
136+
cur_max_chars = newline_max_chars;
133137
cur_start += len;
134138
}
135139
SnippetState::EndWithLineFeed(line, len) => {
@@ -358,7 +362,7 @@ mod test {
358362
fn issue343() {
359363
let config = Default::default();
360364
let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config);
361-
rewrite_string("eq_", &fmt);
365+
rewrite_string("eq_", &fmt, 2);
362366
}
363367

364368
#[test]
@@ -463,7 +467,7 @@ mod test {
463467
let mut config: Config = Default::default();
464468
config.set().max_width(27);
465469
let fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
466-
let rewritten_string = rewrite_string(string, &fmt);
470+
let rewritten_string = rewrite_string(string, &fmt, 27);
467471
assert_eq!(
468472
rewritten_string,
469473
Some("\"Nulla\nconsequat erat at massa. \\\n Vivamus id mi.\"".to_string())
@@ -477,11 +481,11 @@ mod test {
477481
let mut fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config);
478482

479483
fmt.trim_end = true;
480-
let rewritten_string = rewrite_string(string, &fmt);
484+
let rewritten_string = rewrite_string(string, &fmt, 25);
481485
assert_eq!(rewritten_string, Some("\"Vivamus id mi.\"".to_string()));
482486

483487
fmt.trim_end = false; // default value of trim_end
484-
let rewritten_string = rewrite_string(string, &fmt);
488+
let rewritten_string = rewrite_string(string, &fmt, 25);
485489
assert_eq!(rewritten_string, Some("\"Vivamus id mi. \"".to_string()));
486490
}
487491

@@ -499,7 +503,7 @@ mod test {
499503
config: &config,
500504
};
501505

502-
let rewritten_string = rewrite_string(string, &fmt);
506+
let rewritten_string = rewrite_string(string, &fmt, 100);
503507
assert_eq!(
504508
rewritten_string,
505509
Some("Vivamus id mi.\n // Vivamus id mi.".to_string())
@@ -521,7 +525,7 @@ mod test {
521525
};
522526

523527
assert_eq!(
524-
rewrite_string(comment, &fmt),
528+
rewrite_string(comment, &fmt, 30),
525529
Some(
526530
"Aenean metus.\n // Vestibulum ac lacus. Vivamus\n // porttitor"
527531
.to_string()
@@ -544,7 +548,7 @@ mod test {
544548
};
545549

546550
assert_eq!(
547-
rewrite_string(comment, &fmt),
551+
rewrite_string(comment, &fmt, 30),
548552
Some(
549553
"Aenean metus.\n // Vestibulum ac lacus. Vivamus@\n // porttitor"
550554
.to_string()
@@ -567,7 +571,7 @@ mod test {
567571

568572
let comment = "Aenean metus. Vestibulum\n\nac lacus. Vivamus porttitor";
569573
assert_eq!(
570-
rewrite_string(comment, &fmt),
574+
rewrite_string(comment, &fmt, 30),
571575
Some(
572576
"Aenean metus. Vestibulum\n //\n // ac lacus. Vivamus porttitor".to_string()
573577
)
@@ -576,7 +580,7 @@ mod test {
576580
fmt.shape = Shape::legacy(15, Indent::from_width(&config, 4));
577581
let comment = "Aenean\n\nmetus. Vestibulum ac lacus. Vivamus porttitor";
578582
assert_eq!(
579-
rewrite_string(comment, &fmt),
583+
rewrite_string(comment, &fmt, 15),
580584
Some(
581585
r#"Aenean
582586
//
@@ -603,21 +607,21 @@ mod test {
603607

604608
let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n\n";
605609
assert_eq!(
606-
rewrite_string(comment, &fmt),
610+
rewrite_string(comment, &fmt, 20),
607611
Some(
608612
"Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n //\n".to_string()
609613
)
610614
);
611615

612616
let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n";
613617
assert_eq!(
614-
rewrite_string(comment, &fmt),
618+
rewrite_string(comment, &fmt, 20),
615619
Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n".to_string())
616620
);
617621

618622
let comment = "Aenean\n \nmetus. Vestibulum ac lacus.";
619623
assert_eq!(
620-
rewrite_string(comment, &fmt),
624+
rewrite_string(comment, &fmt, 20),
621625
Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.".to_string())
622626
);
623627
}
@@ -637,22 +641,22 @@ mod test {
637641

638642
let comment = "Aenean metus. Vestibulum ac lacus.";
639643
assert_eq!(
640-
rewrite_string(comment, &fmt),
644+
rewrite_string(comment, &fmt, 13),
641645
Some("Aenean metus.\n // Vestibulum ac\n // lacus.".to_string())
642646
);
643647

644648
fmt.trim_end = false;
645649
let comment = "Vestibulum ac lacus.";
646650
assert_eq!(
647-
rewrite_string(comment, &fmt),
651+
rewrite_string(comment, &fmt, 13),
648652
Some("Vestibulum \n // ac lacus.".to_string())
649653
);
650654

651655
fmt.trim_end = true;
652656
fmt.line_end = "\\";
653657
let comment = "Vestibulum ac lacus.";
654658
assert_eq!(
655-
rewrite_string(comment, &fmt),
659+
rewrite_string(comment, &fmt, 13),
656660
Some("Vestibulum\\\n // ac lacus.".to_string())
657661
);
658662
}

tests/source/issue-3059.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rustfmt-wrap_comments: true
2+
// rustfmt-max_width: 80
3+
4+
/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc commodo ultricies dui.
5+
/// Cras gravida rutrum massa. Donec accumsan mattis turpis. Quisque sem. Quisque elementum sapien
6+
/// iaculis augue. In dui sem, congue sit amet, feugiat quis, lobortis at, eros.
7+
fn func4() {}

tests/target/issue-3059.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// rustfmt-wrap_comments: true
2+
// rustfmt-max_width: 80
3+
4+
/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc
5+
/// commodo ultricies dui. Cras gravida rutrum massa. Donec accumsan mattis
6+
/// turpis. Quisque sem. Quisque elementum sapien iaculis augue. In dui sem,
7+
/// congue sit amet, feugiat quis, lobortis at, eros.
8+
fn func4() {}

tests/target/itemized-blocks/wrap.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
/// All the parameters ***except for
4141
/// `from_theater`*** should be inserted as sent
4242
/// by the remote theater, ie. as passed to
43-
/// [`Theater::send`] on the remote
44-
/// actor:
43+
/// [`Theater::send`] on the remote actor:
4544
/// * `from` is the sending (remote) [`ActorId`],
4645
/// as reported by the remote theater by
4746
/// theater-specific means
@@ -53,15 +52,13 @@
5352
/// All the parameters ***except for
5453
/// `from_theater`*** should be inserted as sent
5554
/// by the remote theater, ie. as passed to
56-
/// [`Theater::send`] on the remote
57-
/// actor
55+
/// [`Theater::send`] on the remote actor
5856
fn func1() {}
5957

6058
/// All the parameters ***except for
6159
/// `from_theater`*** should be inserted as sent
6260
/// by the remote theater, ie. as passed to
63-
/// [`Theater::send`] on the remote
64-
/// actor:
61+
/// [`Theater::send`] on the remote actor:
6562
/// * `from` is the sending (remote) [`ActorId`],
6663
/// as reported by the remote theater by
6764
/// theater-specific means

0 commit comments

Comments
 (0)