mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Use hir::Trait in parameter
This commit is contained in:
parent
28f1e62482
commit
fc5dd8b798
@ -1,4 +1,4 @@
|
|||||||
use ide_db::famous_defs::FamousDefs;
|
use ide_db::{famous_defs::FamousDefs, traits::resolve_target_trait};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
AstNode,
|
AstNode,
|
||||||
ast::{self, edit_in_place::Indent, make},
|
ast::{self, edit_in_place::Indent, make},
|
||||||
@ -48,27 +48,19 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
|
|||||||
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();
|
let impl_def = ctx.find_node_at_offset::<ast::Impl>()?.clone_for_update();
|
||||||
let indent = impl_def.indent_level();
|
let indent = impl_def.indent_level();
|
||||||
|
|
||||||
let (apply_trait, new_apply_trait) = impl_def
|
let ast::Type::PathType(path) = impl_def.trait_()? else {
|
||||||
.syntax()
|
return None;
|
||||||
.descendants()
|
};
|
||||||
.filter_map(ast::NameRef::cast)
|
let trait_name = path.path()?.segment()?.name_ref()?;
|
||||||
.find_map(process_trait_name)?;
|
|
||||||
|
|
||||||
let trait_ = impl_def.trait_()?;
|
let scope = ctx.sema.scope(impl_def.trait_()?.syntax())?;
|
||||||
if let ast::Type::PathType(trait_path) = trait_ {
|
let famous = FamousDefs(&ctx.sema, scope.krate());
|
||||||
let trait_type = ctx.sema.resolve_trait(&trait_path.path()?)?;
|
|
||||||
let scope = ctx.sema.scope(trait_path.syntax())?;
|
let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
|
||||||
let famous_defs = FamousDefs(&ctx.sema, scope.krate());
|
let trait_new = get_trait_mut(&trait_, famous)?;
|
||||||
if trait_type != get_famous(&apply_trait.text(), famous_defs)? {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Index -> IndexMut
|
// Index -> IndexMut
|
||||||
ted::replace(
|
ted::replace(trait_name.syntax(), make::name_ref(trait_new).clone_for_update().syntax());
|
||||||
apply_trait.syntax(),
|
|
||||||
make::path_segment(make::name_ref(new_apply_trait)).clone_for_update().syntax(),
|
|
||||||
);
|
|
||||||
|
|
||||||
// index -> index_mut
|
// index -> index_mut
|
||||||
let (trait_method_name, new_trait_method_name) = impl_def
|
let (trait_method_name, new_trait_method_name) = impl_def
|
||||||
@ -108,7 +100,7 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
|
|||||||
let target = impl_def.syntax().text_range();
|
let target = impl_def.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId::generate("generate_mut_trait_impl"),
|
AssistId::generate("generate_mut_trait_impl"),
|
||||||
format!("Generate `{new_apply_trait}` impl from this `{apply_trait}` trait"),
|
format!("Generate `{trait_new}` impl from this `{trait_name}` trait"),
|
||||||
target,
|
target,
|
||||||
|edit| {
|
|edit| {
|
||||||
edit.insert(target.start(), format!("$0{impl_def}\n\n{indent}"));
|
edit.insert(target.start(), format!("$0{impl_def}\n\n{indent}"));
|
||||||
@ -116,23 +108,18 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_famous(apply_trait: &str, famous: FamousDefs<'_, '_>) -> Option<hir::Trait> {
|
fn get_trait_mut(apply_trait: &hir::Trait, famous: FamousDefs<'_, '_>) -> Option<&'static str> {
|
||||||
match apply_trait {
|
let trait_ = Some(apply_trait);
|
||||||
"Index" => famous.core_convert_Index(),
|
if trait_ == famous.core_convert_Index().as_ref() {
|
||||||
"AsRef" => famous.core_convert_AsRef(),
|
return Some("IndexMut");
|
||||||
"Borrow" => famous.core_borrow_Borrow(),
|
|
||||||
_ => None,
|
|
||||||
}
|
}
|
||||||
}
|
if trait_ == famous.core_convert_AsRef().as_ref() {
|
||||||
|
return Some("AsMut");
|
||||||
fn process_trait_name(name: ast::NameRef) -> Option<(ast::NameRef, &'static str)> {
|
}
|
||||||
let new_name = match &*name.text() {
|
if trait_ == famous.core_borrow_Borrow().as_ref() {
|
||||||
"Index" => "IndexMut",
|
return Some("BorrowMut");
|
||||||
"AsRef" => "AsMut",
|
}
|
||||||
"Borrow" => "BorrowMut",
|
None
|
||||||
_ => return None,
|
|
||||||
};
|
|
||||||
Some((name, new_name))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_method_name(name: ast::Name) -> Option<(ast::Name, &'static str)> {
|
fn process_method_name(name: ast::Name) -> Option<(ast::Name, &'static str)> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user