]> git.lizzy.rs Git - rust.git/commitdiff
Code review fixes
authorKirill Bulatov <mail4score@gmail.com>
Mon, 4 Jan 2021 16:33:05 +0000 (18:33 +0200)
committerKirill Bulatov <mail4score@gmail.com>
Mon, 4 Jan 2021 16:33:05 +0000 (18:33 +0200)
crates/completion/src/completions/unqualified_path.rs
crates/hir_def/src/import_map.rs
crates/ide_db/src/imports_locator.rs

index 068b6b407e8efb6727e6904944233f08bee0471a..f376ded57bdc48333962fe4b771f9434d10c757c 100644 (file)
@@ -3,7 +3,7 @@
 use std::iter;
 
 use either::Either;
-use hir::{Adt, AsAssocItem, ModPath, ModuleDef, ScopeDef, Type};
+use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type};
 use ide_db::helpers::insert_use::ImportScope;
 use ide_db::imports_locator;
 use syntax::AstNode;
@@ -142,15 +142,8 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
         Some(40),
         potential_import_name,
         true,
+        true,
     )
-    .filter(|import_candidate| match import_candidate {
-        Either::Left(ModuleDef::Function(function)) => function.as_assoc_item(ctx.db).is_none(),
-        Either::Left(ModuleDef::Const(const_)) => const_.as_assoc_item(ctx.db).is_none(),
-        Either::Left(ModuleDef::TypeAlias(type_alias)) => {
-            type_alias.as_assoc_item(ctx.db).is_none()
-        }
-        _ => true,
-    })
     .filter_map(|import_candidate| {
         Some(match import_candidate {
             Either::Left(module_def) => {
index dd3a7198f081e8c4b33ab1903409bbff76dc4a99..1325a93d1382965ab5045d428718e8772272b2d0 100644 (file)
@@ -199,7 +199,7 @@ fn collect_trait_assoc_items(
                 ItemInNs::Values(module_def_id)
             };
 
-            let mut assoc_item_info = original_import_info.to_owned();
+            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);
@@ -325,38 +325,38 @@ pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> Self {
         self.exclude_import_kinds.insert(import_kind);
         self
     }
-}
 
-fn import_matches_query(import: &ImportInfo, query: &Query, enforce_lowercase: bool) -> bool {
-    let mut input = if import.is_trait_assoc_item || query.name_only {
-        import.path.segments.last().unwrap().to_string()
-    } else {
-        import.path.to_string()
-    };
-    if enforce_lowercase || !query.case_sensitive {
-        input.make_ascii_lowercase();
-    }
+    fn import_matches(&self, import: &ImportInfo, enforce_lowercase: bool) -> bool {
+        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 && query.case_sensitive { &query.query } else { &query.lowercased };
-
-    match query.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();
+        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()
             }
-            mismatching_query_char.is_none()
         }
     }
 }
@@ -390,7 +390,7 @@ pub fn search_dependencies<'a>(
             let importables = &import_map.importables[indexed_value.value as usize..];
 
             let common_importable_data = &import_map.map[&importables[0]];
-            if !import_matches_query(common_importable_data, &query, true) {
+            if !query.import_matches(common_importable_data, true) {
                 continue;
             }
 
@@ -410,7 +410,7 @@ pub fn search_dependencies<'a>(
                 })
                 .filter(|item| {
                     !query.case_sensitive // we've already checked the common importables path case-insensitively
-                        || import_matches_query(&import_map.map[item], &query, false)
+                        || query.import_matches(&import_map.map[item], false)
                 });
             res.extend(iter);
 
index 0f4c2ca473b36a64507c0fa8bc64dedae0204d32..0782ab070957ee75038e889bfa7590d9270f9e89 100644 (file)
@@ -1,7 +1,7 @@
 //! This module contains an import search funcionality that is provided to the assists module.
 //! Later, this should be moved away to a separate crate that is accessible from the assists module.
 
-use hir::{import_map, Crate, MacroDef, ModuleDef, Semantics};
+use hir::{import_map, AsAssocItem, Crate, MacroDef, ModuleDef, Semantics};
 use syntax::{ast, AstNode, SyntaxKind::NAME};
 
 use crate::{
@@ -40,8 +40,9 @@ pub fn find_similar_imports<'a>(
     krate: Crate,
     limit: Option<usize>,
     fuzzy_search_string: String,
+    ignore_assoc_items: bool,
     name_only: bool,
-) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
+) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> + 'a {
     let _p = profile::span("find_similar_imports");
 
     let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
@@ -57,7 +58,21 @@ pub fn find_similar_imports<'a>(
         external_query = external_query.limit(limit);
     }
 
-    find_imports(sema, krate, local_query, external_query)
+    let db = sema.db;
+    find_imports(sema, krate, local_query, external_query).filter(move |import_candidate| {
+        if ignore_assoc_items {
+            match import_candidate {
+                Either::Left(ModuleDef::Function(function)) => function.as_assoc_item(db).is_none(),
+                Either::Left(ModuleDef::Const(const_)) => const_.as_assoc_item(db).is_none(),
+                Either::Left(ModuleDef::TypeAlias(type_alias)) => {
+                    type_alias.as_assoc_item(db).is_none()
+                }
+                _ => true,
+            }
+        } else {
+            true
+        }
+    })
 }
 
 fn find_imports<'a>(