From 02b844e9fbc351a53d56398e269e5861ed1ad5c1 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 13 Oct 2020 18:52:56 +0200 Subject: Adhere to style guidelines in import_assets --- crates/assists/src/utils/import_assets.rs | 66 ++++++++++++++----------------- 1 file changed, 29 insertions(+), 37 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 ce5986db7..b816edc82 100644 --- a/crates/assists/src/utils/import_assets.rs +++ b/crates/assists/src/utils/import_assets.rs @@ -9,6 +9,23 @@ use syntax::{ast, AstNode, SyntaxNode}; use crate::assist_config::InsertUseConfig; +#[derive(Debug)] +pub(crate) enum ImportCandidate { + /// Simple name like 'HashMap' + UnqualifiedName(String), + /// First part of the qualified name. + /// For 'std::collections::HashMap', that will be 'std'. + QualifierStart(String), + /// A trait associated function (with no self parameter) or associated constant. + /// For 'test_mod::TestEnum::test_function', `Type` is the `test_mod::TestEnum` expression type + /// and `String` is the `test_function` + TraitAssocItem(hir::Type, String), + /// A trait method with self parameter. + /// For 'test_enum.test_method()', `Type` is the `test_enum` expression type + /// and `String` is the `test_method` + TraitMethod(hir::Type, String), +} + #[derive(Debug)] pub(crate) struct ImportAssets { import_candidate: ImportCandidate, @@ -17,23 +34,7 @@ pub(crate) struct ImportAssets { } impl ImportAssets { - pub(crate) fn new(ctx: &crate::assist_context::AssistContext) -> Option { - if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::() { - Self::for_regular_path(path_under_caret, &ctx.sema) - } else { - Self::for_method_call(ctx.find_node_at_offset_with_descend()?, &ctx.sema) - } - } - - pub(crate) fn syntax_under_caret(&self) -> &SyntaxNode { - &self.syntax_under_caret - } - - pub(crate) fn import_candidate(&self) -> &ImportCandidate { - &self.import_candidate - } - - fn for_method_call( + pub(crate) fn for_method_call( method_call: ast::MethodCallExpr, sema: &Semantics, ) -> Option { @@ -46,7 +47,7 @@ impl ImportAssets { }) } - fn for_regular_path( + pub(crate) fn for_regular_path( path_under_caret: ast::Path, sema: &Semantics, ) -> Option { @@ -63,6 +64,14 @@ impl ImportAssets { }) } + pub(crate) fn syntax_under_caret(&self) -> &SyntaxNode { + &self.syntax_under_caret + } + + pub(crate) fn import_candidate(&self) -> &ImportCandidate { + &self.import_candidate + } + fn get_search_query(&self) -> &str { match &self.import_candidate { ImportCandidate::UnqualifiedName(name) => name, @@ -182,25 +191,8 @@ impl ImportAssets { } } -#[derive(Debug)] -pub(crate) enum ImportCandidate { - /// Simple name like 'HashMap' - UnqualifiedName(String), - /// First part of the qualified name. - /// For 'std::collections::HashMap', that will be 'std'. - QualifierStart(String), - /// A trait associated function (with no self parameter) or associated constant. - /// For 'test_mod::TestEnum::test_function', `Type` is the `test_mod::TestEnum` expression type - /// and `String` is the `test_function` - TraitAssocItem(hir::Type, String), - /// A trait method with self parameter. - /// For 'test_enum.test_method()', `Type` is the `test_enum` expression type - /// and `String` is the `test_method` - TraitMethod(hir::Type, String), -} - impl ImportCandidate { - pub(crate) fn for_method_call( + fn for_method_call( sema: &Semantics, method_call: &ast::MethodCallExpr, ) -> Option { @@ -213,7 +205,7 @@ impl ImportCandidate { )) } - pub(crate) fn for_regular_path( + fn for_regular_path( sema: &Semantics, path_under_caret: &ast::Path, ) -> Option { -- cgit v1.2.3