mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 12:33:33 +00:00
Replace stripped derives with whitespace in process_macro_input
This commit is contained in:
parent
8cd171cd94
commit
5507512eaf
1 changed files with 23 additions and 10 deletions
|
@ -40,7 +40,7 @@ fn remove_derives_up_to(item: ast::Item, attr_index: usize) -> ast::Item {
|
||||||
attr.path().and_then(|path| path.as_single_segment()).and_then(|seg| seg.name_ref())
|
attr.path().and_then(|path| path.as_single_segment()).and_then(|seg| seg.name_ref())
|
||||||
{
|
{
|
||||||
if name.as_name() == name![derive] {
|
if name.as_name() == name![derive] {
|
||||||
attr.syntax().detach();
|
replace_attr(&item, &attr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,10 +54,14 @@ fn remove_attr_invoc(item: ast::Item, attr_index: usize) -> ast::Item {
|
||||||
.attrs()
|
.attrs()
|
||||||
.nth(attr_index)
|
.nth(attr_index)
|
||||||
.unwrap_or_else(|| panic!("cannot find attribute #{}", attr_index));
|
.unwrap_or_else(|| panic!("cannot find attribute #{}", attr_index));
|
||||||
|
replace_attr(&item, &attr);
|
||||||
|
item
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace_attr(item: &ast::Item, attr: &ast::Attr) {
|
||||||
let syntax_index = attr.syntax().index();
|
let syntax_index = attr.syntax().index();
|
||||||
let ws = make::tokens::whitespace(&" ".repeat(u32::from(attr.syntax().text().len()) as usize));
|
let ws = make::tokens::whitespace(&" ".repeat(u32::from(attr.syntax().text().len()) as usize));
|
||||||
item.syntax().splice_children(syntax_index..syntax_index + 1, vec![ws.into()]);
|
item.syntax().splice_children(syntax_index..syntax_index + 1, vec![ws.into()]);
|
||||||
item
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -78,7 +82,9 @@ mod tests {
|
||||||
assert_eq!(items.len(), 1);
|
assert_eq!(items.len(), 1);
|
||||||
|
|
||||||
let item = remove_derives_up_to(items.pop().unwrap(), attr);
|
let item = remove_derives_up_to(items.pop().unwrap(), attr);
|
||||||
expect.assert_eq(&item.to_string());
|
let res: String =
|
||||||
|
item.syntax().children_with_tokens().map(|e| format!("{:?}\n", e)).collect();
|
||||||
|
expect.assert_eq(&res);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -95,13 +101,20 @@ struct A {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
#[allow(unused)]
|
Node(ATTR@0..16)
|
||||||
|
Token(WHITESPACE@16..17 "\n")
|
||||||
|
Token(WHITESPACE@17..32 " ")
|
||||||
#[derive(Clone)]
|
Token(WHITESPACE@32..33 "\n")
|
||||||
struct A {
|
Token(WHITESPACE@33..49 " ")
|
||||||
bar: u32
|
Token(WHITESPACE@49..50 "\n")
|
||||||
}"#]],
|
Node(ATTR@50..66)
|
||||||
|
Token(WHITESPACE@66..67 "\n")
|
||||||
|
Token(STRUCT_KW@67..73 "struct")
|
||||||
|
Token(WHITESPACE@73..74 " ")
|
||||||
|
Node(NAME@74..75)
|
||||||
|
Token(WHITESPACE@75..76 " ")
|
||||||
|
Node(RECORD_FIELD_LIST@76..92)
|
||||||
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue