pub fn has_comment(&self) -> bool {
self.pre_comment
.as_ref()
- .map_or(false, |comment| comment.starts_with("//"))
+ .map_or(false, |comment| comment.trim_left().starts_with("//"))
|| self.post_comment
.as_ref()
- .map_or(false, |comment| comment.starts_with("//"))
+ .map_or(false, |comment| comment.trim_left().starts_with("//"))
}
pub fn from_str<S: Into<String>>(s: S) -> ListItem {
Vertical,
Horizontal,
Mixed,
+ // Special case tactic for `format!()` variants.
+ FormatCall,
+ // Special case tactic for `write!()` varianta.
+ WriteCall,
}
impl DefinitiveListTactic {
*self == SeparatorPlace::Back
}
- pub fn from_tactic(default: SeparatorPlace, tactic: DefinitiveListTactic) -> SeparatorPlace {
+ pub fn from_tactic(
+ default: SeparatorPlace,
+ tactic: DefinitiveListTactic,
+ sep: &str,
+ ) -> SeparatorPlace {
match tactic {
DefinitiveListTactic::Vertical => default,
- _ => SeparatorPlace::Back,
+ _ => if sep == "," {
+ SeparatorPlace::Back
+ } else {
+ default
+ },
}
}
}
I: IntoIterator<Item = T> + Clone,
T: AsRef<ListItem>,
{
- let tactic = formatting.tactic;
+ let mut tactic = formatting.tactic;
let sep_len = formatting.separator.len();
// Now that we know how we will layout, we can decide for sure if there
let cloned_items = items.clone();
let mut iter = items.into_iter().enumerate().peekable();
let mut item_max_width: Option<usize> = None;
- let mut sep_place = SeparatorPlace::from_tactic(formatting.separator_place, tactic);
+ let sep_place =
+ SeparatorPlace::from_tactic(formatting.separator_place, tactic, formatting.separator);
let mut line_len = 0;
let indent_str = &formatting.shape.indent.to_string(formatting.config);
let inner_item = item.item.as_ref()?;
let first = i == 0;
let last = iter.peek().is_none();
- let mut separate = !last || trailing_separator;
+ let mut separate = match sep_place {
+ SeparatorPlace::Front => !first,
+ SeparatorPlace::Back => !last || trailing_separator,
+ };
let item_sep_len = if separate { sep_len } else { 0 };
// Item string may be multi-line. Its length (used for block comment alignment)
DefinitiveListTactic::Horizontal if !first => {
result.push(' ');
}
+ DefinitiveListTactic::FormatCall if !first => {
+ result.push('\n');
+ result.push_str(indent_str);
+ tactic = DefinitiveListTactic::Horizontal;
+ }
+ DefinitiveListTactic::WriteCall => {
+ let second = i == 1;
+ let third = i == 2;
+
+ if first {
+ // Nothing
+ } else if second {
+ result.push('\n');
+ result.push_str(indent_str);
+ } else if third {
+ result.push('\n');
+ result.push_str(indent_str);
+ tactic = DefinitiveListTactic::Horizontal;
+ } else {
+ unreachable!();
+ }
+ }
DefinitiveListTactic::Vertical if !first => {
result.push('\n');
result.push_str(indent_str);
trailing_separator = true;
}
}
- sep_place = formatting.separator_place;
- } else {
- sep_place = SeparatorPlace::Back;
}
if line_len > 0 {
formatting.config.max_width(),
));
}
- let overhead = if let Some(max_width) = *item_max_width {
+ let overhead = if starts_with_newline(comment) {
+ 0
+ } else if let Some(max_width) = *item_max_width {
max_width + 2
} else {
// 1 = space between item and comment.
|| comment.trim().contains('\n')
|| comment.trim().len() > width;
- rewrite_comment(comment, block_style, comment_shape, formatting.config)
+ rewrite_comment(
+ comment.trim_left(),
+ block_style,
+ comment_shape,
+ formatting.config,
+ )
};
let mut formatted_comment = rewrite_post_comment(&mut item_max_width)?;
- if !starts_with_newline(&formatted_comment) {
+ if !starts_with_newline(comment) {
let mut comment_alignment =
post_comment_alignment(item_max_width, inner_item.len());
if first_line_width(&formatted_comment) + last_line_width(&result)
{
result.push(' ');
}
+ } else {
+ result.push('\n');
+ result.push_str(indent_str);
}
if formatted_comment.contains('\n') {
item_max_width = None;
prev_span_end: BytePos,
next_span_start: BytePos,
terminator: &'a str,
+ separator: &'a str,
leave_last: bool,
}
let mut block_open_index = post_snippet.find("/*");
// check if it really is a block comment (and not `//*` or a nested comment)
if let Some(i) = block_open_index {
- match post_snippet.find("/") {
+ match post_snippet.find('/') {
Some(j) if j < i => block_open_index = None,
_ if i > 0 && &post_snippet[i - 1..i] == "/" => block_open_index = None,
_ => (),
}
}
let newline_index = post_snippet.find('\n');
- if let Some(separator_index) = post_snippet.find_uncommented(",") {
+ if let Some(separator_index) = post_snippet.find_uncommented(self.separator) {
match (block_open_index, newline_index) {
// Separator before comment, with the next item on same line.
// Comment belongs to next item.
(_, Some(j)) if j > separator_index => j + 1,
_ => post_snippet.len(),
}
- } else {
+ } else if let Some(newline_index) = newline_index {
// Match arms may not have trailing comma. In any case, for match arms,
// we will assume that the post comment belongs to the next arm if they
// do not end with trailing comma.
- if let Some(newline_index) = newline_index {
- newline_index + 1
- } else {
- 0
- }
+ newline_index + 1
+ } else {
+ 0
}
}
None => post_snippet
codemap: &'a CodeMap,
inner: I,
terminator: &'a str,
+ separator: &'a str,
get_lo: F1,
get_hi: F2,
get_item_string: F3,
prev_span_end: prev_span_end,
next_span_start: next_span_start,
terminator: terminator,
+ separator: separator,
leave_last: leave_last,
}
}
};
let shape_width = shape.width.checked_sub(prefix_width + suffix_width);
if let Some(w) = shape_width {
- let shape_width = cmp::min(w, context.config.struct_lit_width());
+ let shape_width = cmp::min(w, context.config.width_heuristics().struct_lit_width);
Some((Some(Shape::legacy(shape_width, shape.indent)), v_shape))
} else {
Some((None, v_shape))
if let Some(h_shape) = h_shape {
let prelim_tactic = match (context.config.indent_style(), items.len()) {
(IndentStyle::Visual, 1) => ListTactic::HorizontalVertical,
- _ => context.config.struct_lit_multiline_style().to_list_tactic(),
+ _ if context.config.struct_lit_single_line() => ListTactic::HorizontalVertical,
+ _ => ListTactic::Vertical,
};
definitive_tactic(items, prelim_tactic, Separator::Comma, h_shape.width)
} else {