mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 13:51:31 +00:00
Add preference modifier for workspace-local crates when using auto import.
This commit is contained in:
parent
b32f181f47
commit
4bf51eb496
1 changed files with 46 additions and 1 deletions
|
@ -272,8 +272,10 @@ fn module_distance_heuristic(db: &dyn HirDatabase, current: &Module, item: &Modu
|
|||
// cost of importing from another crate
|
||||
let crate_boundary_cost = if current.krate() == item.krate() {
|
||||
0
|
||||
} else if item.krate().is_builtin(db) {
|
||||
} else if item.krate().origin(db).is_local() {
|
||||
2
|
||||
} else if item.krate().is_builtin(db) {
|
||||
3
|
||||
} else {
|
||||
4
|
||||
};
|
||||
|
@ -365,6 +367,49 @@ pub struct HashMap;
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefer_workspace() {
|
||||
let before = r"
|
||||
//- /main.rs crate:main deps:foo,bar
|
||||
HashMap$0::new();
|
||||
|
||||
//- /lib.rs crate:foo
|
||||
pub mod module {
|
||||
pub struct HashMap;
|
||||
}
|
||||
|
||||
//- /lib.rs crate:bar library
|
||||
pub struct HashMap;
|
||||
";
|
||||
|
||||
check_auto_import_order(before, &["Import `foo::module::HashMap`", "Import `bar::HashMap`"])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefer_non_local_over_long_path() {
|
||||
let before = r"
|
||||
//- /main.rs crate:main deps:foo,bar
|
||||
HashMap$0::new();
|
||||
|
||||
//- /lib.rs crate:foo
|
||||
pub mod deeply {
|
||||
pub mod nested {
|
||||
pub mod module {
|
||||
pub struct HashMap;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//- /lib.rs crate:bar library
|
||||
pub struct HashMap;
|
||||
";
|
||||
|
||||
check_auto_import_order(
|
||||
before,
|
||||
&["Import `bar::HashMap`", "Import `foo::deeply::nested::module::HashMap`"],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn not_applicable_if_scope_inside_macro() {
|
||||
check_assist_not_applicable(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue