Make replace_derive_with_manual_impl work again

This commit is contained in:
Lukas Wirth 2022-02-21 12:57:57 +01:00
parent be3168dabe
commit f13c98034b
9 changed files with 118 additions and 130 deletions

View file

@ -3,8 +3,7 @@ use ide_db::{
helpers::{insert_whitespace_into_node::insert_ws_into, pick_best_token},
RootDatabase,
};
use itertools::Itertools;
use syntax::{ast, ted, AstNode, SyntaxKind, SyntaxNode};
use syntax::{ast, ted, AstNode, NodeOrToken, SyntaxKind, SyntaxNode, T};
use crate::FilePosition;
@ -52,7 +51,17 @@ pub(crate) fn expand_macro(db: &RootDatabase, position: FilePosition) -> Option<
let token = hir::InFile::new(hir_file, descended).upmap(db)?.value;
let attr = token.ancestors().find_map(ast::Attr::cast)?;
let expansions = sema.expand_derive_macro(&attr)?;
Some(ExpandedMacro { name, expansion: expansions.into_iter().map(insert_ws_into).join("") })
let idx = attr
.token_tree()?
.token_trees_and_tokens()
.filter_map(NodeOrToken::into_token)
.take_while(|it| it == &token)
.filter(|it| it.kind() == T![,])
.count();
Some(ExpandedMacro {
name,
expansion: expansions.get(idx).cloned().map(insert_ws_into)?.to_string(),
})
});
if derive.is_some() {
@ -370,11 +379,9 @@ struct Foo {}
struct Foo {}
"#,
expect![[r#"
Copy, Clone
Copy
impl < >core::marker::Copy for Foo< >{}
impl < >core::clone::Clone for Foo< >{}
"#]],
);
}