From 6742f38e49d001359a7a9911becc0fcae4c67910 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 16 Jan 2021 19:33:36 +0200 Subject: Share import_assets and related entities --- crates/assists/src/handlers/auto_import.rs | 9 +++---- crates/assists/src/handlers/qualify_path.rs | 40 +++++++++++++++++------------ 2 files changed, 27 insertions(+), 22 deletions(-) (limited to 'crates/assists/src/handlers') diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index 55620f0f3..4e2a4fcd9 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs @@ -1,13 +1,11 @@ use ide_db::helpers::{ + import_assets::{ImportAssets, ImportCandidate}, insert_use::{insert_use, ImportScope}, mod_path_to_ast, }; use syntax::ast; -use crate::{ - utils::import_assets::{ImportAssets, ImportCandidate}, - AssistContext, AssistId, AssistKind, Assists, GroupLabel, -}; +use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; // Feature: Auto Import // @@ -121,8 +119,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel { let name = match import_candidate { - ImportCandidate::UnqualifiedName(candidate) - | ImportCandidate::QualifierStart(candidate) => format!("Import {}", &candidate.name), + ImportCandidate::Path(candidate) => format!("Import {}", &candidate.name), ImportCandidate::TraitAssocItem(candidate) => { format!("Import a trait for item {}", &candidate.name) } diff --git a/crates/assists/src/handlers/qualify_path.rs b/crates/assists/src/handlers/qualify_path.rs index f7fbf37f4..a7d9fd4dc 100644 --- a/crates/assists/src/handlers/qualify_path.rs +++ b/crates/assists/src/handlers/qualify_path.rs @@ -1,7 +1,10 @@ use std::iter; use hir::AsName; -use ide_db::helpers::mod_path_to_ast; +use ide_db::helpers::{ + import_assets::{ImportAssets, ImportCandidate}, + mod_path_to_ast, +}; use ide_db::RootDatabase; use syntax::{ ast, @@ -12,7 +15,6 @@ use test_utils::mark; use crate::{ assist_context::{AssistContext, Assists}, - utils::import_assets::{ImportAssets, ImportCandidate}, AssistId, AssistKind, GroupLabel, }; @@ -53,17 +55,18 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range; let qualify_candidate = match candidate { - ImportCandidate::QualifierStart(_) => { - mark::hit!(qualify_path_qualifier_start); - let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; - let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?); - QualifyCandidate::QualifierStart(segment, prev_segment.generic_arg_list()) - } - ImportCandidate::UnqualifiedName(_) => { - mark::hit!(qualify_path_unqualified_name); - let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; - let generics = path.segment()?.generic_arg_list(); - QualifyCandidate::UnqualifiedName(generics) + ImportCandidate::Path(candidate) => { + if candidate.qualifier.is_some() { + mark::hit!(qualify_path_qualifier_start); + let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; + let (prev_segment, segment) = (path.qualifier()?.segment()?, path.segment()?); + QualifyCandidate::QualifierStart(segment, prev_segment.generic_arg_list()) + } else { + mark::hit!(qualify_path_unqualified_name); + let path = ast::Path::cast(import_assets.syntax_under_caret().clone())?; + let generics = path.segment()?.generic_arg_list(); + QualifyCandidate::UnqualifiedName(generics) + } } ImportCandidate::TraitAssocItem(_) => { mark::hit!(qualify_path_trait_assoc_item); @@ -186,7 +189,7 @@ fn item_as_trait(item: hir::ItemInNs) -> Option { fn group_label(candidate: &ImportCandidate) -> GroupLabel { let name = match candidate { - ImportCandidate::UnqualifiedName(it) | ImportCandidate::QualifierStart(it) => &it.name, + ImportCandidate::Path(it) => &it.name, ImportCandidate::TraitAssocItem(it) | ImportCandidate::TraitMethod(it) => &it.name, }; GroupLabel(format!("Qualify {}", name)) @@ -194,8 +197,13 @@ fn group_label(candidate: &ImportCandidate) -> GroupLabel { fn label(candidate: &ImportCandidate, import: &hir::ModPath) -> String { match candidate { - ImportCandidate::UnqualifiedName(_) => format!("Qualify as `{}`", &import), - ImportCandidate::QualifierStart(_) => format!("Qualify with `{}`", &import), + ImportCandidate::Path(candidate) => { + if candidate.qualifier.is_some() { + format!("Qualify with `{}`", &import) + } else { + format!("Qualify as `{}`", &import) + } + } ImportCandidate::TraitAssocItem(_) => format!("Qualify `{}`", &import), ImportCandidate::TraitMethod(_) => format!("Qualify with cast as `{}`", &import), } -- cgit v1.2.3