use rewrite::RewriteContext;
use utils::{first_line_width, last_line_width, mk_sp};
-#[derive(Eq, PartialEq, Debug, Copy, Clone)]
/// Formatting tactic for lists. This will be cast down to a
-/// DefinitiveListTactic depending on the number and length of the items and
+/// `DefinitiveListTactic` depending on the number and length of the items and
/// their comments.
+#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum ListTactic {
// One item per row.
Vertical,
impl ListItem {
pub fn inner_as_ref(&self) -> &str {
- self.item.as_ref().map_or("", |s| &*s)
+ self.item.as_ref().map_or("", |s| s)
}
- pub fn is_multiline(&self) -> bool {
+ pub fn is_different_group(&self) -> bool {
self.inner_as_ref().contains('\n') || self.pre_comment.is_some() ||
self.post_comment
.as_ref()
.map_or(false, |s| s.contains('\n'))
}
+ pub fn is_multiline(&self) -> bool {
+ self.inner_as_ref().contains('\n') ||
+ self.pre_comment
+ .as_ref()
+ .map_or(false, |s| s.contains('\n')) ||
+ self.post_comment
+ .as_ref()
+ .map_or(false, |s| s.contains('\n'))
+ }
+
pub fn has_comment(&self) -> bool {
self.pre_comment
.as_ref()
}
}
-#[derive(Eq, PartialEq, Debug, Copy, Clone)]
/// The definitive formatting tactic for lists.
+#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum DefinitiveListTactic {
Vertical,
Horizontal,
}
/// The type of separator for lists.
-#[derive(Eq, PartialEq, Debug)]
+#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum Separator {
Comma,
VerticalBar,
// Now that we know how we will layout, we can decide for sure if there
// will be a trailing separator.
let mut trailing_separator = formatting.needs_trailing_separator();
- let mut result = String::new();
+ let mut result = String::with_capacity(128);
let cloned_items = items.clone();
let mut iter = items.into_iter().enumerate().peekable();
let mut item_max_width: Option<usize> = None;
if tactic == DefinitiveListTactic::Vertical {
// We cannot keep pre-comments on the same line if the comment if normalized.
- let keep_comment = if formatting.config.normalize_comments() {
- false
- } else if item.pre_comment_style == ListItemCommentStyle::DifferentLine {
+ let keep_comment = if formatting.config.normalize_comments() ||
+ item.pre_comment_style == ListItemCommentStyle::DifferentLine
+ {
false
} else {
// We will try to keep the comment on the same line with the item here.
formatting.config.max_width(),
));
}
- let overhead = if let &mut Some(max_width) = item_max_width {
+ let overhead = if let Some(max_width) = *item_max_width {
max_width + 2
} else {
// 1 = space between item and comment.
let item = item.as_ref();
let inner_item_width = item.inner_as_ref().len();
if !first &&
- (item.is_multiline() || !item.post_comment.is_some() ||
+ (item.is_different_group() || !item.post_comment.is_some() ||
inner_item_width + overhead > max_budget)
{
return max_width;
.chars()
.rev()
.take(comment_end + 1)
- .find(|c| *c == '\n')
- .is_some()
+ .any(|c| c == '\n')
{
(
Some(trimmed_pre_snippet.to_owned()),
}
None => post_snippet
.find_uncommented(self.terminator)
- .unwrap_or(post_snippet.len()),
+ .unwrap_or_else(|| post_snippet.len()),
};
if !post_snippet.is_empty() && comment_end > 0 {
// Everything from the separator to the next item.
let test_snippet = &post_snippet[comment_end - 1..];
- let first_newline = test_snippet.find('\n').unwrap_or(test_snippet.len());
+ let first_newline = test_snippet
+ .find('\n')
+ .unwrap_or_else(|| test_snippet.len());
// From the end of the first line of comments.
let test_snippet = &test_snippet[first_newline..];
let first = test_snippet
.find(|c: char| !c.is_whitespace())
- .unwrap_or(test_snippet.len());
+ .unwrap_or_else(|| test_snippet.len());
// From the end of the first line of comments to the next non-whitespace char.
let test_snippet = &test_snippet[..first];
self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);
let post_snippet = post_snippet[..comment_end].trim();
- let post_snippet_trimmed = if post_snippet.starts_with(',') {
+ let post_snippet_trimmed = if post_snippet.starts_with(|c| c == ',' || c == ':') {
post_snippet[1..].trim_matches(white_space)
} else if post_snippet.ends_with(',') {
post_snippet[..(post_snippet.len() - 1)].trim_matches(white_space)
IndentStyle::Block => {
let shape = shape.block_indent(context.config.tab_spaces());
Shape {
- width: try_opt!(context.config.max_width().checked_sub(shape.indent.width())),
+ width: context.budget(shape.indent.width()),
..shape
}
}