]> 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 07ee7bdfd7ecf205dd410af5390f66ff10e9a41f..5e91a3df0a2b84d052b2747b90caad62834e8b54 100644 (file)
@@ -1,6 +1,6 @@
 //! 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};
@@ -69,81 +69,15 @@ pub struct ImportMap {
 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 = 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()));
-                    }
-                }
-            }
-        }
 
-        let mut importables = import_map.map.iter().collect::<Vec<_>>();
+        let mut import_map = collect_import_map(db, krate);
 
-        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.
 
@@ -151,20 +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 key = fst_path(&importables[last_batch_start].1.path);
-            builder.insert(key, last_batch_start as u64).unwrap();
+            let _ = builder.insert(key, last_batch_start as u64);
 
             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)
     }
@@ -185,6 +119,7 @@ fn collect_trait_assoc_items(
         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),
@@ -210,6 +145,84 @@ fn collect_trait_assoc_items(
     }
 }
 
+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.
@@ -240,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,
@@ -338,6 +346,7 @@ pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> 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;
@@ -463,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::*;
 
@@ -558,7 +567,7 @@ fn assoc_to_trait(db: &dyn DefDatabase, item: ItemInNs) -> Option<ItemInNs> {
         };
 
         match container {
-            AssocContainerId::TraitId(it) => Some(ItemInNs::Types(it.into())),
+            ItemContainerId::TraitId(it) => Some(ItemInNs::Types(it.into())),
             _ => None,
         }
     }
@@ -912,10 +921,10 @@ pub mod fmt {
                 dep::fmt (t)
                 dep::format (f)
                 dep::Fmt (v)
-                dep::fmt::Display (t)
+                dep::Fmt (m)
                 dep::Fmt (t)
                 dep::fmt::Display::fmt (a)
-                dep::Fmt (m)
+                dep::fmt::Display (t)
             "#]],
         );
 
@@ -926,9 +935,9 @@ pub mod fmt {
             expect![[r#"
                 dep::fmt (t)
                 dep::Fmt (v)
+                dep::Fmt (m)
                 dep::Fmt (t)
                 dep::fmt::Display::fmt (a)
-                dep::Fmt (m)
             "#]],
         );
 
@@ -939,10 +948,10 @@ pub mod fmt {
             expect![[r#"
                 dep::fmt (t)
                 dep::Fmt (v)
-                dep::fmt::Display (t)
+                dep::Fmt (m)
                 dep::Fmt (t)
                 dep::fmt::Display::fmt (a)
-                dep::Fmt (m)
+                dep::fmt::Display (t)
             "#]],
         );
     }
@@ -980,10 +989,10 @@ pub mod fmt {
             expect![[r#"
                 dep::fmt (t)
                 dep::Fmt (v)
-                dep::fmt::Display (t)
+                dep::Fmt (m)
                 dep::Fmt (t)
                 dep::fmt::Display::fmt (a)
-                dep::Fmt (m)
+                dep::fmt::Display (t)
             "#]],
         );
 
@@ -994,9 +1003,9 @@ pub mod fmt {
             expect![[r#"
                 dep::fmt (t)
                 dep::Fmt (v)
+                dep::Fmt (m)
                 dep::Fmt (t)
                 dep::fmt::Display::fmt (a)
-                dep::Fmt (m)
             "#]],
         );
     }
@@ -1058,8 +1067,8 @@ pub fn no() {}
             Query::new("".to_string()).limit(2),
             expect![[r#"
                 dep::fmt (t)
-                dep::Fmt (t)
                 dep::Fmt (m)
+                dep::Fmt (t)
                 dep::Fmt (v)
             "#]],
         );
@@ -1094,27 +1103,4 @@ fn search_exclusions() {
             expect![[r#""#]],
         );
     }
-
-    #[test]
-    fn search_with_path() {
-        check_search(
-            r#"
-//- /main.rs crate:main deps:dep
-//- /dep.rs crate:dep
-pub mod foo {
-    pub mod bar {
-        pub mod baz {
-            pub trait Display {
-                fn fmt();
-            }
-        }
-    }
-}"#,
-            "main",
-            Query::new("baz::fmt".to_string()).search_mode(SearchMode::Fuzzy),
-            expect![[r#"
-                dep::foo::bar::baz::Display::fmt (a)
-            "#]],
-        );
-    }
 }