aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2021-01-04 16:33:05 +0000
committerKirill Bulatov <[email protected]>2021-01-04 16:33:05 +0000
commitca42a52051e69d441b3f32ba4268286a8f0d8685 (patch)
tree217c9b610f657a3b60833c4e7b1c0bbf5c29d122 /crates
parented1ef3ae13a8f7bc2144bd06d0271d9a6e3205a0 (diff)
Code review fixes
Diffstat (limited to 'crates')
-rw-r--r--crates/completion/src/completions/unqualified_path.rs11
-rw-r--r--crates/hir_def/src/import_map.rs60
-rw-r--r--crates/ide_db/src/imports_locator.rs21
3 files changed, 50 insertions, 42 deletions
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index 068b6b407..f376ded57 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -3,7 +3,7 @@
3use std::iter; 3use std::iter;
4 4
5use either::Either; 5use either::Either;
6use hir::{Adt, AsAssocItem, ModPath, ModuleDef, ScopeDef, Type}; 6use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type};
7use ide_db::helpers::insert_use::ImportScope; 7use ide_db::helpers::insert_use::ImportScope;
8use ide_db::imports_locator; 8use ide_db::imports_locator;
9use syntax::AstNode; 9use syntax::AstNode;
@@ -142,15 +142,8 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
142 Some(40), 142 Some(40),
143 potential_import_name, 143 potential_import_name,
144 true, 144 true,
145 true,
145 ) 146 )
146 .filter(|import_candidate| match import_candidate {
147 Either::Left(ModuleDef::Function(function)) => function.as_assoc_item(ctx.db).is_none(),
148 Either::Left(ModuleDef::Const(const_)) => const_.as_assoc_item(ctx.db).is_none(),
149 Either::Left(ModuleDef::TypeAlias(type_alias)) => {
150 type_alias.as_assoc_item(ctx.db).is_none()
151 }
152 _ => true,
153 })
154 .filter_map(|import_candidate| { 147 .filter_map(|import_candidate| {
155 Some(match import_candidate { 148 Some(match import_candidate {
156 Either::Left(module_def) => { 149 Either::Left(module_def) => {
diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs
index dd3a7198f..1325a93d1 100644
--- a/crates/hir_def/src/import_map.rs
+++ b/crates/hir_def/src/import_map.rs
@@ -199,7 +199,7 @@ impl ImportMap {
199 ItemInNs::Values(module_def_id) 199 ItemInNs::Values(module_def_id)
200 }; 200 };
201 201
202 let mut assoc_item_info = original_import_info.to_owned(); 202 let mut assoc_item_info = original_import_info.clone();
203 assoc_item_info.path.segments.push(assoc_item_name.to_owned()); 203 assoc_item_info.path.segments.push(assoc_item_name.to_owned());
204 assoc_item_info.is_trait_assoc_item = true; 204 assoc_item_info.is_trait_assoc_item = true;
205 self.map.insert(assoc_item, assoc_item_info); 205 self.map.insert(assoc_item, assoc_item_info);
@@ -325,38 +325,38 @@ impl Query {
325 self.exclude_import_kinds.insert(import_kind); 325 self.exclude_import_kinds.insert(import_kind);
326 self 326 self
327 } 327 }
328}
329 328
330fn import_matches_query(import: &ImportInfo, query: &Query, enforce_lowercase: bool) -> bool { 329 fn import_matches(&self, import: &ImportInfo, enforce_lowercase: bool) -> bool {
331 let mut input = if import.is_trait_assoc_item || query.name_only { 330 let mut input = if import.is_trait_assoc_item || self.name_only {
332 import.path.segments.last().unwrap().to_string() 331 import.path.segments.last().unwrap().to_string()
333 } else { 332 } else {
334 import.path.to_string() 333 import.path.to_string()
335 }; 334 };
336 if enforce_lowercase || !query.case_sensitive { 335 if enforce_lowercase || !self.case_sensitive {
337 input.make_ascii_lowercase(); 336 input.make_ascii_lowercase();
338 } 337 }
339 338
340 let query_string = 339 let query_string =
341 if !enforce_lowercase && query.case_sensitive { &query.query } else { &query.lowercased }; 340 if !enforce_lowercase && self.case_sensitive { &self.query } else { &self.lowercased };
342 341
343 match query.search_mode { 342 match self.search_mode {
344 SearchMode::Equals => &input == query_string, 343 SearchMode::Equals => &input == query_string,
345 SearchMode::Contains => input.contains(query_string), 344 SearchMode::Contains => input.contains(query_string),
346 SearchMode::Fuzzy => { 345 SearchMode::Fuzzy => {
347 let mut unchecked_query_chars = query_string.chars(); 346 let mut unchecked_query_chars = query_string.chars();
348 let mut mismatching_query_char = unchecked_query_chars.next(); 347 let mut mismatching_query_char = unchecked_query_chars.next();
349 348
350 for input_char in input.chars() { 349 for input_char in input.chars() {
351 match mismatching_query_char { 350 match mismatching_query_char {
352 None => return true, 351 None => return true,
353 Some(matching_query_char) if matching_query_char == input_char => { 352 Some(matching_query_char) if matching_query_char == input_char => {
354 mismatching_query_char = unchecked_query_chars.next(); 353 mismatching_query_char = unchecked_query_chars.next();
354 }
355 _ => (),
355 } 356 }
356 _ => (),
357 } 357 }
358 mismatching_query_char.is_none()
358 } 359 }
359 mismatching_query_char.is_none()
360 } 360 }
361 } 361 }
362} 362}
@@ -390,7 +390,7 @@ pub fn search_dependencies<'a>(
390 let importables = &import_map.importables[indexed_value.value as usize..]; 390 let importables = &import_map.importables[indexed_value.value as usize..];
391 391
392 let common_importable_data = &import_map.map[&importables[0]]; 392 let common_importable_data = &import_map.map[&importables[0]];
393 if !import_matches_query(common_importable_data, &query, true) { 393 if !query.import_matches(common_importable_data, true) {
394 continue; 394 continue;
395 } 395 }
396 396
@@ -410,7 +410,7 @@ pub fn search_dependencies<'a>(
410 }) 410 })
411 .filter(|item| { 411 .filter(|item| {
412 !query.case_sensitive // we've already checked the common importables path case-insensitively 412 !query.case_sensitive // we've already checked the common importables path case-insensitively
413 || import_matches_query(&import_map.map[item], &query, false) 413 || query.import_matches(&import_map.map[item], false)
414 }); 414 });
415 res.extend(iter); 415 res.extend(iter);
416 416
diff --git a/crates/ide_db/src/imports_locator.rs b/crates/ide_db/src/imports_locator.rs
index 0f4c2ca47..0782ab070 100644
--- a/crates/ide_db/src/imports_locator.rs
+++ b/crates/ide_db/src/imports_locator.rs
@@ -1,7 +1,7 @@
1//! This module contains an import search funcionality that is provided to the assists module. 1//! This module contains an import search funcionality that is provided to the assists module.
2//! Later, this should be moved away to a separate crate that is accessible from the assists module. 2//! Later, this should be moved away to a separate crate that is accessible from the assists module.
3 3
4use hir::{import_map, Crate, MacroDef, ModuleDef, Semantics}; 4use hir::{import_map, AsAssocItem, Crate, MacroDef, ModuleDef, Semantics};
5use syntax::{ast, AstNode, SyntaxKind::NAME}; 5use syntax::{ast, AstNode, SyntaxKind::NAME};
6 6
7use crate::{ 7use crate::{
@@ -40,8 +40,9 @@ pub fn find_similar_imports<'a>(
40 krate: Crate, 40 krate: Crate,
41 limit: Option<usize>, 41 limit: Option<usize>,
42 fuzzy_search_string: String, 42 fuzzy_search_string: String,
43 ignore_assoc_items: bool,
43 name_only: bool, 44 name_only: bool,
44) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> { 45) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> + 'a {
45 let _p = profile::span("find_similar_imports"); 46 let _p = profile::span("find_similar_imports");
46 47
47 let mut external_query = import_map::Query::new(fuzzy_search_string.clone()) 48 let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
@@ -57,7 +58,21 @@ pub fn find_similar_imports<'a>(
57 external_query = external_query.limit(limit); 58 external_query = external_query.limit(limit);
58 } 59 }
59 60
60 find_imports(sema, krate, local_query, external_query) 61 let db = sema.db;
62 find_imports(sema, krate, local_query, external_query).filter(move |import_candidate| {
63 if ignore_assoc_items {
64 match import_candidate {
65 Either::Left(ModuleDef::Function(function)) => function.as_assoc_item(db).is_none(),
66 Either::Left(ModuleDef::Const(const_)) => const_.as_assoc_item(db).is_none(),
67 Either::Left(ModuleDef::TypeAlias(type_alias)) => {
68 type_alias.as_assoc_item(db).is_none()
69 }
70 _ => true,
71 }
72 } else {
73 true
74 }
75 })
61} 76}
62 77
63fn find_imports<'a>( 78fn find_imports<'a>(