From 075aa909cd2bcdc603f45e0b65119c6a7e3b2030 Mon Sep 17 00:00:00 2001 From: Daniele D'Orazio Date: Mon, 15 Oct 2018 04:18:37 +0200 Subject: [PATCH] try to fix comment bad wrapping (#3099) --- src/comment.rs | 16 +++++++++--- src/expr.rs | 1 + src/string.rs | 38 +++++++++++++++------------- tests/source/issue-3059.rs | 7 +++++ tests/target/issue-3059.rs | 8 ++++++ tests/target/itemized-blocks/wrap.rs | 9 +++---- 6 files changed, 52 insertions(+), 27 deletions(-) create mode 100644 tests/source/issue-3059.rs create mode 100644 tests/target/issue-3059.rs diff --git a/src/comment.rs b/src/comment.rs index c946cc6e1c013..0f2409be1c6b0 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -579,7 +579,11 @@ fn rewrite_comment_inner( let item_fmt = ib.create_string_format(&fmt); result.push_str(&comment_line_separator); result.push_str(&ib.opener); - match rewrite_string(&item_block_buffer.replace("\n", " "), &item_fmt) { + match rewrite_string( + &item_block_buffer.replace("\n", " "), + &item_fmt, + max_chars.saturating_sub(ib.indent), + ) { Some(s) => result.push_str(&join_block( &s, &format!("{}{}", &comment_line_separator, ib.line_start), @@ -654,7 +658,7 @@ fn rewrite_comment_inner( } if config.wrap_comments() && line.len() > fmt.shape.width && !has_url(line) { - match rewrite_string(line, &fmt) { + match rewrite_string(line, &fmt, max_chars) { Some(ref s) => { is_prev_line_multi_line = s.contains('\n'); result.push_str(s); @@ -665,7 +669,7 @@ fn rewrite_comment_inner( result.pop(); result.push_str(&comment_line_separator); fmt.shape = Shape::legacy(max_chars, fmt_indent); - match rewrite_string(line, &fmt) { + match rewrite_string(line, &fmt, max_chars) { Some(ref s) => { is_prev_line_multi_line = s.contains('\n'); result.push_str(s); @@ -719,7 +723,11 @@ fn rewrite_comment_inner( let item_fmt = ib.create_string_format(&fmt); result.push_str(&comment_line_separator); result.push_str(&ib.opener); - match rewrite_string(&item_block_buffer.replace("\n", " "), &item_fmt) { + match rewrite_string( + &item_block_buffer.replace("\n", " "), + &item_fmt, + max_chars.saturating_sub(ib.indent), + ) { Some(s) => result.push_str(&join_block( &s, &format!("{}{}", &comment_line_separator, ib.line_start), diff --git a/src/expr.rs b/src/expr.rs index 8a5b3de7a1477..752fb792899fa 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1254,6 +1254,7 @@ fn rewrite_string_lit(context: &RewriteContext, span: Span, shape: Shape) -> Opt rewrite_string( str_lit, &StringFormat::new(shape.visual_indent(0), context.config), + shape.width.saturating_sub(2), ) } diff --git a/src/string.rs b/src/string.rs index 2fe13db2139cf..ca063123e39a7 100644 --- a/src/string.rs +++ b/src/string.rs @@ -70,7 +70,11 @@ impl<'a> StringFormat<'a> { } } -pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option { +pub fn rewrite_string<'a>( + orig: &str, + fmt: &StringFormat<'a>, + newline_max_chars: usize, +) -> Option { let max_chars_with_indent = fmt.max_chars_with_indent()?; let max_chars_without_indent = fmt.max_chars_without_indent()?; 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 result.push_str(fmt.line_end); result.push_str(&indent_with_newline); result.push_str(fmt.line_start); - cur_max_chars = max_chars_with_indent; + cur_max_chars = newline_max_chars; cur_start += len; } SnippetState::EndWithLineFeed(line, len) => { @@ -358,7 +362,7 @@ mod test { fn issue343() { let config = Default::default(); let fmt = StringFormat::new(Shape::legacy(2, Indent::empty()), &config); - rewrite_string("eq_", &fmt); + rewrite_string("eq_", &fmt, 2); } #[test] @@ -463,7 +467,7 @@ mod test { let mut config: Config = Default::default(); config.set().max_width(27); let fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config); - let rewritten_string = rewrite_string(string, &fmt); + let rewritten_string = rewrite_string(string, &fmt, 27); assert_eq!( rewritten_string, Some("\"Nulla\nconsequat erat at massa. \\\n Vivamus id mi.\"".to_string()) @@ -477,11 +481,11 @@ mod test { let mut fmt = StringFormat::new(Shape::legacy(25, Indent::empty()), &config); fmt.trim_end = true; - let rewritten_string = rewrite_string(string, &fmt); + let rewritten_string = rewrite_string(string, &fmt, 25); assert_eq!(rewritten_string, Some("\"Vivamus id mi.\"".to_string())); fmt.trim_end = false; // default value of trim_end - let rewritten_string = rewrite_string(string, &fmt); + let rewritten_string = rewrite_string(string, &fmt, 25); assert_eq!(rewritten_string, Some("\"Vivamus id mi. \"".to_string())); } @@ -499,7 +503,7 @@ mod test { config: &config, }; - let rewritten_string = rewrite_string(string, &fmt); + let rewritten_string = rewrite_string(string, &fmt, 100); assert_eq!( rewritten_string, Some("Vivamus id mi.\n // Vivamus id mi.".to_string()) @@ -521,7 +525,7 @@ mod test { }; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 30), Some( "Aenean metus.\n // Vestibulum ac lacus. Vivamus\n // porttitor" .to_string() @@ -544,7 +548,7 @@ mod test { }; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 30), Some( "Aenean metus.\n // Vestibulum ac lacus. Vivamus@\n // porttitor" .to_string() @@ -567,7 +571,7 @@ mod test { let comment = "Aenean metus. Vestibulum\n\nac lacus. Vivamus porttitor"; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 30), Some( "Aenean metus. Vestibulum\n //\n // ac lacus. Vivamus porttitor".to_string() ) @@ -576,7 +580,7 @@ mod test { fmt.shape = Shape::legacy(15, Indent::from_width(&config, 4)); let comment = "Aenean\n\nmetus. Vestibulum ac lacus. Vivamus porttitor"; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 15), Some( r#"Aenean // @@ -603,7 +607,7 @@ mod test { let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n\n"; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 20), Some( "Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n //\n".to_string() ) @@ -611,13 +615,13 @@ mod test { let comment = "Aenean\n\nmetus. Vestibulum ac lacus.\n"; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 20), Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.\n".to_string()) ); let comment = "Aenean\n \nmetus. Vestibulum ac lacus."; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 20), Some("Aenean\n //\n // metus. Vestibulum ac\n // lacus.".to_string()) ); } @@ -637,14 +641,14 @@ mod test { let comment = "Aenean metus. Vestibulum ac lacus."; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 13), Some("Aenean metus.\n // Vestibulum ac\n // lacus.".to_string()) ); fmt.trim_end = false; let comment = "Vestibulum ac lacus."; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 13), Some("Vestibulum \n // ac lacus.".to_string()) ); @@ -652,7 +656,7 @@ mod test { fmt.line_end = "\\"; let comment = "Vestibulum ac lacus."; assert_eq!( - rewrite_string(comment, &fmt), + rewrite_string(comment, &fmt, 13), Some("Vestibulum\\\n // ac lacus.".to_string()) ); } diff --git a/tests/source/issue-3059.rs b/tests/source/issue-3059.rs new file mode 100644 index 0000000000000..49a75cd67abba --- /dev/null +++ b/tests/source/issue-3059.rs @@ -0,0 +1,7 @@ +// rustfmt-wrap_comments: true +// rustfmt-max_width: 80 + +/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc commodo ultricies dui. +/// Cras gravida rutrum massa. Donec accumsan mattis turpis. Quisque sem. Quisque elementum sapien +/// iaculis augue. In dui sem, congue sit amet, feugiat quis, lobortis at, eros. +fn func4() {} diff --git a/tests/target/issue-3059.rs b/tests/target/issue-3059.rs new file mode 100644 index 0000000000000..f750c12875952 --- /dev/null +++ b/tests/target/issue-3059.rs @@ -0,0 +1,8 @@ +// rustfmt-wrap_comments: true +// rustfmt-max_width: 80 + +/// Vestibulum elit nibh, rhoncus non, euismod sit amet, pretium eu, enim. Nunc +/// commodo ultricies dui. Cras gravida rutrum massa. Donec accumsan mattis +/// turpis. Quisque sem. Quisque elementum sapien iaculis augue. In dui sem, +/// congue sit amet, feugiat quis, lobortis at, eros. +fn func4() {} diff --git a/tests/target/itemized-blocks/wrap.rs b/tests/target/itemized-blocks/wrap.rs index 08d8ca3526127..c4d687dd3dbd8 100644 --- a/tests/target/itemized-blocks/wrap.rs +++ b/tests/target/itemized-blocks/wrap.rs @@ -40,8 +40,7 @@ /// All the parameters ***except for /// `from_theater`*** should be inserted as sent /// by the remote theater, ie. as passed to -/// [`Theater::send`] on the remote -/// actor: +/// [`Theater::send`] on the remote actor: /// * `from` is the sending (remote) [`ActorId`], /// as reported by the remote theater by /// theater-specific means @@ -53,15 +52,13 @@ /// All the parameters ***except for /// `from_theater`*** should be inserted as sent /// by the remote theater, ie. as passed to -/// [`Theater::send`] on the remote -/// actor +/// [`Theater::send`] on the remote actor fn func1() {} /// All the parameters ***except for /// `from_theater`*** should be inserted as sent /// by the remote theater, ie. as passed to -/// [`Theater::send`] on the remote -/// actor: +/// [`Theater::send`] on the remote actor: /// * `from` is the sending (remote) [`ActorId`], /// as reported by the remote theater by /// theater-specific means