mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Prefer imports starting with std
This commit is contained in:
parent
5dd8f8e26f
commit
7a2e449424
3 changed files with 34 additions and 7 deletions
|
@ -7,7 +7,7 @@ use crate::{
|
|||
visibility::Visibility,
|
||||
CrateId, ModuleDefId, ModuleId,
|
||||
};
|
||||
use hir_expand::name::Name;
|
||||
use hir_expand::name::{known, Name};
|
||||
|
||||
const MAX_PATH_LEN: usize = 15;
|
||||
|
||||
|
@ -102,7 +102,7 @@ fn find_path_inner(
|
|||
let mut best_path = None;
|
||||
let mut best_path_len = max_len;
|
||||
for (module_id, name) in importable_locations {
|
||||
let mut path = match find_path_inner(
|
||||
let mut new_path = match find_path_inner(
|
||||
db,
|
||||
ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
|
||||
from,
|
||||
|
@ -111,15 +111,40 @@ fn find_path_inner(
|
|||
None => continue,
|
||||
Some(path) => path,
|
||||
};
|
||||
path.segments.push(name);
|
||||
if path_len(&path) < best_path_len {
|
||||
best_path_len = path_len(&path);
|
||||
best_path = Some(path);
|
||||
new_path.segments.push(name);
|
||||
|
||||
if prefer_new_path(best_path_len, best_path.as_ref(), &new_path) {
|
||||
best_path_len = path_len(&new_path);
|
||||
best_path = Some(new_path);
|
||||
}
|
||||
}
|
||||
best_path
|
||||
}
|
||||
|
||||
fn prefer_new_path(old_path_len: usize, old_path: Option<&ModPath>, new_path: &ModPath) -> bool {
|
||||
match (old_path.and_then(|mod_path| mod_path.segments.first()), new_path.segments.first()) {
|
||||
(Some(old_path_start), Some(new_path_start))
|
||||
if old_path_start == &known::std && use_std_instead(new_path_start) =>
|
||||
{
|
||||
false
|
||||
}
|
||||
(Some(old_path_start), Some(new_path_start))
|
||||
if new_path_start == &known::std && use_std_instead(old_path_start) =>
|
||||
{
|
||||
true
|
||||
}
|
||||
(None, Some(_)) => true,
|
||||
(Some(_), None) => false,
|
||||
_ => path_len(new_path) < old_path_len,
|
||||
}
|
||||
}
|
||||
|
||||
// When std library is present, paths starting with `std::`
|
||||
// should be preferred over paths starting with `core::` and `alloc::`
|
||||
fn use_std_instead(name: &Name) -> bool {
|
||||
name == &known::core || name == &known::alloc
|
||||
}
|
||||
|
||||
fn path_len(path: &ModPath) -> usize {
|
||||
path.segments.len()
|
||||
+ match path.kind {
|
||||
|
|
|
@ -141,6 +141,8 @@ pub mod known {
|
|||
macro_rules,
|
||||
// Components of known path (value or mod name)
|
||||
std,
|
||||
core,
|
||||
alloc,
|
||||
iter,
|
||||
ops,
|
||||
future,
|
||||
|
|
2
editors/code/package-lock.json
generated
2
editors/code/package-lock.json
generated
|
@ -782,7 +782,7 @@
|
|||
"semver": {
|
||||
"version": "5.7.1",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
|
||||
"integrity": "sha1-qVT5Ma66UI0we78Gnv8MAclhFvc=",
|
||||
"integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue