Merge pull request #2766 from topecongiro/issue-2761

Allow using mixed layout with comments
This commit is contained in:
Nick Cameron 2018-06-05 18:03:51 +12:00 committed by GitHub
commit 9f535de503
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 6 deletions

View File

@ -228,6 +228,7 @@ pub fn write_list<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
let mut item_max_width: Option<usize> = 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<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
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<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
// 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<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
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<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
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<I, T>(items: I, formatting: &ListFormatting) -> Option<String>
item_max_width = None;
result.push('\n');
}
prev_item_had_post_comment = item.post_comment.is_some();
}
Some(result)

View File

@ -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)
}

View File

@ -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)
];

View File

@ -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)
];