Add  support for inputs with line breaks
@@ -585,7 +585,27 @@ impl<'a> WrapIterImpl<'a> {
while let Some((idx, ch)) = self.char_indices.next() {
let char_width = ch.width().unwrap_or(0);
let char_len = ch.len_utf8();
- if is_whitespace(ch) {
+
+ if ch == '\n' {
+ self.split = idx;
+ self.split_len = char_len;
+ self.line_width_at_split = self.line_width;
+ self.in_whitespace = false;
+
+ // If this is not the final line, return the current line. Otherwise,
+ // we will return the line with its line break after exiting the loop
+ if self.split + self.split_len < self.source.len() {
+ let mut line = self.create_result_line(wrapper);
+ cow_add_assign(&mut line, &self.source[self.start..self.split]);
+
+ self.start = self.split + self.split_len;
+ self.line_width += wrapper.subsequent_indent.width();
+ self.line_width -= self.line_width_at_split;
+
+ return Some(line);
+ }
+ }
+ else if is_whitespace(ch) {
// Extend the previous split or create a new one.
if self.in_whitespace {
self.split_len += char_len;
@@ -1223,4 +1243,24 @@ mod tests {
" bar"];
assert_eq!(dedent(&add_nl(&x)), add_nl(&y));
}
+
+ #[test]
+ fn multiline() {
+ assert_eq!(fill("1 3 5 7\n1 3 5 7",11),"1 3 5 7\n1 3 5 7");
+ assert_eq!(fill("1 3 5 7\n1 3 5 7",5),"1 3 5\n7\n1 3 5\n7");
+ assert_eq!(fill("1 3 5 7\n1 3 5 7",6),"1 3 5\n7\n1 3 5\n7");
+ assert_eq!(fill("1 3 5 7\nabcdefghi k",5),"1 3 5\n7\nabcde\nfghi\nk");
+ assert_eq!(fill("abcdefgh\ni k",5),"abcde\nfgh\ni k");
+ assert_eq!(fill("abcdefgh\nijklmnop",5),"abcde\nfgh\nijklm\nnop");
+
+ assert_eq!(fill("test\n",11),"test\n");
+ assert_eq!(fill("test\na",11),"test\na");
+ assert_eq!(fill("test\na\n",11),"test\na\n");
+ assert_eq!(fill("test\n\na\n\n",11),"test\n\na\n\n");
+ assert_eq!(fill("test\n\nabcdefghijk\n\nabcdefghijkm",11),"test\n\nabcdefghijk\n\nabcdefghijk\nm");
+ assert_eq!(fill("test\nabcdefghi\n",11),"test\nabcdefghi\n");
+ assert_eq!(fill("test\nabcdefghi\n\n",11),"test\nabcdefghi\n\n");
+ assert_eq!(fill("test\nabcdefghijklmnopq\n\n",11),"test\nabcdefghijk\nlmnopq\n\n");
+ assert_eq!(fill("test\nabcdefghijklmnopq abcdefghijk\n\n",11),"test\nabcdefghijk\nlmnopq\nabcdefghijk\n\n");
+ }
}
