Skip to content

Commit 1644b17

Browse files
authored
Merge pull request #2557 from topecongiro/vertical-layout-complex-attrs
Use vertical layout for complex attributes
2 parents 8dd08dd + e5b403c commit 1644b17

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

src/attr.rs

Lines changed: 46 additions & 7 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;
@@ -183,17 +184,35 @@ fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
183184
(if mlb { "\n" } else { "" }, if mla { "\n" } else { "" })
184185
}
185186

187+
fn allow_mixed_tactic_for_nested_metaitem_list(list: &[ast::NestedMetaItem]) -> bool {
188+
list.iter().all(|nested_metaitem| {
189+
if let ast::NestedMetaItemKind::MetaItem(ref inner_metaitem) = nested_metaitem.node {
190+
match inner_metaitem.node {
191+
ast::MetaItemKind::List(..) | ast::MetaItemKind::NameValue(..) => false,
192+
_ => true,
193+
}
194+
} else {
195+
true
196+
}
197+
})
198+
}
199+
186200
impl Rewrite for ast::MetaItem {
187201
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
188202
Some(match self.node {
189203
ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
190204
ast::MetaItemKind::List(ref list) => {
191205
let name = self.name.as_str();
192-
// 1 = `(`, 2 = `]` and `)`
193-
let item_shape = shape
194-
.visual_indent(0)
195-
.shrink_left(name.len() + 1)
196-
.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+
};
197216
let items = itemize_list(
198217
context.snippet_provider,
199218
list.iter(),
@@ -207,8 +226,18 @@ impl Rewrite for ast::MetaItem {
207226
false,
208227
);
209228
let item_vec = items.collect::<Vec<_>>();
229+
let tactic = if allow_mixed_tactic_for_nested_metaitem_list(list) {
230+
DefinitiveListTactic::Mixed
231+
} else {
232+
::lists::definitive_tactic(
233+
&item_vec,
234+
ListTactic::HorizontalVertical,
235+
::lists::Separator::Comma,
236+
item_shape.width,
237+
)
238+
};
210239
let fmt = ListFormatting {
211-
tactic: DefinitiveListTactic::Mixed,
240+
tactic,
212241
separator: ",",
213242
trailing_separator: SeparatorTactic::Never,
214243
separator_place: SeparatorPlace::Back,
@@ -217,7 +246,17 @@ impl Rewrite for ast::MetaItem {
217246
preserve_newline: false,
218247
config: context.config,
219248
};
220-
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+
}
221260
}
222261
ast::MetaItemKind::NameValue(ref literal) => {
223262
let name = self.name.as_str();

tests/source/attrib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// rustfmt-wrap_comments: true
22
// Test attributes and doc comments are preserved.
3+
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
4+
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
5+
html_root_url = "https://doc.rust-lang.org/nightly/",
6+
html_playground_url = "https://play.rust-lang.org/", test(attr(deny(warnings))))]
37

48
//! Doc comment
59

tests/target/attrib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// rustfmt-wrap_comments: true
22
// Test attributes and doc comments are preserved.
3+
#![doc(
4+
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
5+
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
6+
html_root_url = "https://doc.rust-lang.org/nightly/",
7+
html_playground_url = "https://play.rust-lang.org/",
8+
test(attr(deny(warnings)))
9+
)]
310

411
//! Doc comment
512

tests/target/enum.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,10 @@ pub enum Bencoding<'i> {
156156
pub enum CoreResourceMsg {
157157
SetCookieForUrl(
158158
ServoUrl,
159-
#[serde(deserialize_with = "::hyper_serde::deserialize",
160-
serialize_with = "::hyper_serde::serialize")]
159+
#[serde(
160+
deserialize_with = "::hyper_serde::deserialize",
161+
serialize_with = "::hyper_serde::serialize"
162+
)]
161163
Cookie,
162164
CookieSource,
163165
),
@@ -221,7 +223,9 @@ enum State {
221223
// #2190
222224
#[derive(Debug, Fail)]
223225
enum AnError {
224-
#[fail(display = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")]
226+
#[fail(
227+
display = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
228+
)]
225229
UnexpectedSingleToken { token: syn::Token },
226230
}
227231

0 commit comments

Comments
 (0)