-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: rename and add import #19799
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
fix: rename and add import #19799
Conversation
if self.def == def | ||
// is our def a trait assoc item? then we want to find all assoc items from trait impls of our trait | ||
|| matches!(self.assoc_item_container, Some(hir::AssocItemContainer::Trait(_))) | ||
&& convert_to_def_in_trait(self.sema.db, def) == self.def => | ||
&& convert_to_def_in_trait(self.sema.db, def) == self.def | ||
|| self.is_related_import(name_ref) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I find some definition item, we use NameRefClass::classify but it only return single definition. So, I check use block have some definition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change give affect to this test
rust-analyzer/crates/ide/src/references.rs
Lines 1765 to 1780 in d2a05af
#[test] | |
fn attr() { | |
check( | |
r#" | |
//- proc_macros: identity | |
use proc_macros::identity; | |
#[proc_macros::$0identity] | |
fn func() {} | |
"#, | |
expect![[r#" | |
identity Attribute FileId(1) 1..107 32..40 | |
FileId(0) 17..25 import | |
FileId(0) 43..51 | |
"#]], |
Signed-off-by: Hayashi Mikihiro <[email protected]>
4b1488f
to
d2a05af
Compare
} else if let Some(res) = ast::UseTree::find_tail_use_tree_for_name_ref(name_ref) | ||
.and_then(|u| u.path()) | ||
.and_then(|p| sema.resolve_path_per_ns(&p)) | ||
{ | ||
let res = res | ||
.to_small_vec() | ||
.into_iter() | ||
.flatten() | ||
.filter_map(|res| match res { | ||
hir::PathResolution::Def(def) => Some(Definition::from(def)), | ||
_ => None, | ||
}) | ||
.unique() | ||
.collect::<Vec<_>>(); | ||
|
||
let range = name_ref.syntax().text_range(); | ||
if res.iter().any(|res| res == &def) { | ||
if res.len() == 1 { | ||
edit.replace(range, new_name.to_owned()); | ||
} else { | ||
edit.replace(range, format!("{{{}, {}}}", new_name, name_ref.text())); | ||
} | ||
} | ||
|
||
return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we rename import item, We check that the path is ambiguous or not. if It is ambiguous, we add old import name and new name.
if let Some(path) = ast::UseTree::find_tail_use_tree_for_name_ref(name_ref).and_then(|u| u.path()).and_then(|p| sema.resolve_path_per_ns(&p)){ | ||
let defs = path.to_small_vec().into_iter().filter_map(|res| match res{ | ||
Some(PathResolution::Def(def)) => Some(Definition::from(def)), | ||
_ => None, | ||
}).unique().collect::<Vec<_>>(); | ||
match defs.len() { | ||
0 => Err(format_err!("No references found at position")), | ||
1 => { | ||
if defs[0].name(sema.db).is_some_and(|it| it.as_str() != name_ref.text().trim_start_matches("r#")) { | ||
Err(format_err!("Renaming aliases is currently unsupported")) | ||
} else { | ||
Ok(defs[0]) | ||
} | ||
} | ||
}) | ||
2.. => Err(format_err!("Ambiguous import is currently unsupported")), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use bar::foo;
//^^^ It import `fn foo()` and `struct foo{}`
mod bar {
pub fn foo() {}
pub struct foo {}
}
I think we shouldn't rename the ambiguous import name_ref.
fix: #18892
2025-05-16.4.31.58.mov