From 6dae5cbb1190cde6a20aa1758c7d87e84933378e Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 2 Feb 2020 14:06:51 +0200 Subject: Require ModPath for importing --- crates/ra_assists/src/assists/add_import.rs | 9 +++++---- crates/ra_assists/src/assists/auto_import.rs | 9 +++------ 2 files changed, 8 insertions(+), 10 deletions(-) (limited to 'crates/ra_assists/src/assists') diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs index bf6cfe865..96a494c93 100644 --- a/crates/ra_assists/src/assists/add_import.rs +++ b/crates/ra_assists/src/assists/add_import.rs @@ -1,4 +1,4 @@ -use hir::{self, db::HirDatabase}; +use hir::{self, db::HirDatabase, ModPath}; use ra_syntax::{ ast::{self, NameOwner}, AstNode, Direction, SmolStr, @@ -21,9 +21,10 @@ pub fn auto_import_text_edit( // The statement to use as anchor (last resort) anchor: &SyntaxNode, // The path to import as a sequence of strings - target: &[SmolStr], + path_to_import: &ModPath, edit: &mut TextEditBuilder, ) { + let target = path_to_import.to_string().split("::").map(SmolStr::new).collect::>(); let container = position.ancestors().find_map(|n| { if let Some(module) = ast::Module::cast(n.clone()) { return module.item_list().map(|it| it.syntax().clone()); @@ -32,8 +33,8 @@ pub fn auto_import_text_edit( }); if let Some(container) = container { - let action = best_action_for_target(container, anchor.clone(), target); - make_assist(&action, target, edit); + let action = best_action_for_target(container, anchor.clone(), &target); + make_assist(&action, &target, edit); } } diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 0d15adb87..9e874aebb 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs @@ -1,7 +1,6 @@ -use hir::db::HirDatabase; +use hir::{db::HirDatabase, ModPath}; use ra_syntax::{ ast::{self, AstNode}, - SmolStr, SyntaxKind::USE_ITEM, SyntaxNode, }; @@ -58,7 +57,6 @@ pub(crate) fn auto_import( .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def)) .filter(|use_path| !use_path.segments.is_empty()) .take(20) - .map(|import| import.to_string()) .collect::>(); if proposed_imports.is_empty() { return None; @@ -76,11 +74,10 @@ pub(crate) fn auto_import( ) } -fn import_to_action(import: String, position: &SyntaxNode, anchor: &SyntaxNode) -> ActionBuilder { +fn import_to_action(import: ModPath, position: &SyntaxNode, anchor: &SyntaxNode) -> ActionBuilder { let mut action_builder = ActionBuilder::default(); action_builder.label(format!("Import `{}`", &import)); - let import_segments = import.split("::").map(SmolStr::new).collect::>(); - auto_import_text_edit(position, anchor, &import_segments, action_builder.text_edit_builder()); + auto_import_text_edit(position, anchor, &import, action_builder.text_edit_builder()); action_builder } -- cgit v1.2.3