Skip to content

Commit efd295a

Browse files
committed
Follow indent style config when formatting attrs
1 parent c77708f commit efd295a

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/attr.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
//! Format attributes and meta items.
1212
13+
use config::IndentStyle;
1314
use config::lists::*;
1415
use syntax::ast;
1516
use syntax::codemap::Span;
@@ -202,11 +203,16 @@ impl Rewrite for ast::MetaItem {
202203
ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
203204
ast::MetaItemKind::List(ref list) => {
204205
let name = self.name.as_str();
205-
// 1 = `(`, 2 = `]` and `)`
206-
let item_shape = shape
207-
.visual_indent(0)
208-
.shrink_left(name.len() + 1)
209-
.and_then(|s| s.sub_width(2))?;
206+
let item_shape = match context.config.indent_style() {
207+
IndentStyle::Block => shape
208+
.block_indent(context.config.tab_spaces())
209+
.with_max_width(context.config),
210+
// 1 = `(`, 2 = `]` and `)`
211+
IndentStyle::Visual => shape
212+
.visual_indent(0)
213+
.shrink_left(name.len() + 1)
214+
.and_then(|s| s.sub_width(2))?,
215+
};
210216
let items = itemize_list(
211217
context.snippet_provider,
212218
list.iter(),
@@ -240,7 +246,17 @@ impl Rewrite for ast::MetaItem {
240246
preserve_newline: false,
241247
config: context.config,
242248
};
243-
format!("{}({})", name, write_list(&item_vec, &fmt)?)
249+
let item_str = write_list(&item_vec, &fmt)?;
250+
let one_line_budget = shape.offset_left(name.len())?.sub_width(2)?.width;
251+
if context.config.indent_style() == IndentStyle::Visual
252+
|| (!item_str.contains('\n') && item_str.len() <= one_line_budget)
253+
{
254+
format!("{}({})", name, item_str)
255+
} else {
256+
let indent = shape.indent.to_string_with_newline(context.config);
257+
let nested_indent = item_shape.indent.to_string_with_newline(context.config);
258+
format!("{}({}{}{})", name, nested_indent, item_str, indent)
259+
}
244260
}
245261
ast::MetaItemKind::NameValue(ref literal) => {
246262
let name = self.name.as_str();

0 commit comments

Comments
 (0)