diff --git a/src/visitor.rs b/src/visitor.rs index a75b60a7bd7..a7198bf588b 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -26,6 +26,7 @@ use comment::rewrite_comment; use macros::{rewrite_macro, MacroPosition}; use items::{rewrite_static, rewrite_associated_type, rewrite_associated_impl_type, rewrite_type_alias, format_impl, format_trait}; +use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorTactic}; fn is_use_item(item: &ast::Item) -> bool { match item.node { @@ -637,6 +638,81 @@ impl<'a> FmtVisitor<'a> { } } +impl Rewrite for ast::NestedMetaItem { + fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + match self.node { + ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape), + ast::NestedMetaItemKind::Literal(..) => Some(context.snippet(self.span)), + } + } +} + +impl Rewrite for ast::MetaItem { + fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + Some(match self.node { + ast::MetaItemKind::Word => String::from(&*self.name.as_str()), + ast::MetaItemKind::List(ref list) => { + let name = self.name.as_str(); + // 3 = `#[` and `(`, 2 = `]` and `)` + let item_shape = try_opt!(shape + .shrink_left(name.len() + 3) + .and_then(|s| s.sub_width(2))); + let items = itemize_list(context.codemap, + list.iter(), + ")", + |nested_meta_item| nested_meta_item.span.lo, + |nested_meta_item| nested_meta_item.span.hi, + |nested_meta_item| { + nested_meta_item.rewrite(context, item_shape) + }, + self.span.lo, + self.span.hi); + let item_vec = items.collect::>(); + let fmt = ListFormatting { + tactic: DefinitiveListTactic::Mixed, + separator: ",", + trailing_separator: SeparatorTactic::Never, + shape: item_shape, + ends_with_newline: false, + config: context.config, + }; + format!("{}({})", name, try_opt!(write_list(&item_vec, &fmt))) + } + ast::MetaItemKind::NameValue(ref literal) => { + let name = self.name.as_str(); + let value = context.snippet(literal.span); + if &*name == "doc" && value.starts_with("///") { + let doc_shape = Shape { + width: cmp::min(shape.width, context.config.comment_width()) + .checked_sub(shape.indent.width()) + .unwrap_or(0), + ..shape + }; + format!("{}", + try_opt!(rewrite_comment(&value, + false, + doc_shape, + context.config))) + } else { + format!("{} = {}", name, value) + } + } + }) + } +} + +impl Rewrite for ast::Attribute { + fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { + self.value + .rewrite(context, shape) + .map(|rw| if rw.starts_with("///") { + rw + } else { + format!("#[{}]", rw) + }) + } +} + impl<'a> Rewrite for [ast::Attribute] { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { let mut result = String::new(); @@ -646,7 +722,7 @@ impl<'a> Rewrite for [ast::Attribute] { let indent = shape.indent.to_string(context.config); for (i, a) in self.iter().enumerate() { - let mut a_str = context.snippet(a.span); + let a_str = try_opt!(a.rewrite(context, shape)); // Write comments and blank lines between attributes. if i > 0 { @@ -673,15 +749,6 @@ impl<'a> Rewrite for [ast::Attribute] { result.push_str(&indent); } - if a_str.starts_with("//") { - a_str = try_opt!(rewrite_comment(&a_str, - false, - Shape::legacy(context.config.comment_width() - - shape.indent.width(), - shape.indent), - context.config)); - } - // Write the attribute itself. result.push_str(&a_str); diff --git a/tests/source/attrib.rs b/tests/source/attrib.rs index a47c2507156..593fb0ff57e 100644 --- a/tests/source/attrib.rs +++ b/tests/source/attrib.rs @@ -41,4 +41,14 @@ impl Bar { /// Blah blah bing. fn f4(self) -> Cat { } + + // We want spaces around `=` + #[cfg(feature="nightly")] + fn f5(self) -> Monkey {} +} + +// #984 +struct Foo { + # [ derive ( Clone , PartialEq , Debug , Deserialize , Serialize ) ] + foo: usize, } diff --git a/tests/source/enum.rs b/tests/source/enum.rs index 0e2e2c066c3..6992ca53c1b 100644 --- a/tests/source/enum.rs +++ b/tests/source/enum.rs @@ -109,3 +109,14 @@ pub enum Bencoding<'i> { // TODO make Dict "structlike" AKA name the two values. Dict(&'i [u8], BTreeMap<&'i [u8], Bencoding<'i>>), } + +// #1261 +pub enum CoreResourceMsg { + SetCookieForUrl( + ServoUrl, + #[serde(deserialize_with = "::hyper_serde::deserialize", + serialize_with = "::hyper_serde::serialize")] + Cookie, + CookieSource + ), +} diff --git a/tests/source/struct-field-attributes.rs b/tests/source/struct-field-attributes.rs index 2e5381a7e41..6fc69c2dfe4 100644 --- a/tests/source/struct-field-attributes.rs +++ b/tests/source/struct-field-attributes.rs @@ -20,3 +20,18 @@ fn do_something() -> Foo { fn main() { do_something(); } + +// #1462 +struct Foo { + foo: usize, + #[cfg(feature="include-bar")] + bar: usize, +} + +fn new_foo() -> Foo { + Foo { + foo: 0, + #[cfg(feature="include-bar")] + bar: 0, + } +} diff --git a/tests/target/attrib.rs b/tests/target/attrib.rs index b861b971d9b..fb70585bd21 100644 --- a/tests/target/attrib.rs +++ b/tests/target/attrib.rs @@ -37,4 +37,14 @@ impl Bar { // tooooooooooooooooooooooooooooooo loooooooooooong. /// Blah blah bing. fn f4(self) -> Cat {} + + // We want spaces around `=` + #[cfg(feature = "nightly")] + fn f5(self) -> Monkey {} +} + +// #984 +struct Foo { + #[derive(Clone, PartialEq, Debug, Deserialize, Serialize)] + foo: usize, } diff --git a/tests/target/enum.rs b/tests/target/enum.rs index 3e0e46d3fae..0bb18e2df67 100644 --- a/tests/target/enum.rs +++ b/tests/target/enum.rs @@ -139,3 +139,12 @@ pub enum Bencoding<'i> { // TODO make Dict "structlike" AKA name the two values. Dict(&'i [u8], BTreeMap<&'i [u8], Bencoding<'i>>), } + +// #1261 +pub enum CoreResourceMsg { + SetCookieForUrl(ServoUrl, + #[serde(deserialize_with = "::hyper_serde::deserialize", + serialize_with = "::hyper_serde::serialize")] + Cookie, + CookieSource), +} diff --git a/tests/target/nestedmod/mod.rs b/tests/target/nestedmod/mod.rs index b3456bf0d0f..ff0d55b0175 100644 --- a/tests/target/nestedmod/mod.rs +++ b/tests/target/nestedmod/mod.rs @@ -7,7 +7,7 @@ mod mymod1 { mod mod3a; } -#[path="mod2c.rs"] +#[path = "mod2c.rs"] mod mymod2; mod submod2; diff --git a/tests/target/nestedmod/mod2b.rs b/tests/target/nestedmod/mod2b.rs index f128e2da6db..f06766f304f 100644 --- a/tests/target/nestedmod/mod2b.rs +++ b/tests/target/nestedmod/mod2b.rs @@ -1,3 +1,3 @@ -#[path="mod2a.rs"] +#[path = "mod2a.rs"] mod c; diff --git a/tests/target/struct-field-attributes.rs b/tests/target/struct-field-attributes.rs index 2e5381a7e41..8ae40ac9bdf 100644 --- a/tests/target/struct-field-attributes.rs +++ b/tests/target/struct-field-attributes.rs @@ -20,3 +20,18 @@ fn do_something() -> Foo { fn main() { do_something(); } + +// #1462 +struct Foo { + foo: usize, + #[cfg(feature = "include-bar")] + bar: usize, +} + +fn new_foo() -> Foo { + Foo { + foo: 0, + #[cfg(feature = "include-bar")] + bar: 0, + } +}