Skip to content

Commit 9f7285f

Browse files
committed
strikethrough: always insert non derive attr's after existing ones
update `strikethrough_derive` test to validade this behaviour
1 parent 7ee48d3 commit 9f7285f

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/imp.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,27 @@ fn strike_through_attributes(
252252
}
253253
});
254254

255-
strike_attrs.iter().for_each(|attr| dec_attrs.insert(0, attr.clone()));
255+
let (mut derive_attrs, rest_attrs) =
256+
strike_attrs
257+
.iter()
258+
.fold((Vec::new(), Vec::new()),
259+
| (mut der, mut rest), i| {
260+
if matches!(&i.path[0], TokenTree::Ident(token) if token == "derive") {
261+
der.push(i.clone());
262+
} else {
263+
rest.push(i.clone());
264+
}
265+
(der, rest)
266+
});
267+
268+
// place other attrs after the existing ones
269+
dec_attrs.extend_from_slice(&rest_attrs[..]);
270+
271+
// insert derive attrs before attributes
272+
derive_attrs
273+
.drain(..)
274+
.for_each(|attr| dec_attrs.insert(0, attr));
275+
256276
}
257277

258278
fn recurse_through_type_list(

src/test.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn check(nested: proc_macro2::TokenStream, planexpected: proc_macro2::TokenStrea
1212
#[test]
1313
fn strikethrough_derive() {
1414
let from = quote! {
15+
#[strikethrough[striked_attr]]
1516
#[strikethrough[derive(Debug, Default, PartialEq)]]
1617
#[gubbel]
1718
struct Parent {
@@ -22,19 +23,23 @@ fn strikethrough_derive() {
2223
e: u32,
2324
}
2425
};
26+
2527
let out = quote! {
2628
#[derive(Debug, Default, PartialEq)]
29+
#[striked_attr]
2730
struct Shared {
2831
d: i32
2932
}
30-
#[gobbel]
3133
#[derive(Debug, Default, PartialEq)]
34+
#[gobbel]
35+
#[striked_attr]
3236
struct A {
3337
b: Shared,
3438
c: Shared,
3539
}
36-
#[gubbel]
3740
#[derive(Debug, Default, PartialEq)]
41+
#[gubbel]
42+
#[striked_attr]
3843
struct Parent {
3944
a: A,
4045
e: u32,

0 commit comments

Comments
 (0)