Skip to content

Commit 56c7145

Browse files
author
Jonas Schievink
committed
Limit import map queries
1 parent bcf875f commit 56c7145

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,16 @@ impl Crate {
104104
db: &dyn DefDatabase,
105105
query: &str,
106106
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
107-
import_map::search_dependencies(db, self.into(), import_map::Query::new(query).anchor_end())
108-
.into_iter()
109-
.map(|item| match item {
110-
ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()),
111-
ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()),
112-
})
107+
import_map::search_dependencies(
108+
db,
109+
self.into(),
110+
import_map::Query::new(query).anchor_end().limit(40),
111+
)
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+
})
113117
}
114118

115119
pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {

crates/ra_hir_def/src/import_map.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,24 @@ fn cmp((_, lhs): &(&ItemInNs, &ModPath), (_, rhs): &(&ItemInNs, &ModPath)) -> Or
178178
pub struct Query {
179179
query: String,
180180
anchor_end: bool,
181+
limit: usize,
181182
}
182183

183184
impl Query {
184185
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() }
186187
}
187188

188189
/// Only returns items whose paths end with the (case-insensitive) query string as their last
189190
/// segment.
190191
pub fn anchor_end(self) -> Self {
191192
Self { anchor_end: true, ..self }
192193
}
194+
195+
/// Limits the returned number of items to `limit`.
196+
pub fn limit(self, limit: usize) -> Self {
197+
Self { limit, ..self }
198+
}
193199
}
194200

195201
/// Searches dependencies of `krate` for an importable path matching `query`.
@@ -237,6 +243,11 @@ pub fn search_dependencies<'a>(
237243
let item_path = &import_map.map[item];
238244
fst_path(item_path) == fst_path(path)
239245
}));
246+
247+
if res.len() >= query.limit {
248+
res.truncate(query.limit);
249+
return res;
250+
}
240251
}
241252
}
242253

@@ -570,4 +581,33 @@ mod tests {
570581
dep::Fmt (m)
571582
"###);
572583
}
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+
}
573613
}

0 commit comments

Comments
 (0)