Skip to content

Commit 02b844e

Browse files
committed
Adhere to style guidelines in import_assets
1 parent 256104d commit 02b844e

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

crates/assists/src/handlers/auto_import.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use syntax::ast;
2+
13
use crate::{
24
utils::import_assets::{ImportAssets, ImportCandidate},
35
utils::{insert_use, mod_path_to_ast, ImportScope},
@@ -24,16 +26,24 @@ use crate::{
2426
// # pub mod std { pub mod collections { pub struct HashMap { } } }
2527
// ```
2628
pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
27-
let auto_import_assets = ImportAssets::new(&ctx)?;
28-
let proposed_imports = auto_import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use);
29+
let import_assets =
30+
if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() {
31+
ImportAssets::for_regular_path(path_under_caret, &ctx.sema)
32+
} else if let Some(method_under_caret) =
33+
ctx.find_node_at_offset_with_descend::<ast::MethodCallExpr>()
34+
{
35+
ImportAssets::for_method_call(method_under_caret, &ctx.sema)
36+
} else {
37+
None
38+
}?;
39+
let proposed_imports = import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use);
2940
if proposed_imports.is_empty() {
3041
return None;
3142
}
3243

33-
let range = ctx.sema.original_range(auto_import_assets.syntax_under_caret()).range;
34-
let group = import_group_message(auto_import_assets.import_candidate());
35-
let scope =
36-
ImportScope::find_insert_use_container(auto_import_assets.syntax_under_caret(), ctx)?;
44+
let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range;
45+
let group = import_group_message(import_assets.import_candidate());
46+
let scope = ImportScope::find_insert_use_container(import_assets.syntax_under_caret(), ctx)?;
3747
let syntax = scope.as_syntax_node();
3848
for import in proposed_imports {
3949
acc.add_group(

crates/assists/src/utils/import_assets.rs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,23 @@ use syntax::{ast, AstNode, SyntaxNode};
99

1010
use crate::assist_config::InsertUseConfig;
1111

12+
#[derive(Debug)]
13+
pub(crate) enum ImportCandidate {
14+
/// Simple name like 'HashMap'
15+
UnqualifiedName(String),
16+
/// First part of the qualified name.
17+
/// For 'std::collections::HashMap', that will be 'std'.
18+
QualifierStart(String),
19+
/// A trait associated function (with no self parameter) or associated constant.
20+
/// For 'test_mod::TestEnum::test_function', `Type` is the `test_mod::TestEnum` expression type
21+
/// and `String` is the `test_function`
22+
TraitAssocItem(hir::Type, String),
23+
/// A trait method with self parameter.
24+
/// For 'test_enum.test_method()', `Type` is the `test_enum` expression type
25+
/// and `String` is the `test_method`
26+
TraitMethod(hir::Type, String),
27+
}
28+
1229
#[derive(Debug)]
1330
pub(crate) struct ImportAssets {
1431
import_candidate: ImportCandidate,
@@ -17,23 +34,7 @@ pub(crate) struct ImportAssets {
1734
}
1835

1936
impl ImportAssets {
20-
pub(crate) fn new(ctx: &crate::assist_context::AssistContext) -> Option<Self> {
21-
if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() {
22-
Self::for_regular_path(path_under_caret, &ctx.sema)
23-
} else {
24-
Self::for_method_call(ctx.find_node_at_offset_with_descend()?, &ctx.sema)
25-
}
26-
}
27-
28-
pub(crate) fn syntax_under_caret(&self) -> &SyntaxNode {
29-
&self.syntax_under_caret
30-
}
31-
32-
pub(crate) fn import_candidate(&self) -> &ImportCandidate {
33-
&self.import_candidate
34-
}
35-
36-
fn for_method_call(
37+
pub(crate) fn for_method_call(
3738
method_call: ast::MethodCallExpr,
3839
sema: &Semantics<RootDatabase>,
3940
) -> Option<Self> {
@@ -46,7 +47,7 @@ impl ImportAssets {
4647
})
4748
}
4849

49-
fn for_regular_path(
50+
pub(crate) fn for_regular_path(
5051
path_under_caret: ast::Path,
5152
sema: &Semantics<RootDatabase>,
5253
) -> Option<Self> {
@@ -63,6 +64,14 @@ impl ImportAssets {
6364
})
6465
}
6566

67+
pub(crate) fn syntax_under_caret(&self) -> &SyntaxNode {
68+
&self.syntax_under_caret
69+
}
70+
71+
pub(crate) fn import_candidate(&self) -> &ImportCandidate {
72+
&self.import_candidate
73+
}
74+
6675
fn get_search_query(&self) -> &str {
6776
match &self.import_candidate {
6877
ImportCandidate::UnqualifiedName(name) => name,
@@ -182,25 +191,8 @@ impl ImportAssets {
182191
}
183192
}
184193

185-
#[derive(Debug)]
186-
pub(crate) enum ImportCandidate {
187-
/// Simple name like 'HashMap'
188-
UnqualifiedName(String),
189-
/// First part of the qualified name.
190-
/// For 'std::collections::HashMap', that will be 'std'.
191-
QualifierStart(String),
192-
/// A trait associated function (with no self parameter) or associated constant.
193-
/// For 'test_mod::TestEnum::test_function', `Type` is the `test_mod::TestEnum` expression type
194-
/// and `String` is the `test_function`
195-
TraitAssocItem(hir::Type, String),
196-
/// A trait method with self parameter.
197-
/// For 'test_enum.test_method()', `Type` is the `test_enum` expression type
198-
/// and `String` is the `test_method`
199-
TraitMethod(hir::Type, String),
200-
}
201-
202194
impl ImportCandidate {
203-
pub(crate) fn for_method_call(
195+
fn for_method_call(
204196
sema: &Semantics<RootDatabase>,
205197
method_call: &ast::MethodCallExpr,
206198
) -> Option<Self> {
@@ -213,7 +205,7 @@ impl ImportCandidate {
213205
))
214206
}
215207

216-
pub(crate) fn for_regular_path(
208+
fn for_regular_path(
217209
sema: &Semantics<RootDatabase>,
218210
path_under_caret: &ast::Path,
219211
) -> Option<Self> {

0 commit comments

Comments
 (0)