use config::lists::*;
use syntax::ast;
use syntax::codemap::Span;
+use syntax::parse::token::DelimToken;
use closures;
use codemap::SpanUtils;
-use expr::{is_nested_call, maybe_get_args_offset, ToExpr};
+use expr::{is_every_expr_simple, is_nested_call, maybe_get_args_offset, ToExpr};
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use std::cmp::min;
+const SHORT_ITEM_THRESHOLD: usize = 10;
+
pub fn rewrite_with_parens<T>(
context: &RewriteContext,
ident: &str,
")",
item_max_width,
force_separator_tactic,
+ None,
).rewrite(shape)
}
">",
context.config.max_width(),
None,
+ None,
+ ).rewrite(shape)
+}
+
+pub fn rewrite_with_square_brackets<T>(
+ context: &RewriteContext,
+ name: &str,
+ items: &[&T],
+ shape: Shape,
+ span: Span,
+ force_separator_tactic: Option<SeparatorTactic>,
+ delim_token: Option<DelimToken>,
+) -> Option<String>
+where
+ T: Rewrite + ToExpr + Spanned,
+{
+ let (lhs, rhs) = match delim_token {
+ Some(DelimToken::Paren) => ("(", ")"),
+ Some(DelimToken::Brace) => ("{", "}"),
+ _ => ("[", "]"),
+ };
+ Context::new(
+ context,
+ items,
+ name,
+ shape,
+ span,
+ lhs,
+ rhs,
+ context.config.width_heuristics().array_width,
+ force_separator_tactic,
+ Some(("[", "]")),
).rewrite(shape)
}
item_max_width: usize,
one_line_width: usize,
force_separator_tactic: Option<SeparatorTactic>,
+ custom_delims: Option<(&'a str, &'a str)>,
}
impl<'a, T: 'a + Rewrite + ToExpr + Spanned> Context<'a, T> {
suffix: &'static str,
item_max_width: usize,
force_separator_tactic: Option<SeparatorTactic>,
+ custom_delims: Option<(&'a str, &'a str)>,
) -> Context<'a, T> {
// 2 = `( `, 1 = `(`
let paren_overhead = if context.config.spaces_within_parens_and_brackets() {
item_max_width,
one_line_width,
force_separator_tactic,
+ custom_delims,
}
}
if one_line {
tactic = DefinitiveListTactic::SpecialMacro(num_args_before);
};
+ } else if is_every_expr_simple(self.items) && no_long_items(list_items) {
+ tactic = DefinitiveListTactic::Mixed;
}
}
}
tactic
} else if !self.context.use_block_indent() {
SeparatorTactic::Never
+ } else if tactic == DefinitiveListTactic::Mixed {
+ // We are using mixed layout because everything did not fit within a single line.
+ SeparatorTactic::Always
} else {
self.context.config.trailing_comma()
},
separator_place: SeparatorPlace::Back,
shape: self.nested_shape,
- ends_with_newline: self.context.use_block_indent()
- && tactic == DefinitiveListTactic::Vertical,
+ ends_with_newline: match tactic {
+ DefinitiveListTactic::Vertical | DefinitiveListTactic::Mixed => {
+ self.context.use_block_indent()
+ }
+ _ => false,
+ },
preserve_newline: false,
config: self.context.config,
};
..shape
};
+ let (prefix, suffix) = match self.custom_delims {
+ Some((lhs, rhs)) => (lhs, rhs),
+ _ => (self.prefix, self.suffix),
+ };
let paren_overhead = paren_overhead(self.context);
let fits_one_line = items_str.len() + paren_overhead <= shape.width;
let extend_width = if items_str.is_empty() {
self.ident.len() + items_str.len() + 2 + indent_str.len() + nested_indent_str.len(),
);
result.push_str(self.ident);
- result.push_str(self.prefix);
+ result.push_str(prefix);
if !self.context.use_block_indent()
|| (self.context.inside_macro() && !items_str.contains('\n') && fits_one_line)
|| (is_extendable && extend_width <= shape.width)
}
result.push_str(&indent_str);
}
- result.push_str(self.suffix);
+ result.push_str(suffix);
result
}
}
}
}
+
+fn no_long_items(list: &[ListItem]) -> bool {
+ list.iter()
+ .all(|item| !item.has_comment() && item.inner_as_ref().len() <= SHORT_ITEM_THRESHOLD)
+}