Skip to content

Commit 9909a37

Browse files
committed
Use hir::Trait in parameter
1 parent 9ba661e commit 9909a37

File tree

1 file changed

+24
-37
lines changed

1 file changed

+24
-37
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_mut_trait_impl.rs

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ide_db::famous_defs::FamousDefs;
1+
use ide_db::{famous_defs::FamousDefs, traits::resolve_target_trait};
22
use syntax::{
33
AstNode,
44
ast::{self, edit_in_place::Indent, make},
@@ -48,27 +48,19 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
4848
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();
4949
let indent = impl_def.indent_level();
5050

51-
let (apply_trait, new_apply_trait) = impl_def
52-
.syntax()
53-
.descendants()
54-
.filter_map(ast::NameRef::cast)
55-
.find_map(process_trait_name)?;
56-
57-
let trait_ = impl_def.trait_()?;
58-
if let ast::Type::PathType(trait_path) = trait_ {
59-
let trait_type = ctx.sema.resolve_trait(&trait_path.path()?)?;
60-
let scope = ctx.sema.scope(trait_path.syntax())?;
61-
let famous_defs = FamousDefs(&ctx.sema, scope.krate());
62-
if trait_type != get_famous(&apply_trait.text(), famous_defs)? {
63-
return None;
64-
}
65-
}
51+
let ast::Type::PathType(path) = impl_def.trait_()? else {
52+
return None;
53+
};
54+
let trait_name = path.path()?.segment()?.name_ref()?;
55+
56+
let scope = ctx.sema.scope(impl_def.trait_()?.syntax())?;
57+
let famous = FamousDefs(&ctx.sema, scope.krate());
58+
59+
let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
60+
let trait_new = get_trait_mut(&trait_, famous)?;
6661

6762
// Index -> IndexMut
68-
ted::replace(
69-
apply_trait.syntax(),
70-
make::path_segment(make::name_ref(new_apply_trait)).clone_for_update().syntax(),
71-
);
63+
ted::replace(trait_name.syntax(), make::name_ref(trait_new).clone_for_update().syntax());
7264

7365
// index -> index_mut
7466
let (trait_method_name, new_trait_method_name) = impl_def
@@ -108,31 +100,26 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
108100
let target = impl_def.syntax().text_range();
109101
acc.add(
110102
AssistId::generate("generate_mut_trait_impl"),
111-
format!("Generate `{new_apply_trait}` impl from this `{apply_trait}` trait"),
103+
format!("Generate `{trait_new}` impl from this `{trait_name}` trait"),
112104
target,
113105
|edit| {
114106
edit.insert(target.start(), format!("$0{impl_def}\n\n{indent}"));
115107
},
116108
)
117109
}
118110

119-
fn get_famous(apply_trait: &str, famous: FamousDefs<'_, '_>) -> Option<hir::Trait> {
120-
match apply_trait {
121-
"Index" => famous.core_convert_Index(),
122-
"AsRef" => famous.core_convert_AsRef(),
123-
"Borrow" => famous.core_borrow_Borrow(),
124-
_ => None,
111+
fn get_trait_mut(apply_trait: &hir::Trait, famous: FamousDefs<'_, '_>) -> Option<&'static str> {
112+
let trait_ = Some(apply_trait);
113+
if trait_ == famous.core_convert_Index().as_ref() {
114+
return Some("IndexMut");
125115
}
126-
}
127-
128-
fn process_trait_name(name: ast::NameRef) -> Option<(ast::NameRef, &'static str)> {
129-
let new_name = match &*name.text() {
130-
"Index" => "IndexMut",
131-
"AsRef" => "AsMut",
132-
"Borrow" => "BorrowMut",
133-
_ => return None,
134-
};
135-
Some((name, new_name))
116+
if trait_ == famous.core_convert_AsRef().as_ref() {
117+
return Some("AsMut");
118+
}
119+
if trait_ == famous.core_borrow_Borrow().as_ref() {
120+
return Some("BorrowMut");
121+
}
122+
None
136123
}
137124

138125
fn process_method_name(name: ast::Name) -> Option<(ast::Name, &'static str)> {

0 commit comments

Comments
 (0)