|
10 | 10 |
|
11 | 11 | //! Format attributes and meta items.
|
12 | 12 |
|
| 13 | +use config::IndentStyle; |
13 | 14 | use config::lists::*;
|
14 | 15 | use syntax::ast;
|
15 | 16 | use syntax::codemap::Span;
|
@@ -202,11 +203,16 @@ impl Rewrite for ast::MetaItem {
|
202 | 203 | ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
|
203 | 204 | ast::MetaItemKind::List(ref list) => {
|
204 | 205 | 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 | + }; |
210 | 216 | let items = itemize_list(
|
211 | 217 | context.snippet_provider,
|
212 | 218 | list.iter(),
|
@@ -240,7 +246,17 @@ impl Rewrite for ast::MetaItem {
|
240 | 246 | preserve_newline: false,
|
241 | 247 | config: context.config,
|
242 | 248 | };
|
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 | + } |
244 | 260 | }
|
245 | 261 | ast::MetaItemKind::NameValue(ref literal) => {
|
246 | 262 | let name = self.name.as_str();
|
|
0 commit comments