aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/find_path.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_def/src/find_path.rs')
-rw-r--r--crates/ra_hir_def/src/find_path.rs64
1 files changed, 33 insertions, 31 deletions
diff --git a/crates/ra_hir_def/src/find_path.rs b/crates/ra_hir_def/src/find_path.rs
index 2eb12ec8f..4db798473 100644
--- a/crates/ra_hir_def/src/find_path.rs
+++ b/crates/ra_hir_def/src/find_path.rs
@@ -4,7 +4,7 @@ use std::sync::Arc;
4 4
5use hir_expand::name::{known, AsName, Name}; 5use hir_expand::name::{known, AsName, Name};
6use ra_prof::profile; 6use ra_prof::profile;
7use test_utils::tested_by; 7use test_utils::mark;
8 8
9use crate::{ 9use crate::{
10 db::DefDatabase, 10 db::DefDatabase,
@@ -20,7 +20,7 @@ use crate::{
20/// *from where* you're referring to the item, hence the `from` parameter. 20/// *from where* you're referring to the item, hence the `from` parameter.
21pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> { 21pub fn find_path(db: &dyn DefDatabase, item: ItemInNs, from: ModuleId) -> Option<ModPath> {
22 let _p = profile("find_path"); 22 let _p = profile("find_path");
23 find_path_inner(db, item, from, MAX_PATH_LEN) 23 db.find_path_inner(item, from, MAX_PATH_LEN)
24} 24}
25 25
26const MAX_PATH_LEN: usize = 15; 26const MAX_PATH_LEN: usize = 15;
@@ -49,7 +49,7 @@ impl ModPath {
49 } 49 }
50} 50}
51 51
52fn find_path_inner( 52pub(crate) fn find_path_inner_query(
53 db: &dyn DefDatabase, 53 db: &dyn DefDatabase,
54 item: ItemInNs, 54 item: ItemInNs,
55 from: ModuleId, 55 from: ModuleId,
@@ -140,8 +140,7 @@ fn find_path_inner(
140 let mut best_path = None; 140 let mut best_path = None;
141 let mut best_path_len = max_len; 141 let mut best_path_len = max_len;
142 for (module_id, name) in importable_locations { 142 for (module_id, name) in importable_locations {
143 let mut path = match find_path_inner( 143 let mut path = match db.find_path_inner(
144 db,
145 ItemInNs::Types(ModuleDefId::ModuleId(module_id)), 144 ItemInNs::Types(ModuleDefId::ModuleId(module_id)),
146 from, 145 from,
147 best_path_len - 1, 146 best_path_len - 1,
@@ -164,17 +163,19 @@ fn find_path_inner(
164 163
165fn select_best_path(old_path: ModPath, new_path: ModPath, prefer_no_std: bool) -> ModPath { 164fn select_best_path(old_path: ModPath, new_path: ModPath, prefer_no_std: bool) -> ModPath {
166 if old_path.starts_with_std() && new_path.can_start_with_std() { 165 if old_path.starts_with_std() && new_path.can_start_with_std() {
167 tested_by!(prefer_std_paths);
168 if prefer_no_std { 166 if prefer_no_std {
167 mark::hit!(prefer_no_std_paths);
169 new_path 168 new_path
170 } else { 169 } else {
170 mark::hit!(prefer_std_paths);
171 old_path 171 old_path
172 } 172 }
173 } else if new_path.starts_with_std() && old_path.can_start_with_std() { 173 } else if new_path.starts_with_std() && old_path.can_start_with_std() {
174 tested_by!(prefer_std_paths);
175 if prefer_no_std { 174 if prefer_no_std {
175 mark::hit!(prefer_no_std_paths);
176 old_path 176 old_path
177 } else { 177 } else {
178 mark::hit!(prefer_std_paths);
178 new_path 179 new_path
179 } 180 }
180 } else if new_path.len() < old_path.len() { 181 } else if new_path.len() < old_path.len() {
@@ -251,12 +252,14 @@ pub(crate) fn importable_locations_of_query(
251 252
252#[cfg(test)] 253#[cfg(test)]
253mod tests { 254mod tests {
254 use super::*;
255 use crate::test_db::TestDB;
256 use hir_expand::hygiene::Hygiene; 255 use hir_expand::hygiene::Hygiene;
257 use ra_db::fixture::WithFixture; 256 use ra_db::fixture::WithFixture;
258 use ra_syntax::ast::AstNode; 257 use ra_syntax::ast::AstNode;
259 use test_utils::covers; 258 use test_utils::mark;
259
260 use crate::test_db::TestDB;
261
262 use super::*;
260 263
261 /// `code` needs to contain a cursor marker; checks that `find_path` for the 264 /// `code` needs to contain a cursor marker; checks that `find_path` for the
262 /// item the `path` refers to returns that same path when called from the 265 /// item the `path` refers to returns that same path when called from the
@@ -511,7 +514,7 @@ mod tests {
511 514
512 #[test] 515 #[test]
513 fn prefer_std_paths_over_alloc() { 516 fn prefer_std_paths_over_alloc() {
514 covers!(prefer_std_paths); 517 mark::check!(prefer_std_paths);
515 let code = r#" 518 let code = r#"
516 //- /main.rs crate:main deps:alloc,std 519 //- /main.rs crate:main deps:alloc,std
517 <|> 520 <|>
@@ -530,51 +533,50 @@ mod tests {
530 } 533 }
531 534
532 #[test] 535 #[test]
533 fn prefer_alloc_paths_over_std() { 536 fn prefer_core_paths_over_std() {
534 covers!(prefer_std_paths); 537 mark::check!(prefer_no_std_paths);
535 let code = r#" 538 let code = r#"
536 //- /main.rs crate:main deps:alloc,std 539 //- /main.rs crate:main deps:core,std
537 #![no_std] 540 #![no_std]
538 541
539 <|> 542 <|>
540 543
541 //- /std.rs crate:std deps:alloc 544 //- /std.rs crate:std deps:core
542 545
543 pub mod sync { 546 pub mod fmt {
544 pub use alloc::sync::Arc; 547 pub use core::fmt::Error;
545 } 548 }
546 549
547 //- /zzz.rs crate:alloc 550 //- /zzz.rs crate:core
548 551
549 pub mod sync { 552 pub mod fmt {
550 pub struct Arc; 553 pub struct Error;
551 } 554 }
552 "#; 555 "#;
553 check_found_path(code, "alloc::sync::Arc"); 556 check_found_path(code, "core::fmt::Error");
554 } 557 }
555 558
556 #[test] 559 #[test]
557 fn prefer_core_paths_over_std() { 560 fn prefer_alloc_paths_over_std() {
558 covers!(prefer_std_paths);
559 let code = r#" 561 let code = r#"
560 //- /main.rs crate:main deps:core,std 562 //- /main.rs crate:main deps:alloc,std
561 #![no_std] 563 #![no_std]
562 564
563 <|> 565 <|>
564 566
565 //- /std.rs crate:std deps:core 567 //- /std.rs crate:std deps:alloc
566 568
567 pub mod fmt { 569 pub mod sync {
568 pub use core::fmt::Error; 570 pub use alloc::sync::Arc;
569 } 571 }
570 572
571 //- /zzz.rs crate:core 573 //- /zzz.rs crate:alloc
572 574
573 pub mod fmt { 575 pub mod sync {
574 pub struct Error; 576 pub struct Arc;
575 } 577 }
576 "#; 578 "#;
577 check_found_path(code, "core::fmt::Error"); 579 check_found_path(code, "alloc::sync::Arc");
578 } 580 }
579 581
580 #[test] 582 #[test]