use config::lists::*;
use config::IndentStyle;
use expr::rewrite_literal;
-use lists::{itemize_list, write_list, ListFormatting};
+use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use rewrite::{Rewrite, RewriteContext};
use shape::Shape;
use types::{rewrite_path, PathContext};
use std::borrow::Cow;
use syntax::ast;
-use syntax::codemap::{BytePos, Span, DUMMY_SP};
+use syntax::source_map::{BytePos, Span, DUMMY_SP};
/// Returns attributes on the given statement.
pub fn get_attrs_from_stmt(stmt: &ast::Stmt) -> &[ast::Attribute] {
}
/// Returns the arguments of `#[derive(...)]`.
-fn get_derive_spans<'a>(attr: &ast::Attribute) -> Option<Vec<Span>> {
+fn get_derive_spans(attr: &ast::Attribute) -> Option<Vec<Span>> {
attr.meta_item_list().map(|meta_item_list| {
meta_item_list
.iter()
fn argument_shape(
left: usize,
right: usize,
+ combine: bool,
shape: Shape,
context: &RewriteContext,
) -> Option<Shape> {
match context.config.indent_style() {
- IndentStyle::Block => Some(
- shape
- .block_indent(context.config.tab_spaces())
- .with_max_width(context.config),
- ),
+ IndentStyle::Block => {
+ if combine {
+ shape.offset_left(left)
+ } else {
+ Some(
+ shape
+ .block_indent(context.config.tab_spaces())
+ .with_max_width(context.config),
+ )
+ }
+ }
IndentStyle::Visual => shape
.visual_indent(0)
.shrink_left(left)
result.push_str(prefix);
result.push_str("[derive(");
- let argument_shape = argument_shape(10 + prefix.len(), 2, shape, context)?;
+ let argument_shape = argument_shape(10 + prefix.len(), 2, false, shape, context)?;
let item_str = format_arg_list(
derive_args.iter(),
|_| DUMMY_SP.lo(),
// 10 = "[derive()]", 3 = "()" and "]"
shape.offset_left(10 + prefix.len())?.sub_width(3)?,
None,
+ false,
)?;
result.push_str(&item_str);
ast::MetaItemKind::List(ref list) => {
let path = rewrite_path(context, PathContext::Type, None, &self.ident, shape)?;
- let snippet = context.snippet(self.span);
- // 2 = )] (this might go wrong if there is whitespace between the brackets, but
- // it's close enough).
- let snippet = snippet[..snippet.len() - 2].trim();
- let trailing_comma = if snippet.ends_with(',') { "," } else { "" };
+ let has_comma = ::expr::span_ends_with_comma(context, self.span);
+ let trailing_comma = if has_comma { "," } else { "" };
+ let combine = list.len() == 1 && match list[0].node {
+ ast::NestedMetaItemKind::Literal(..) => false,
+ ast::NestedMetaItemKind::MetaItem(ref inner_meta_item) => {
+ match inner_meta_item.node {
+ ast::MetaItemKind::List(..) => rewrite_path(
+ context,
+ PathContext::Type,
+ None,
+ &inner_meta_item.ident,
+ shape,
+ ).map_or(false, |s| s.len() + path.len() + 2 <= shape.width),
+ _ => false,
+ }
+ }
+ };
- let argument_shape =
- argument_shape(path.len() + 1, 2 + trailing_comma.len(), shape, context)?;
+ let argument_shape = argument_shape(
+ path.len() + 1,
+ 2 + trailing_comma.len(),
+ combine,
+ shape,
+ context,
+ )?;
let item_str = format_arg_list(
list.iter(),
|nested_meta_item| nested_meta_item.span.lo(),
.offset_left(path.len())?
.sub_width(3 + trailing_comma.len())?,
Some(context.config.width_heuristics().fn_call_width),
+ combine,
)?;
let indent = if item_str.starts_with('\n') {
shape: Shape,
one_line_shape: Shape,
one_line_limit: Option<usize>,
+ combine: bool,
) -> Option<String>
where
I: Iterator<Item = T>,
ListTactic::HorizontalVertical
};
- let tactic =
- ::lists::definitive_tactic(&item_vec, tactic, ::lists::Separator::Comma, shape.width);
+ let tactic = definitive_tactic(&item_vec, tactic, Separator::Comma, shape.width);
let fmt = ListFormatting::new(shape, context.config)
.tactic(tactic)
.ends_with_newline(false);
let one_line_budget = one_line_shape.width;
if context.config.indent_style() == IndentStyle::Visual
+ || combine
|| (!item_str.contains('\n') && item_str.len() <= one_line_budget)
{
Some(item_str)