aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_hir_def/src/find_path.rs37
-rw-r--r--crates/ra_hir_expand/src/name.rs2
-rw-r--r--editors/code/package-lock.json2
3 files changed, 34 insertions, 7 deletions
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs
index 8cc2fb160..d9e67607f 100644
--- a/crates/ra_hir_def/src/find_path.rs
+++ b/crates/ra_hir_def/src/find_path.rs
@@ -7,7 +7,7 @@ use crate::{
7 visibility::Visibility, 7 visibility::Visibility,
8 CrateId, ModuleDefId, ModuleId, 8 CrateId, ModuleDefId, ModuleId,
9}; 9};
10use hir_expand::name::Name; 10use hir_expand::name::{known, Name};
11 11
12const MAX_PATH_LEN: usize = 15; 12const MAX_PATH_LEN: usize = 15;
13 13
@@ -102,7 +102,7 @@ fn find_path_inner(
102 let mut best_path = None; 102 let mut best_path = None;
103 let mut best_path_len = max_len; 103 let mut best_path_len = max_len;
104 for (module_id, name) in importable_locations { 104 for (module_id, name) in importable_locations {
105 let mut path = match find_path_inner( 105 let mut new_path = match find_path_inner(
106 db, 106 db,
107 ItemInNs::Types(ModuleDefId::ModuleId(module_id)), 107 ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
108 from, 108 from,
@@ -111,15 +111,40 @@ fn find_path_inner(
111 None => continue, 111 None => continue,
112 Some(path) => path, 112 Some(path) => path,
113 }; 113 };
114 path.segments.push(name); 114 new_path.segments.push(name);
115 if path_len(&path) < best_path_len { 115
116 best_path_len = path_len(&path); 116 if prefer_new_path(best_path_len, best_path.as_ref(), &new_path) {
117 best_path = Some(path); 117 best_path_len = path_len(&new_path);
118 best_path = Some(new_path);
118 } 119 }
119 } 120 }
120 best_path 121 best_path
121} 122}
122 123
124fn prefer_new_path(old_path_len: usize, old_path: Option<&ModPath>, new_path: &ModPath) -> bool {
125 match (old_path.and_then(|mod_path| mod_path.segments.first()), new_path.segments.first()) {
126 (Some(old_path_start), Some(new_path_start))
127 if old_path_start == &known::std && use_std_instead(new_path_start) =>
128 {
129 false
130 }
131 (Some(old_path_start), Some(new_path_start))
132 if new_path_start == &known::std && use_std_instead(old_path_start) =>
133 {
134 true
135 }
136 (None, Some(_)) => true,
137 (Some(_), None) => false,
138 _ => path_len(new_path) < old_path_len,
139 }
140}
141
142// When std library is present, paths starting with `std::`
143// should be preferred over paths starting with `core::` and `alloc::`
144fn use_std_instead(name: &Name) -> bool {
145 name == &known::core || name == &known::alloc
146}
147
123fn path_len(path: &ModPath) -> usize { 148fn path_len(path: &ModPath) -> usize {
124 path.segments.len() 149 path.segments.len()
125 + match path.kind { 150 + match path.kind {
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs
index b3fa1efba..b2e10f445 100644
--- a/crates/ra_hir_expand/src/name.rs
+++ b/crates/ra_hir_expand/src/name.rs
@@ -141,6 +141,8 @@ pub mod known {
141 macro_rules, 141 macro_rules,
142 // Components of known path (value or mod name) 142 // Components of known path (value or mod name)
143 std, 143 std,
144 core,
145 alloc,
144 iter, 146 iter,
145 ops, 147 ops,
146 future, 148 future,
diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json
index d38a45b85..96e9ac378 100644
--- a/editors/code/package-lock.json
+++ b/editors/code/package-lock.json
@@ -782,7 +782,7 @@
782 "semver": { 782 "semver": {
783 "version": "5.7.1", 783 "version": "5.7.1",
784 "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", 784 "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz",
785 "integrity": "sha1-qVT5Ma66UI0we78Gnv8MAclhFvc=", 785 "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==",
786 "dev": true 786 "dev": true
787 } 787 }
788 } 788 }