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/ra_hir_def | |
parent | bcf875f46ae5142c42ddac8094e1b6652182d4be (diff) |
Limit import map queries
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/import_map.rs | 42 |
1 files changed, 41 insertions, 1 deletions
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 | } |