diff --git a/src/lists.rs b/src/lists.rs index 7d612913361..24779e1667e 100644 --- a/src/lists.rs +++ b/src/lists.rs @@ -228,6 +228,7 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option let mut item_max_width: Option = None; let sep_place = SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator); + let mut prev_item_had_post_comment = false; let mut line_len = 0; let indent_str = &formatting.shape.indent.to_string(formatting.config); @@ -284,7 +285,9 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option let total_width = total_item_width(item) + item_sep_len; // 1 is space between separator and item. - if line_len > 0 && line_len + 1 + total_width > formatting.shape.width { + if (line_len > 0 && line_len + 1 + total_width > formatting.shape.width) + || prev_item_had_post_comment + { result.push('\n'); result.push_str(indent_str); line_len = 0; @@ -310,14 +313,15 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option // Pre-comments if let Some(ref comment) = item.pre_comment { // Block style in non-vertical mode. - let block_mode = tactic != DefinitiveListTactic::Vertical; + let block_mode = tactic == DefinitiveListTactic::Horizontal; // Width restriction is only relevant in vertical mode. let comment = rewrite_comment(comment, block_mode, formatting.shape, formatting.config)?; result.push_str(&comment); if !inner_item.is_empty() { - if tactic == DefinitiveListTactic::Vertical { + if tactic == DefinitiveListTactic::Vertical || tactic == DefinitiveListTactic::Mixed + { // We cannot keep pre-comments on the same line if the comment if normalized. let keep_comment = if formatting.config.normalize_comments() || item.pre_comment_style == ListItemCommentStyle::DifferentLine @@ -349,7 +353,7 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option result.push_str(inner_item); // Post-comments - if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() { + if tactic == DefinitiveListTactic::Horizontal && item.post_comment.is_some() { let comment = item.post_comment.as_ref().unwrap(); let formatted_comment = rewrite_comment( comment, @@ -366,7 +370,7 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option result.push_str(formatting.separator); } - if tactic == DefinitiveListTactic::Vertical && item.post_comment.is_some() { + if tactic != DefinitiveListTactic::Horizontal && item.post_comment.is_some() { let comment = item.post_comment.as_ref().unwrap(); let overhead = last_line_width(&result) + first_line_width(comment.trim()); @@ -446,6 +450,8 @@ pub fn write_list(items: I, formatting: &ListFormatting) -> Option item_max_width = None; result.push('\n'); } + + prev_item_had_post_comment = item.post_comment.is_some(); } Some(result) diff --git a/src/overflow.rs b/src/overflow.rs index f2f05d835ff..62675b4002c 100644 --- a/src/overflow.rs +++ b/src/overflow.rs @@ -530,5 +530,5 @@ fn shape_from_indent_style( fn no_long_items(list: &[ListItem]) -> bool { list.iter() - .all(|item| !item.has_comment() && item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD) + .all(|item| item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD) } diff --git a/tests/source/issue-2761.rs b/tests/source/issue-2761.rs new file mode 100644 index 00000000000..bc312319034 --- /dev/null +++ b/tests/source/issue-2761.rs @@ -0,0 +1,15 @@ +const DATA: &'static [u8] = &[ + 0x42, 0x50, 0x54, 0x44, //type + 0x23, 0x00, 0x00, 0x00, //size + 0x00, 0x00, 0x04, 0x00, //flags + 0xEC, 0x0C, 0x00, 0x00, //id + 0x00, 0x00, 0x00, 0x00, //revision + 0x2B, 0x00, //version + 0x00, 0x00, //unknown + 0x42, 0x50, 0x54, 0x4E, //field type + 0x1D, 0x00, //field size + 0x19, 0x00, 0x00, 0x00, //decompressed field size + 0x75, 0xc5, 0x21, 0x0d, 0x00, 0x00, 0x08, 0x05, 0xd1, 0x6c, //field data (compressed) + 0x6c, 0xdc, 0x57, 0x48, 0x3c, 0xfd, 0x5b, 0x5c, 0x02, 0xd4, //field data (compressed) + 0x6b, 0x32, 0xb5, 0xdc, 0xa3 //field data (compressed) +]; diff --git a/tests/target/issue-2761.rs b/tests/target/issue-2761.rs new file mode 100644 index 00000000000..ae4086617f8 --- /dev/null +++ b/tests/target/issue-2761.rs @@ -0,0 +1,15 @@ +const DATA: &'static [u8] = &[ + 0x42, 0x50, 0x54, 0x44, //type + 0x23, 0x00, 0x00, 0x00, //size + 0x00, 0x00, 0x04, 0x00, //flags + 0xEC, 0x0C, 0x00, 0x00, //id + 0x00, 0x00, 0x00, 0x00, //revision + 0x2B, 0x00, //version + 0x00, 0x00, //unknown + 0x42, 0x50, 0x54, 0x4E, //field type + 0x1D, 0x00, //field size + 0x19, 0x00, 0x00, 0x00, //decompressed field size + 0x75, 0xc5, 0x21, 0x0d, 0x00, 0x00, 0x08, 0x05, 0xd1, 0x6c, //field data (compressed) + 0x6c, 0xdc, 0x57, 0x48, 0x3c, 0xfd, 0x5b, 0x5c, 0x02, 0xd4, //field data (compressed) + 0x6b, 0x32, 0xb5, 0xdc, 0xa3, //field data (compressed) +];