Limit import map queries

This commit is contained in:
Jonas Schievink 2020-06-10 12:30:33 +02:00
parent bcf875f46a
commit 56c7145993
2 changed files with 51 additions and 7 deletions

View file

@ -104,12 +104,16 @@ impl Crate {
db: &dyn DefDatabase, db: &dyn DefDatabase,
query: &str, query: &str,
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> { ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
import_map::search_dependencies(db, self.into(), import_map::Query::new(query).anchor_end()) import_map::search_dependencies(
.into_iter() db,
.map(|item| match item { self.into(),
ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()), import_map::Query::new(query).anchor_end().limit(40),
ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()), )
}) .into_iter()
.map(|item| match item {
ItemInNs::Types(mod_id) | ItemInNs::Values(mod_id) => Either::Left(mod_id.into()),
ItemInNs::Macros(mac_id) => Either::Right(mac_id.into()),
})
} }
pub fn all(db: &dyn HirDatabase) -> Vec<Crate> { pub fn all(db: &dyn HirDatabase) -> Vec<Crate> {

View file

@ -178,11 +178,12 @@ fn cmp((_, lhs): &(&ItemInNs, &ModPath), (_, rhs): &(&ItemInNs, &ModPath)) -> Or
pub struct Query { pub struct Query {
query: String, query: String,
anchor_end: bool, anchor_end: bool,
limit: usize,
} }
impl Query { impl Query {
pub fn new(query: &str) -> Self { pub fn new(query: &str) -> Self {
Self { query: query.to_lowercase(), anchor_end: false } Self { query: query.to_lowercase(), anchor_end: false, limit: usize::max_value() }
} }
/// Only returns items whose paths end with the (case-insensitive) query string as their last /// Only returns items whose paths end with the (case-insensitive) query string as their last
@ -190,6 +191,11 @@ impl Query {
pub fn anchor_end(self) -> Self { pub fn anchor_end(self) -> Self {
Self { anchor_end: true, ..self } Self { anchor_end: true, ..self }
} }
/// Limits the returned number of items to `limit`.
pub fn limit(self, limit: usize) -> Self {
Self { limit, ..self }
}
} }
/// Searches dependencies of `krate` for an importable path matching `query`. /// Searches dependencies of `krate` for an importable path matching `query`.
@ -237,6 +243,11 @@ pub fn search_dependencies<'a>(
let item_path = &import_map.map[item]; let item_path = &import_map.map[item];
fst_path(item_path) == fst_path(path) fst_path(item_path) == fst_path(path)
})); }));
if res.len() >= query.limit {
res.truncate(query.limit);
return res;
}
} }
} }
@ -570,4 +581,33 @@ mod tests {
dep::Fmt (m) dep::Fmt (m)
"###); "###);
} }
#[test]
fn search_limit() {
let res = search_dependencies_of(
r#"
//- /main.rs crate:main deps:dep
//- /dep.rs crate:dep
pub mod fmt {
pub trait Display {
fn fmt();
}
}
#[macro_export]
macro_rules! Fmt {
() => {};
}
pub struct Fmt;
pub fn format() {}
pub fn no() {}
"#,
"main",
Query::new("").limit(2),
);
assert_snapshot!(res, @r###"
dep::fmt (t)
dep::Fmt (t)
"###);
}
} }