]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_def/src/import_map.rs
parameters.split_last()
[rust.git] / crates / hir_def / src / import_map.rs
index c0f10884808e6516148f858bd0934c3c026a269d..5e91a3df0a2b84d052b2747b90caad62834e8b54 100644 (file)
@@ -1,15 +1,13 @@
 //! A map of all publicly exported items in a crate.
 
-use std::{cmp::Ordering, fmt, hash::BuildHasherDefault, sync::Arc};
+use std::{fmt, hash::BuildHasherDefault, sync::Arc};
 
 use base_db::CrateId;
 use fst::{self, Streamer};
 use hir_expand::name::Name;
 use indexmap::{map::Entry, IndexMap};
 use itertools::Itertools;
-use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
-use smallvec::SmallVec;
-use syntax::SmolStr;
+use rustc_hash::{FxHashSet, FxHasher};
 
 use crate::{
     db::DefDatabase, item_scope::ItemInNs, visibility::Visibility, AssocItemId, ModuleDefId,
@@ -25,6 +23,8 @@ pub struct ImportInfo {
     pub path: ImportPath,
     /// The module containing this item.
     pub container: ModuleId,
+    /// Whether the import is a trait associated item or not.
+    pub is_trait_assoc_item: bool,
 }
 
 #[derive(Debug, Clone, Eq, PartialEq)]
@@ -64,82 +64,20 @@ pub struct ImportMap {
     /// the index of the first one.
     importables: Vec<ItemInNs>,
     fst: fst::Map<Vec<u8>>,
-
-    /// Maps names of associated items to the item's ID. Only includes items whose defining trait is
-    /// exported.
-    assoc_map: FxHashMap<SmolStr, SmallVec<[AssocItemId; 1]>>,
 }
 
 impl ImportMap {
     pub fn import_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<Self> {
         let _p = profile::span("import_map_query");
-        let def_map = db.crate_def_map(krate);
-        let mut import_map = Self::default();
-
-        // We look only into modules that are public(ly reexported), starting with the crate root.
-        let empty = ImportPath { segments: vec![] };
-        let root = ModuleId { krate, local_id: def_map.root };
-        let mut worklist = vec![(root, empty)];
-        while let Some((module, mod_path)) = worklist.pop() {
-            let ext_def_map;
-            let mod_data = if module.krate == krate {
-                &def_map[module.local_id]
-            } else {
-                // The crate might reexport a module defined in another crate.
-                ext_def_map = db.crate_def_map(module.krate);
-                &ext_def_map[module.local_id]
-            };
-
-            let visible_items = mod_data.scope.entries().filter_map(|(name, per_ns)| {
-                let per_ns = per_ns.filter_visibility(|vis| vis == Visibility::Public);
-                if per_ns.is_none() {
-                    None
-                } else {
-                    Some((name, per_ns))
-                }
-            });
-
-            for (name, per_ns) in visible_items {
-                let mk_path = || {
-                    let mut path = mod_path.clone();
-                    path.segments.push(name.clone());
-                    path
-                };
-
-                for item in per_ns.iter_items() {
-                    let path = mk_path();
-                    match import_map.map.entry(item) {
-                        Entry::Vacant(entry) => {
-                            entry.insert(ImportInfo { path, container: module });
-                        }
-                        Entry::Occupied(mut entry) => {
-                            // If the new path is shorter, prefer that one.
-                            if path.len() < entry.get().path.len() {
-                                *entry.get_mut() = ImportInfo { path, container: module };
-                            } else {
-                                continue;
-                            }
-                        }
-                    }
-
-                    // If we've just added a path to a module, descend into it. We might traverse
-                    // modules multiple times, but only if the new path to it is shorter than the
-                    // first (else we `continue` above).
-                    if let Some(ModuleDefId::ModuleId(mod_id)) = item.as_module_def_id() {
-                        worklist.push((mod_id, mk_path()));
-                    }
 
-                    // If we've added a path to a trait, add the trait's methods to the method map.
-                    if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() {
-                        import_map.collect_trait_methods(db, tr);
-                    }
-                }
-            }
-        }
+        let mut import_map = collect_import_map(db, krate);
 
-        let mut importables = import_map.map.iter().collect::<Vec<_>>();
-
-        importables.sort_by(cmp);
+        let mut importables = import_map
+            .map
+            .iter()
+            .map(|(item, info)| (item, fst_path(&info.path)))
+            .collect::<Vec<_>>();
+        importables.sort_by(|(_, fst_path), (_, fst_path2)| fst_path.cmp(fst_path2));
 
         // Build the FST, taking care not to insert duplicate values.
 
@@ -147,22 +85,20 @@ pub fn import_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<Self> {
         let mut last_batch_start = 0;
 
         for idx in 0..importables.len() {
-            if let Some(next_item) = importables.get(idx + 1) {
-                if cmp(&importables[last_batch_start], next_item) == Ordering::Equal {
+            let key = &importables[last_batch_start].1;
+            if let Some((_, fst_path)) = importables.get(idx + 1) {
+                if key == fst_path {
                     continue;
                 }
             }
 
-            let start = last_batch_start;
-            last_batch_start = idx + 1;
-
-            let key = fst_path(&importables[start].1.path);
+            let _ = builder.insert(key, last_batch_start as u64);
 
-            builder.insert(key, start as u64).unwrap();
+            last_batch_start = idx + 1;
         }
 
-        import_map.fst = fst::Map::new(builder.into_inner().unwrap()).unwrap();
-        import_map.importables = importables.iter().map(|(item, _)| **item).collect();
+        import_map.fst = builder.into_map();
+        import_map.importables = importables.iter().map(|&(&item, _)| item).collect();
 
         Arc::new(import_map)
     }
@@ -176,14 +112,117 @@ pub fn import_info_for(&self, item: ItemInNs) -> Option<&ImportInfo> {
         self.map.get(&item)
     }
 
-    fn collect_trait_methods(&mut self, db: &dyn DefDatabase, tr: TraitId) {
-        let data = db.trait_data(tr);
-        for (name, item) in data.items.iter() {
-            self.assoc_map.entry(name.to_string().into()).or_default().push(*item);
+    fn collect_trait_assoc_items(
+        &mut self,
+        db: &dyn DefDatabase,
+        tr: TraitId,
+        is_type_in_ns: bool,
+        original_import_info: &ImportInfo,
+    ) {
+        let _p = profile::span("collect_trait_assoc_items");
+        for (assoc_item_name, item) in &db.trait_data(tr).items {
+            let module_def_id = match item {
+                AssocItemId::FunctionId(f) => ModuleDefId::from(*f),
+                AssocItemId::ConstId(c) => ModuleDefId::from(*c),
+                // cannot use associated type aliases directly: need a `<Struct as Trait>::TypeAlias`
+                // qualifier, ergo no need to store it for imports in import_map
+                AssocItemId::TypeAliasId(_) => {
+                    cov_mark::hit!(type_aliases_ignored);
+                    continue;
+                }
+            };
+            let assoc_item = if is_type_in_ns {
+                ItemInNs::Types(module_def_id)
+            } else {
+                ItemInNs::Values(module_def_id)
+            };
+
+            let mut assoc_item_info = original_import_info.clone();
+            assoc_item_info.path.segments.push(assoc_item_name.to_owned());
+            assoc_item_info.is_trait_assoc_item = true;
+            self.map.insert(assoc_item, assoc_item_info);
         }
     }
 }
 
+fn collect_import_map(db: &dyn DefDatabase, krate: CrateId) -> ImportMap {
+    let _p = profile::span("collect_import_map");
+
+    let def_map = db.crate_def_map(krate);
+    let mut import_map = ImportMap::default();
+
+    // We look only into modules that are public(ly reexported), starting with the crate root.
+    let empty = ImportPath { segments: vec![] };
+    let root = def_map.module_id(def_map.root());
+    let mut worklist = vec![(root, empty)];
+    while let Some((module, mod_path)) = worklist.pop() {
+        let ext_def_map;
+        let mod_data = if module.krate == krate {
+            &def_map[module.local_id]
+        } else {
+            // The crate might reexport a module defined in another crate.
+            ext_def_map = module.def_map(db);
+            &ext_def_map[module.local_id]
+        };
+
+        let visible_items = mod_data.scope.entries().filter_map(|(name, per_ns)| {
+            let per_ns = per_ns.filter_visibility(|vis| vis == Visibility::Public);
+            if per_ns.is_none() {
+                None
+            } else {
+                Some((name, per_ns))
+            }
+        });
+
+        for (name, per_ns) in visible_items {
+            let mk_path = || {
+                let mut path = mod_path.clone();
+                path.segments.push(name.clone());
+                path
+            };
+
+            for item in per_ns.iter_items() {
+                let path = mk_path();
+                let path_len = path.len();
+                let import_info =
+                    ImportInfo { path, container: module, is_trait_assoc_item: false };
+
+                if let Some(ModuleDefId::TraitId(tr)) = item.as_module_def_id() {
+                    import_map.collect_trait_assoc_items(
+                        db,
+                        tr,
+                        matches!(item, ItemInNs::Types(_)),
+                        &import_info,
+                    );
+                }
+
+                match import_map.map.entry(item) {
+                    Entry::Vacant(entry) => {
+                        entry.insert(import_info);
+                    }
+                    Entry::Occupied(mut entry) => {
+                        // If the new path is shorter, prefer that one.
+                        if path_len < entry.get().path.len() {
+                            *entry.get_mut() = import_info;
+                        } else {
+                            continue;
+                        }
+                    }
+                }
+
+                // If we've just added a path to a module, descend into it. We might traverse
+                // modules multiple times, but only if the new path to it is shorter than the
+                // first (else we `continue` above).
+                if let Some(ModuleDefId::ModuleId(mod_id)) = item.as_module_def_id() {
+                    worklist.push((mod_id, mk_path()));
+                }
+            }
+        }
+    }
+
+    import_map
+}
+
 impl PartialEq for ImportMap {
     fn eq(&self, other: &Self) -> bool {
         // `fst` and `importables` are built from `map`, so we don't need to compare them.
@@ -214,17 +253,12 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 }
 
 fn fst_path(path: &ImportPath) -> String {
+    let _p = profile::span("fst_path");
     let mut s = path.to_string();
     s.make_ascii_lowercase();
     s
 }
 
-fn cmp((_, lhs): &(&ItemInNs, &ImportInfo), (_, rhs): &(&ItemInNs, &ImportInfo)) -> Ordering {
-    let lhs_str = fst_path(&lhs.path);
-    let rhs_str = fst_path(&rhs.path);
-    lhs_str.cmp(&rhs_str)
-}
-
 #[derive(Debug, Eq, PartialEq, Hash)]
 pub enum ImportKind {
     Module,
@@ -236,34 +270,63 @@ pub enum ImportKind {
     Trait,
     TypeAlias,
     BuiltinType,
+    AssociatedItem,
+}
+
+/// A way to match import map contents against the search query.
+#[derive(Debug)]
+pub enum SearchMode {
+    /// Import map entry should strictly match the query string.
+    Equals,
+    /// Import map entry should contain the query string.
+    Contains,
+    /// Import map entry should contain all letters from the query string,
+    /// in the same order, but not necessary adjacent.
+    Fuzzy,
 }
 
 #[derive(Debug)]
 pub struct Query {
     query: String,
     lowercased: String,
-    anchor_end: bool,
+    name_only: bool,
+    assoc_items_only: bool,
+    search_mode: SearchMode,
     case_sensitive: bool,
     limit: usize,
     exclude_import_kinds: FxHashSet<ImportKind>,
 }
 
 impl Query {
-    pub fn new(query: &str) -> Self {
+    pub fn new(query: String) -> Self {
+        let lowercased = query.to_lowercase();
         Self {
-            lowercased: query.to_lowercase(),
-            query: query.to_string(),
-            anchor_end: false,
+            query,
+            lowercased,
+            name_only: false,
+            assoc_items_only: false,
+            search_mode: SearchMode::Contains,
             case_sensitive: false,
             limit: usize::max_value(),
             exclude_import_kinds: FxHashSet::default(),
         }
     }
 
-    /// Only returns items whose paths end with the (case-insensitive) query string as their last
-    /// segment.
-    pub fn anchor_end(self) -> Self {
-        Self { anchor_end: true, ..self }
+    /// Matches entries' names only, ignoring the rest of
+    /// the qualifier.
+    /// Example: for `std::marker::PhantomData`, the name is `PhantomData`.
+    pub fn name_only(self) -> Self {
+        Self { name_only: true, ..self }
+    }
+
+    /// Matches only the entries that are associated items, ignoring the rest.
+    pub fn assoc_items_only(self) -> Self {
+        Self { assoc_items_only: true, ..self }
+    }
+
+    /// Specifies the way to search for the entries using the query.
+    pub fn search_mode(self, search_mode: SearchMode) -> Self {
+        Self { search_mode, ..self }
     }
 
     /// Limits the returned number of items to `limit`.
@@ -281,6 +344,49 @@ pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> Self {
         self.exclude_import_kinds.insert(import_kind);
         self
     }
+
+    fn import_matches(&self, import: &ImportInfo, enforce_lowercase: bool) -> bool {
+        let _p = profile::span("import_map::Query::import_matches");
+        if import.is_trait_assoc_item {
+            if self.exclude_import_kinds.contains(&ImportKind::AssociatedItem) {
+                return false;
+            }
+        } else if self.assoc_items_only {
+            return false;
+        }
+
+        let mut input = if import.is_trait_assoc_item || self.name_only {
+            import.path.segments.last().unwrap().to_string()
+        } else {
+            import.path.to_string()
+        };
+        if enforce_lowercase || !self.case_sensitive {
+            input.make_ascii_lowercase();
+        }
+
+        let query_string =
+            if !enforce_lowercase && self.case_sensitive { &self.query } else { &self.lowercased };
+
+        match self.search_mode {
+            SearchMode::Equals => &input == query_string,
+            SearchMode::Contains => input.contains(query_string),
+            SearchMode::Fuzzy => {
+                let mut unchecked_query_chars = query_string.chars();
+                let mut mismatching_query_char = unchecked_query_chars.next();
+
+                for input_char in input.chars() {
+                    match mismatching_query_char {
+                        None => return true,
+                        Some(matching_query_char) if matching_query_char == input_char => {
+                            mismatching_query_char = unchecked_query_chars.next();
+                        }
+                        _ => (),
+                    }
+                }
+                mismatching_query_char.is_none()
+            }
+        }
+    }
 }
 
 /// Searches dependencies of `krate` for an importable path matching `query`.
@@ -290,7 +396,7 @@ pub fn search_dependencies<'a>(
     db: &'a dyn DefDatabase,
     krate: CrateId,
     query: Query,
-) -> Vec<ItemInNs> {
+) -> FxHashSet<ItemInNs> {
     let _p = profile::span("search_dependencies").detail(|| format!("{:?}", query));
 
     let graph = db.crate_graph();
@@ -305,64 +411,42 @@ pub fn search_dependencies<'a>(
     }
 
     let mut stream = op.union();
-    let mut res = Vec::new();
-    while let Some((_, indexed_values)) = stream.next() {
-        for indexed_value in indexed_values {
-            let import_map = &import_maps[indexed_value.index];
-            let importables = &import_map.importables[indexed_value.value as usize..];
-
-            // Path shared by the importable items in this group.
-            let path = &import_map.map[&importables[0]].path;
 
-            if query.anchor_end {
-                // Last segment must match query.
-                let last = path.segments.last().unwrap().to_string();
-                if last.to_lowercase() != query.lowercased {
-                    continue;
-                }
-            }
+    let mut all_indexed_values = FxHashSet::default();
+    while let Some((_, indexed_values)) = stream.next() {
+        all_indexed_values.extend(indexed_values.iter().copied());
+    }
 
-            // Add the items from this `ModPath` group. Those are all subsequent items in
-            // `importables` whose paths match `path`.
-            let iter = importables
-                .iter()
-                .copied()
-                .take_while(|item| {
-                    let item_path = &import_map.map[item].path;
-                    fst_path(item_path) == fst_path(path)
-                })
-                .filter(|&item| match item_import_kind(item) {
-                    Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
-                    None => true,
-                });
-
-            if query.case_sensitive {
-                // FIXME: This does not do a subsequence match.
-                res.extend(iter.filter(|item| {
-                    let item_path = &import_map.map[item].path;
-                    item_path.to_string().contains(&query.query)
-                }));
-            } else {
-                res.extend(iter);
-            }
+    let mut res = FxHashSet::default();
+    for indexed_value in all_indexed_values {
+        let import_map = &import_maps[indexed_value.index];
+        let importables = &import_map.importables[indexed_value.value as usize..];
 
-            if res.len() >= query.limit {
-                res.truncate(query.limit);
-                return res;
-            }
+        let common_importable_data = &import_map.map[&importables[0]];
+        if !query.import_matches(common_importable_data, true) {
+            continue;
         }
-    }
 
-    // Add all exported associated items whose names match the query (exactly).
-    for map in &import_maps {
-        if let Some(v) = map.assoc_map.get(&*query.query) {
-            res.extend(v.iter().map(|&assoc| {
-                ItemInNs::Types(match assoc {
-                    AssocItemId::FunctionId(it) => it.into(),
-                    AssocItemId::ConstId(it) => it.into(),
-                    AssocItemId::TypeAliasId(it) => it.into(),
-                })
-            }));
+        // Path shared by the importable items in this group.
+        let common_importables_path_fst = fst_path(&common_importable_data.path);
+        // Add the items from this `ModPath` group. Those are all subsequent items in
+        // `importables` whose paths match `path`.
+        let iter = importables
+            .iter()
+            .copied()
+            .take_while(|item| common_importables_path_fst == fst_path(&import_map.map[item].path))
+            .filter(|&item| match item_import_kind(item) {
+                Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
+                None => true,
+            })
+            .filter(|item| {
+                !query.case_sensitive // we've already checked the common importables path case-insensitively
+                        || query.import_matches(&import_map.map[item], false)
+            });
+        res.extend(iter);
+
+        if res.len() >= query.limit {
+            return res;
         }
     }
 
@@ -388,7 +472,7 @@ mod tests {
     use base_db::{fixture::WithFixture, SourceDatabase, Upcast};
     use expect_test::{expect, Expect};
 
-    use crate::{test_db::TestDB, AssocContainerId, Lookup};
+    use crate::{test_db::TestDB, ItemContainerId, Lookup};
 
     use super::*;
 
@@ -405,37 +489,75 @@ fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect
 
         let actual = search_dependencies(db.upcast(), krate, query)
             .into_iter()
-            .filter_map(|item| {
-                let mark = match item {
-                    ItemInNs::Types(_) => "t",
-                    ItemInNs::Values(_) => "v",
-                    ItemInNs::Macros(_) => "m",
+            .filter_map(|dependency| {
+                let dependency_krate = dependency.krate(db.upcast())?;
+                let dependency_imports = db.import_map(dependency_krate);
+
+                let (path, mark) = match assoc_item_path(&db, &dependency_imports, dependency) {
+                    Some(assoc_item_path) => (assoc_item_path, "a"),
+                    None => (
+                        dependency_imports.path_of(dependency)?.to_string(),
+                        match dependency {
+                            ItemInNs::Types(ModuleDefId::FunctionId(_))
+                            | ItemInNs::Values(ModuleDefId::FunctionId(_)) => "f",
+                            ItemInNs::Types(_) => "t",
+                            ItemInNs::Values(_) => "v",
+                            ItemInNs::Macros(_) => "m",
+                        },
+                    ),
                 };
-                let item = assoc_to_trait(&db, item);
-                item.krate(db.upcast()).map(|krate| {
-                    let map = db.import_map(krate);
-                    let path = map.path_of(item).unwrap();
-                    format!(
-                        "{}::{} ({})\n",
-                        crate_graph[krate].display_name.as_ref().unwrap(),
-                        path,
-                        mark
-                    )
-                })
+
+                Some(format!(
+                    "{}::{} ({})\n",
+                    crate_graph[dependency_krate].display_name.as_ref()?,
+                    path,
+                    mark
+                ))
             })
             .collect::<String>();
         expect.assert_eq(&actual)
     }
 
-    fn assoc_to_trait(db: &dyn DefDatabase, item: ItemInNs) -> ItemInNs {
+    fn assoc_item_path(
+        db: &dyn DefDatabase,
+        dependency_imports: &ImportMap,
+        dependency: ItemInNs,
+    ) -> Option<String> {
+        let dependency_assoc_item_id = match dependency {
+            ItemInNs::Types(ModuleDefId::FunctionId(id))
+            | ItemInNs::Values(ModuleDefId::FunctionId(id)) => AssocItemId::from(id),
+            ItemInNs::Types(ModuleDefId::ConstId(id))
+            | ItemInNs::Values(ModuleDefId::ConstId(id)) => AssocItemId::from(id),
+            ItemInNs::Types(ModuleDefId::TypeAliasId(id))
+            | ItemInNs::Values(ModuleDefId::TypeAliasId(id)) => AssocItemId::from(id),
+            _ => return None,
+        };
+
+        let trait_ = assoc_to_trait(db, dependency)?;
+        if let ModuleDefId::TraitId(tr) = trait_.as_module_def_id()? {
+            let trait_data = db.trait_data(tr);
+            let assoc_item_name =
+                trait_data.items.iter().find_map(|(assoc_item_name, assoc_item_id)| {
+                    if &dependency_assoc_item_id == assoc_item_id {
+                        Some(assoc_item_name)
+                    } else {
+                        None
+                    }
+                })?;
+            return Some(format!("{}::{}", dependency_imports.path_of(trait_)?, assoc_item_name));
+        }
+        None
+    }
+
+    fn assoc_to_trait(db: &dyn DefDatabase, item: ItemInNs) -> Option<ItemInNs> {
         let assoc: AssocItemId = match item {
             ItemInNs::Types(it) | ItemInNs::Values(it) => match it {
                 ModuleDefId::TypeAliasId(it) => it.into(),
                 ModuleDefId::FunctionId(it) => it.into(),
                 ModuleDefId::ConstId(it) => it.into(),
-                _ => return item,
+                _ => return None,
             },
-            _ => return item,
+            _ => return None,
         };
 
         let container = match assoc {
@@ -445,8 +567,8 @@ fn assoc_to_trait(db: &dyn DefDatabase, item: ItemInNs) -> ItemInNs {
         };
 
         match container {
-            AssocContainerId::TraitId(it) => ItemInNs::Types(it.into()),
-            _ => item,
+            ItemContainerId::TraitId(it) => Some(ItemInNs::Types(it.into())),
+            _ => None,
         }
     }
 
@@ -685,7 +807,88 @@ macro_rules! Thing {  // m
     }
 
     #[test]
-    fn search() {
+    fn fuzzy_import_trait_and_assoc_items() {
+        cov_mark::check!(type_aliases_ignored);
+        let ra_fixture = r#"
+        //- /main.rs crate:main deps:dep
+        //- /dep.rs crate:dep
+        pub mod fmt {
+            pub trait Display {
+                type FmtTypeAlias;
+                const FMT_CONST: bool;
+
+                fn format_function();
+                fn format_method(&self);
+            }
+        }
+    "#;
+
+        check_search(
+            ra_fixture,
+            "main",
+            Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
+            expect![[r#"
+                dep::fmt (t)
+                dep::fmt::Display::format_method (a)
+                dep::fmt::Display (t)
+                dep::fmt::Display::FMT_CONST (a)
+                dep::fmt::Display::format_function (a)
+            "#]],
+        );
+    }
+
+    #[test]
+    fn assoc_items_filtering() {
+        let ra_fixture = r#"
+        //- /main.rs crate:main deps:dep
+        //- /dep.rs crate:dep
+        pub mod fmt {
+            pub trait Display {
+                type FmtTypeAlias;
+                const FMT_CONST: bool;
+
+                fn format_function();
+                fn format_method(&self);
+            }
+        }
+    "#;
+
+        check_search(
+            ra_fixture,
+            "main",
+            Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy).assoc_items_only(),
+            expect![[r#"
+            dep::fmt::Display::format_method (a)
+            dep::fmt::Display::FMT_CONST (a)
+            dep::fmt::Display::format_function (a)
+        "#]],
+        );
+
+        check_search(
+            ra_fixture,
+            "main",
+            Query::new("fmt".to_string())
+                .search_mode(SearchMode::Fuzzy)
+                .exclude_import_kind(ImportKind::AssociatedItem),
+            expect![[r#"
+            dep::fmt (t)
+            dep::fmt::Display (t)
+        "#]],
+        );
+
+        check_search(
+            ra_fixture,
+            "main",
+            Query::new("fmt".to_string())
+                .search_mode(SearchMode::Fuzzy)
+                .assoc_items_only()
+                .exclude_import_kind(ImportKind::AssociatedItem),
+            expect![[r#""#]],
+        );
+    }
+
+    #[test]
+    fn search_mode() {
         let ra_fixture = r#"
             //- /main.rs crate:main deps:dep
             //- /dep.rs crate:dep deps:tdep
@@ -713,14 +916,14 @@ pub mod fmt {
         check_search(
             ra_fixture,
             "main",
-            Query::new("fmt"),
+            Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
             expect![[r#"
                 dep::fmt (t)
-                dep::Fmt (t)
+                dep::format (f)
                 dep::Fmt (v)
                 dep::Fmt (m)
-                dep::fmt::Display (t)
-                dep::format (v)
+                dep::Fmt (t)
+                dep::fmt::Display::fmt (a)
                 dep::fmt::Display (t)
             "#]],
         );
@@ -728,15 +931,83 @@ pub mod fmt {
         check_search(
             ra_fixture,
             "main",
-            Query::new("fmt").anchor_end(),
+            Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
             expect![[r#"
                 dep::fmt (t)
+                dep::Fmt (v)
+                dep::Fmt (m)
+                dep::Fmt (t)
+                dep::fmt::Display::fmt (a)
+            "#]],
+        );
+
+        check_search(
+            ra_fixture,
+            "main",
+            Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
+            expect![[r#"
+                dep::fmt (t)
+                dep::Fmt (v)
+                dep::Fmt (m)
                 dep::Fmt (t)
+                dep::fmt::Display::fmt (a)
+                dep::fmt::Display (t)
+            "#]],
+        );
+    }
+
+    #[test]
+    fn name_only() {
+        let ra_fixture = r#"
+            //- /main.rs crate:main deps:dep
+            //- /dep.rs crate:dep deps:tdep
+            use tdep::fmt as fmt_dep;
+            pub mod fmt {
+                pub trait Display {
+                    fn fmt();
+                }
+            }
+            #[macro_export]
+            macro_rules! Fmt {
+                () => {};
+            }
+            pub struct Fmt;
+
+            pub fn format() {}
+            pub fn no() {}
+
+            //- /tdep.rs crate:tdep
+            pub mod fmt {
+                pub struct NotImportableFromMain;
+            }
+        "#;
+
+        check_search(
+            ra_fixture,
+            "main",
+            Query::new("fmt".to_string()),
+            expect![[r#"
+                dep::fmt (t)
                 dep::Fmt (v)
                 dep::Fmt (m)
+                dep::Fmt (t)
+                dep::fmt::Display::fmt (a)
                 dep::fmt::Display (t)
             "#]],
         );
+
+        check_search(
+            ra_fixture,
+            "main",
+            Query::new("fmt".to_string()).name_only(),
+            expect![[r#"
+                dep::fmt (t)
+                dep::Fmt (v)
+                dep::Fmt (m)
+                dep::Fmt (t)
+                dep::fmt::Display::fmt (a)
+            "#]],
+        );
     }
 
     #[test]
@@ -752,19 +1023,19 @@ fn search_casing() {
         check_search(
             ra_fixture,
             "main",
-            Query::new("FMT"),
+            Query::new("FMT".to_string()),
             expect![[r#"
                 dep::fmt (t)
+                dep::FMT (v)
                 dep::fmt (v)
                 dep::FMT (t)
-                dep::FMT (v)
             "#]],
         );
 
         check_search(
             ra_fixture,
             "main",
-            Query::new("FMT").case_sensitive(),
+            Query::new("FMT".to_string()).case_sensitive(),
             expect![[r#"
                 dep::FMT (t)
                 dep::FMT (v)
@@ -793,10 +1064,12 @@ pub fn format() {}
         pub fn no() {}
     "#,
             "main",
-            Query::new("").limit(2),
+            Query::new("".to_string()).limit(2),
             expect![[r#"
                 dep::fmt (t)
+                dep::Fmt (m)
                 dep::Fmt (t)
+                dep::Fmt (v)
             "#]],
         );
     }
@@ -814,19 +1087,19 @@ fn search_exclusions() {
         check_search(
             ra_fixture,
             "main",
-            Query::new("FMT"),
+            Query::new("FMT".to_string()),
             expect![[r#"
                 dep::fmt (t)
+                dep::FMT (v)
                 dep::fmt (v)
                 dep::FMT (t)
-                dep::FMT (v)
             "#]],
         );
 
         check_search(
             ra_fixture,
             "main",
-            Query::new("FMT").exclude_import_kind(ImportKind::Adt),
+            Query::new("FMT".to_string()).exclude_import_kind(ImportKind::Adt),
             expect![[r#""#]],
         );
     }