aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2020-01-27 22:09:56 +0000
committerKirill Bulatov <[email protected]>2020-01-27 23:02:06 +0000
commit7a2e449424227c7f6cebe0581513287023297999 (patch)
treec76026fa1779741d8680f77e77360c01ce8cc759 /crates
parent5dd8f8e26f2a62f5e7e4da50dcdfde344f6d31b9 (diff)
Prefer imports starting with std
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_def/src/find_path.rs37
-rw-r--r--crates/ra_hir_expand/src/name.rs2
2 files changed, 33 insertions, 6 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,