Skip to content

Commit 79614c4

Browse files
committed
Enable auto_import on ident patterns
1 parent 637dbb2 commit 79614c4

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

crates/ide_assists/src/handlers/auto_import.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ pub(super) fn find_importable_node(ctx: &AssistContext) -> Option<(ImportAssets,
123123
{
124124
ImportAssets::for_method_call(&method_under_caret, &ctx.sema)
125125
.zip(Some(method_under_caret.syntax().clone()))
126+
} else if let Some(pat) = ctx
127+
.find_node_at_offset_with_descend::<ast::IdentPat>()
128+
.filter(ast::IdentPat::is_simple_ident)
129+
{
130+
ImportAssets::for_ident_pat(&pat, &ctx.sema).zip(Some(pat.syntax().clone()))
126131
} else {
127132
None
128133
}
@@ -995,6 +1000,31 @@ mod foo {}
9951000
const _: () = {
9961001
Foo
9971002
};
1003+
"#,
1004+
);
1005+
}
1006+
1007+
#[test]
1008+
fn works_on_ident_patterns() {
1009+
check_assist(
1010+
auto_import,
1011+
r#"
1012+
mod foo {
1013+
pub struct Foo {}
1014+
}
1015+
fn foo() {
1016+
let Foo$0;
1017+
}
1018+
"#,
1019+
r#"
1020+
use foo::Foo;
1021+
1022+
mod foo {
1023+
pub struct Foo {}
1024+
}
1025+
fn foo() {
1026+
let Foo;
1027+
}
9981028
"#,
9991029
);
10001030
}

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ use hir::{
55
};
66
use itertools::Itertools;
77
use rustc_hash::FxHashSet;
8-
use syntax::{ast, utils::path_to_string_stripping_turbo_fish, AstNode, SyntaxNode};
8+
use syntax::{
9+
ast::{self, NameOwner},
10+
utils::path_to_string_stripping_turbo_fish,
11+
AstNode, SyntaxNode,
12+
};
913

1014
use crate::{
1115
items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT},
@@ -115,6 +119,19 @@ impl ImportAssets {
115119
})
116120
}
117121

122+
pub fn for_ident_pat(pat: &ast::IdentPat, sema: &Semantics<RootDatabase>) -> Option<Self> {
123+
let name = pat.name()?;
124+
let candidate_node = pat.syntax().clone();
125+
if !pat.is_simple_ident() {
126+
return None;
127+
}
128+
Some(Self {
129+
import_candidate: ImportCandidate::for_name(sema, &name)?,
130+
module_with_candidate: sema.scope(&candidate_node).module()?,
131+
candidate_node,
132+
})
133+
}
134+
118135
pub fn for_fuzzy_path(
119136
module_with_candidate: Module,
120137
qualifier: Option<ast::Path>,
@@ -543,6 +560,20 @@ impl ImportCandidate {
543560
)
544561
}
545562

563+
fn for_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<Self> {
564+
if sema
565+
.scope(name.syntax())
566+
.speculative_resolve(&ast::make::ext::ident_path(&name.text()))
567+
.is_some()
568+
{
569+
return None;
570+
}
571+
Some(ImportCandidate::Path(PathImportCandidate {
572+
qualifier: None,
573+
name: NameToImport::Exact(name.to_string()),
574+
}))
575+
}
576+
546577
fn for_fuzzy_path(
547578
qualifier: Option<ast::Path>,
548579
fuzzy_name: String,

0 commit comments

Comments
 (0)