Skip to content

Commit 1f1777c

Browse files
committed
Implement Rewrite trait for syntax::ast::Attribute
1 parent d04ab9e commit 1f1777c

File tree

9 files changed

+149
-12
lines changed

9 files changed

+149
-12
lines changed

src/visitor.rs

+77-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use comment::rewrite_comment;
2626
use macros::{rewrite_macro, MacroPosition};
2727
use items::{rewrite_static, rewrite_associated_type, rewrite_associated_impl_type,
2828
rewrite_type_alias, format_impl, format_trait};
29+
use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorTactic};
2930

3031
fn is_use_item(item: &ast::Item) -> bool {
3132
match item.node {
@@ -637,6 +638,81 @@ impl<'a> FmtVisitor<'a> {
637638
}
638639
}
639640

641+
impl Rewrite for ast::NestedMetaItem {
642+
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
643+
match self.node {
644+
ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
645+
ast::NestedMetaItemKind::Literal(..) => Some(context.snippet(self.span)),
646+
}
647+
}
648+
}
649+
650+
impl Rewrite for ast::MetaItem {
651+
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
652+
Some(match self.node {
653+
ast::MetaItemKind::Word => context.snippet(self.span),
654+
ast::MetaItemKind::List(ref list) => {
655+
let name = self.name.as_str();
656+
// 3 = `#[` and `(`, 2 = `]` and `)`
657+
let item_shape = try_opt!(shape
658+
.shrink_left(name.len() + 3)
659+
.and_then(|s| s.sub_width(1)));
660+
let items = itemize_list(context.codemap,
661+
list.iter(),
662+
")",
663+
|nested_meta_item| nested_meta_item.span.lo,
664+
|nested_meta_item| nested_meta_item.span.hi,
665+
|nested_meta_item| {
666+
nested_meta_item.rewrite(context, item_shape)
667+
},
668+
self.span.lo,
669+
self.span.hi);
670+
let item_vec = items.collect::<Vec<_>>();
671+
let fmt = ListFormatting {
672+
tactic: DefinitiveListTactic::Mixed,
673+
separator: ",",
674+
trailing_separator: SeparatorTactic::Never,
675+
shape: item_shape,
676+
ends_with_newline: false,
677+
config: context.config,
678+
};
679+
format!("{}({})", name, try_opt!(write_list(&item_vec, &fmt)))
680+
}
681+
ast::MetaItemKind::NameValue(ref literal) => {
682+
let name = self.name.as_str();
683+
let value = context.snippet(literal.span);
684+
if &*name == "doc" && value.starts_with("///") {
685+
let doc_shape = Shape {
686+
width: cmp::min(shape.width, context.config.comment_width())
687+
.checked_sub(shape.indent.width())
688+
.unwrap_or(0),
689+
..shape
690+
};
691+
format!("{}",
692+
try_opt!(rewrite_comment(&value,
693+
false,
694+
doc_shape,
695+
context.config)))
696+
} else {
697+
format!("{} = {}", name, value)
698+
}
699+
}
700+
})
701+
}
702+
}
703+
704+
impl Rewrite for ast::Attribute {
705+
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
706+
self.value
707+
.rewrite(context, shape)
708+
.map(|rw| if rw.starts_with("///") {
709+
rw
710+
} else {
711+
format!("#[{}]", rw)
712+
})
713+
}
714+
}
715+
640716
impl<'a> Rewrite for [ast::Attribute] {
641717
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
642718
let mut result = String::new();
@@ -646,7 +722,7 @@ impl<'a> Rewrite for [ast::Attribute] {
646722
let indent = shape.indent.to_string(context.config);
647723

648724
for (i, a) in self.iter().enumerate() {
649-
let mut a_str = context.snippet(a.span);
725+
let a_str = try_opt!(a.rewrite(context, shape));
650726

651727
// Write comments and blank lines between attributes.
652728
if i > 0 {
@@ -673,15 +749,6 @@ impl<'a> Rewrite for [ast::Attribute] {
673749
result.push_str(&indent);
674750
}
675751

676-
if a_str.starts_with("//") {
677-
a_str = try_opt!(rewrite_comment(&a_str,
678-
false,
679-
Shape::legacy(context.config.comment_width() -
680-
shape.indent.width(),
681-
shape.indent),
682-
context.config));
683-
}
684-
685752
// Write the attribute itself.
686753
result.push_str(&a_str);
687754

tests/source/attrib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,14 @@ impl Bar {
4141
/// Blah blah bing.
4242
fn f4(self) -> Cat {
4343
}
44+
45+
// We want spaces around `=`
46+
#[cfg(feature="nightly")]
47+
fn f5(self) -> Monkey {}
48+
}
49+
50+
// #984
51+
struct Foo {
52+
# [ derive ( Clone , PartialEq , Debug , Deserialize , Serialize ) ]
53+
foo: usize,
4454
}

tests/source/enum.rs

+11
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,14 @@ pub enum Bencoding<'i> {
109109
// TODO make Dict "structlike" AKA name the two values.
110110
Dict(&'i [u8], BTreeMap<&'i [u8], Bencoding<'i>>),
111111
}
112+
113+
// #1261
114+
pub enum CoreResourceMsg {
115+
SetCookieForUrl(
116+
ServoUrl,
117+
#[serde(deserialize_with = "::hyper_serde::deserialize",
118+
serialize_with = "::hyper_serde::serialize")]
119+
Cookie,
120+
CookieSource
121+
),
122+
}

tests/source/struct-field-attributes.rs

+15
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,18 @@ fn do_something() -> Foo {
2020
fn main() {
2121
do_something();
2222
}
23+
24+
// #1462
25+
struct Foo {
26+
foo: usize,
27+
#[cfg(feature="include-bar")]
28+
bar: usize,
29+
}
30+
31+
fn new_foo() -> Foo {
32+
Foo {
33+
foo: 0,
34+
#[cfg(feature="include-bar")]
35+
bar: 0,
36+
}
37+
}

tests/target/attrib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ impl Bar {
3737
// tooooooooooooooooooooooooooooooo loooooooooooong.
3838
/// Blah blah bing.
3939
fn f4(self) -> Cat {}
40+
41+
// We want spaces around `=`
42+
#[cfg(feature = "nightly")]
43+
fn f5(self) -> Monkey {}
44+
}
45+
46+
// #984
47+
struct Foo {
48+
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
49+
foo: usize,
4050
}

tests/target/enum.rs

+9
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,12 @@ pub enum Bencoding<'i> {
139139
// TODO make Dict "structlike" AKA name the two values.
140140
Dict(&'i [u8], BTreeMap<&'i [u8], Bencoding<'i>>),
141141
}
142+
143+
// #1261
144+
pub enum CoreResourceMsg {
145+
SetCookieForUrl(ServoUrl,
146+
#[serde(deserialize_with = "::hyper_serde::deserialize",
147+
serialize_with = "::hyper_serde::serialize")]
148+
Cookie,
149+
CookieSource),
150+
}

tests/target/nestedmod/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod mymod1 {
77
mod mod3a;
88
}
99

10-
#[path="mod2c.rs"]
10+
#[path = "mod2c.rs"]
1111
mod mymod2;
1212

1313
mod submod2;

tests/target/nestedmod/mod2b.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
#[path="mod2a.rs"]
2+
#[path = "mod2a.rs"]
33
mod c;

tests/target/struct-field-attributes.rs

+15
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,18 @@ fn do_something() -> Foo {
2020
fn main() {
2121
do_something();
2222
}
23+
24+
// #1462
25+
struct Foo {
26+
foo: usize,
27+
#[cfg(feature = "include-bar")]
28+
bar: usize,
29+
}
30+
31+
fn new_foo() -> Foo {
32+
Foo {
33+
foo: 0,
34+
#[cfg(feature = "include-bar")]
35+
bar: 0,
36+
}
37+
}

0 commit comments

Comments
 (0)