diff options
author | Jonas Schievink <[email protected]> | 2020-06-10 11:30:33 +0100 |
---|---|---|
committer | Jonas Schievink <[email protected]> | 2020-06-10 11:40:33 +0100 |
commit | 56c7145993f94a12bf923f08cbd62d963e62bbd1 (patch) | |
tree | d61f3726112bc0c2fd5749ce7d95777de1246c43 /crates | |
parent | bcf875f46ae5142c42ddac8094e1b6652182d4be (diff) |
Limit import map queries
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir_def/src/import_map.rs | 42 |
2 files changed, 51 insertions, 7 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c8329d971..a55fe03a6 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -104,12 +104,16 @@ impl Crate { | |||
104 | db: &dyn DefDatabase, | 104 | db: &dyn DefDatabase, |
105 | query: &str, | 105 | query: &str, |
106 | ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> { | 106 | ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> { |
107 | import_map::search_dependencies(db, self.into(), import_map::Query::new(query).anchor_end()) | 107 | import_map::search_dependencies( |
108 | .into_iter() | 108 | db, |
109 | .map(|item| match item { | 109 | self.into(), |
110 | ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()), | 110 | import_map::Query::new(query).anchor_end().limit(40), |
111 | ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()), | 111 | ) |
112 | }) | 112 | .into_iter() |
113 | .map(|item| match item { | ||
114 | ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()), | ||
115 | ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()), | ||
116 | }) | ||
113 | } | 117 | } |
114 | 118 | ||
115 | pub fn all(db: &dyn HirDatabase) -> Vec<Crate> { | 119 | pub fn all(db: &dyn HirDatabase) -> Vec<Crate> { |
diff --git a/crates/ra_hir_def/src/import_map.rs b/crates/ra_hir_def/src/import_map.rs index f2e4ca2db..70368d8df 100644 --- a/crates/ra_hir_def/src/import_map.rs +++ b/crates/ra_hir_def/src/import_map.rs | |||
@@ -178,11 +178,12 @@ fn cmp((_, lhs): &(&ItemInNs, &ModPath), (_, rhs): &(&ItemInNs, &ModPath)) -> Or | |||
178 | pub struct Query { | 178 | pub struct Query { |
179 | query: String, | 179 | query: String, |
180 | anchor_end: bool, | 180 | anchor_end: bool, |
181 | limit: usize, | ||
181 | } | 182 | } |
182 | 183 | ||
183 | impl Query { | 184 | impl Query { |
184 | pub fn new(query: &str) -> Self { | 185 | pub fn new(query: &str) -> Self { |
185 | Self { query: query.to_lowercase(), anchor_end: false } | 186 | Self { query: query.to_lowercase(), anchor_end: false, limit: usize::max_value() } |
186 | } | 187 | } |
187 | 188 | ||
188 | /// Only returns items whose paths end with the (case-insensitive) query string as their last | 189 | /// Only returns items whose paths end with the (case-insensitive) query string as their last |
@@ -190,6 +191,11 @@ impl Query { | |||
190 | pub fn anchor_end(self) -> Self { | 191 | pub fn anchor_end(self) -> Self { |
191 | Self { anchor_end: true, ..self } | 192 | Self { anchor_end: true, ..self } |
192 | } | 193 | } |
194 | |||
195 | /// Limits the returned number of items to `limit`. | ||
196 | pub fn limit(self, limit: usize) -> Self { | ||
197 | Self { limit, ..self } | ||
198 | } | ||
193 | } | 199 | } |
194 | 200 | ||
195 | /// Searches dependencies of `krate` for an importable path matching `query`. | 201 | /// Searches dependencies of `krate` for an importable path matching `query`. |
@@ -237,6 +243,11 @@ pub fn search_dependencies<'a>( | |||
237 | let item_path = &import_map.map[item]; | 243 | let item_path = &import_map.map[item]; |
238 | fst_path(item_path) == fst_path(path) | 244 | fst_path(item_path) == fst_path(path) |
239 | })); | 245 | })); |
246 | |||
247 | if res.len() >= query.limit { | ||
248 | res.truncate(query.limit); | ||
249 | return res; | ||
250 | } | ||
240 | } | 251 | } |
241 | } | 252 | } |
242 | 253 | ||
@@ -570,4 +581,33 @@ mod tests { | |||
570 | dep::Fmt (m) | 581 | dep::Fmt (m) |
571 | "###); | 582 | "###); |
572 | } | 583 | } |
584 | |||
585 | #[test] | ||
586 | fn search_limit() { | ||
587 | let res = search_dependencies_of( | ||
588 | r#" | ||
589 | //- /main.rs crate:main deps:dep | ||
590 | //- /dep.rs crate:dep | ||
591 | pub mod fmt { | ||
592 | pub trait Display { | ||
593 | fn fmt(); | ||
594 | } | ||
595 | } | ||
596 | #[macro_export] | ||
597 | macro_rules! Fmt { | ||
598 | () => {}; | ||
599 | } | ||
600 | pub struct Fmt; | ||
601 | |||
602 | pub fn format() {} | ||
603 | pub fn no() {} | ||
604 | "#, | ||
605 | "main", | ||
606 | Query::new("").limit(2), | ||
607 | ); | ||
608 | assert_snapshot!(res, @r###" | ||
609 | dep::fmt (t) | ||
610 | dep::Fmt (t) | ||
611 | "###); | ||
612 | } | ||
573 | } | 613 | } |