Follow indent style config when formatting attrs

This commit is contained in:
Seiichi Uchida 2018-03-28 16:56:39 +09:00
parent c77708ff9a
commit efd295a4e1

View file

@ -10,6 +10,7 @@
//! Format attributes and meta items.
use config::IndentStyle;
use config::lists::*;
use syntax::ast;
use syntax::codemap::Span;
@ -202,11 +203,16 @@ impl Rewrite for ast::MetaItem {
ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
ast::MetaItemKind::List(ref list) => {
let name = self.name.as_str();
// 1 = `(`, 2 = `]` and `)`
let item_shape = shape
.visual_indent(0)
.shrink_left(name.len() + 1)
.and_then(|s| s.sub_width(2))?;
let item_shape = match context.config.indent_style() {
IndentStyle::Block => shape
.block_indent(context.config.tab_spaces())
.with_max_width(context.config),
// 1 = `(`, 2 = `]` and `)`
IndentStyle::Visual => shape
.visual_indent(0)
.shrink_left(name.len() + 1)
.and_then(|s| s.sub_width(2))?,
};
let items = itemize_list(
context.snippet_provider,
list.iter(),
@ -240,7 +246,17 @@ impl Rewrite for ast::MetaItem {
preserve_newline: false,
config: context.config,
};
format!("{}({})", name, write_list(&item_vec, &fmt)?)
let item_str = write_list(&item_vec, &fmt)?;
let one_line_budget = shape.offset_left(name.len())?.sub_width(2)?.width;
if context.config.indent_style() == IndentStyle::Visual
|| (!item_str.contains('\n') && item_str.len() <= one_line_budget)
{
format!("{}({})", name, item_str)
} else {
let indent = shape.indent.to_string_with_newline(context.config);
let nested_indent = item_shape.indent.to_string_with_newline(context.config);
format!("{}({}{}{})", name, nested_indent, item_str, indent)
}
}
ast::MetaItemKind::NameValue(ref literal) => {
let name = self.name.as_str();