From f62e8616c879255e70052ae35ce7f98bffedac11 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 18 Sep 2020 23:40:11 +0300 Subject: Add imports in auto completion --- crates/assists/src/utils/insert_use.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'crates/assists/src/utils') diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs index af3fc96b6..1aa727e11 100644 --- a/crates/assists/src/utils/insert_use.rs +++ b/crates/assists/src/utils/insert_use.rs @@ -1,6 +1,8 @@ //! Handle syntactic aspects of inserting a new `use`. use std::{cmp::Ordering, iter::successors}; +use hir::Semantics; +use ide_db::RootDatabase; use itertools::{EitherOrBoth, Itertools}; use syntax::{ algo::SyntaxRewriter, @@ -14,7 +16,7 @@ use syntax::{ use test_utils::mark; #[derive(Debug)] -pub(crate) enum ImportScope { +pub enum ImportScope { File(ast::SourceFile), Module(ast::ItemList), } @@ -31,14 +33,14 @@ impl ImportScope { } /// Determines the containing syntax node in which to insert a `use` statement affecting `position`. - pub(crate) fn find_insert_use_container( + pub fn find_insert_use_container( position: &SyntaxNode, - ctx: &crate::assist_context::AssistContext, + sema: &Semantics<'_, RootDatabase>, ) -> Option { - ctx.sema.ancestors_with_macros(position.clone()).find_map(Self::from) + sema.ancestors_with_macros(position.clone()).find_map(Self::from) } - pub(crate) fn as_syntax_node(&self) -> &SyntaxNode { + pub fn as_syntax_node(&self) -> &SyntaxNode { match self { ImportScope::File(file) => file.syntax(), ImportScope::Module(item_list) => item_list.syntax(), @@ -88,7 +90,7 @@ fn is_inner_comment(token: SyntaxToken) -> bool { } /// Insert an import path into the given file/node. A `merge` value of none indicates that no import merging is allowed to occur. -pub(crate) fn insert_use<'a>( +pub fn insert_use<'a>( scope: &ImportScope, path: ast::Path, merge: Option, -- cgit v1.2.3 From 4c8edd003aa447bd2da10fd81b24f582deacdc11 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 13 Nov 2020 19:16:56 +0200 Subject: Use imports_locator --- crates/assists/src/utils/import_assets.rs | 34 +++++++++++++++++-------------- 1 file changed, 19 insertions(+), 15 deletions(-) (limited to 'crates/assists/src/utils') diff --git a/crates/assists/src/utils/import_assets.rs b/crates/assists/src/utils/import_assets.rs index f47edbb76..89e62ba79 100644 --- a/crates/assists/src/utils/import_assets.rs +++ b/crates/assists/src/utils/import_assets.rs @@ -179,21 +179,25 @@ impl ImportAssets { } }; - let mut res = imports_locator::find_imports(sema, current_crate, &self.get_search_query()) - .into_iter() - .filter_map(filter) - .filter_map(|candidate| { - let item: hir::ItemInNs = candidate.either(Into::into, Into::into); - if let Some(prefix_kind) = prefixed { - self.module_with_name_to_import.find_use_path_prefixed(db, item, prefix_kind) - } else { - self.module_with_name_to_import.find_use_path(db, item) - } - .map(|path| (path, item)) - }) - .filter(|(use_path, _)| !use_path.segments.is_empty()) - .take(20) - .collect::>(); + let mut res = + imports_locator::find_exact_imports(sema, current_crate, &self.get_search_query()) + .filter_map(filter) + .filter_map(|candidate| { + let item: hir::ItemInNs = candidate.either(Into::into, Into::into); + if let Some(prefix_kind) = prefixed { + self.module_with_name_to_import.find_use_path_prefixed( + db, + item, + prefix_kind, + ) + } else { + self.module_with_name_to_import.find_use_path(db, item) + } + .map(|path| (path, item)) + }) + .filter(|(use_path, _)| !use_path.segments.is_empty()) + .take(20) + .collect::>(); res.sort_by_key(|(path, _)| path.clone()); res } -- cgit v1.2.3 From 38ef1fd4ad7fd26439201a1a4147a7d90a13601f Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 14 Nov 2020 11:59:23 +0200 Subject: Better filter mod paths --- crates/assists/src/utils/import_assets.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/assists/src/utils') diff --git a/crates/assists/src/utils/import_assets.rs b/crates/assists/src/utils/import_assets.rs index 89e62ba79..ff5c0e78e 100644 --- a/crates/assists/src/utils/import_assets.rs +++ b/crates/assists/src/utils/import_assets.rs @@ -195,7 +195,7 @@ impl ImportAssets { } .map(|path| (path, item)) }) - .filter(|(use_path, _)| !use_path.segments.is_empty()) + .filter(|(use_path, _)| use_path.len() > 1) .take(20) .collect::>(); res.sort_by_key(|(path, _)| path.clone()); -- cgit v1.2.3 From d4128beb3d8c647674ae43407d0ed6edd71ff420 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 16 Nov 2020 23:16:41 +0200 Subject: Avoid turning completion objects into builders --- crates/assists/src/utils/insert_use.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/assists/src/utils') diff --git a/crates/assists/src/utils/insert_use.rs b/crates/assists/src/utils/insert_use.rs index 1aa727e11..423782a0e 100644 --- a/crates/assists/src/utils/insert_use.rs +++ b/crates/assists/src/utils/insert_use.rs @@ -15,7 +15,7 @@ use syntax::{ }; use test_utils::mark; -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum ImportScope { File(ast::SourceFile), Module(ast::ItemList), -- cgit v1.2.3