Skip to content

Commit 4337f5e

Browse files
bors[bot]Veykrillnicola
authored
Merge #9555 #9556
9555: feat: Enable `auto_import` on ident patterns r=Veykril a=Veykril Helpful for when you want to import a type in a pattern right before destructuring it. 9556: Bump deps r=lnicola a=lnicola bors r+ Co-authored-by: Lukas Wirth <[email protected]> Co-authored-by: Laurențiu Nicola <[email protected]>
3 parents 6f3a3bd + 79614c4 + df729ed commit 4337f5e

File tree

5 files changed

+82
-18
lines changed

5 files changed

+82
-18
lines changed

Cargo.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir_ty/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ ena = "0.14.0"
1717
log = "0.4.8"
1818
rustc-hash = "1.1.0"
1919
scoped-tls = "1"
20-
chalk-solve = { version = "0.68", default-features = false }
21-
chalk-ir = "0.68"
22-
chalk-recursive = { version = "0.68", default-features = false }
20+
chalk-solve = { version = "0.69", default-features = false }
21+
chalk-ir = "0.69"
22+
chalk-recursive = { version = "0.69", default-features = false }
2323
la-arena = { version = "0.2.0", path = "../../lib/arena" }
2424
once_cell = { version = "1.5.0" }
2525

@@ -34,6 +34,9 @@ syntax = { path = "../syntax", version = "0.0.0" }
3434
test_utils = { path = "../test_utils" }
3535
expect-test = "1.1"
3636
tracing = "0.1"
37-
tracing-subscriber = { version = "0.2", default-features = false, features = ["env-filter", "registry"] }
37+
tracing-subscriber = { version = "0.2", default-features = false, features = [
38+
"env-filter",
39+
"registry",
40+
] }
3841
tracing-tree = { version = "0.1.4" }
3942
once_cell = { version = "1.5.0", features = ["unstable"] }

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,

crates/syntax/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ doctest = false
1313
cov-mark = "2.0.0-pre.1"
1414
itertools = "0.10.0"
1515
rowan = "=0.13.0-pre.7"
16-
rustc_lexer = { version = "721.0.0", package = "rustc-ap-rustc_lexer" }
16+
rustc_lexer = { version = "725.0.0", package = "rustc-ap-rustc_lexer" }
1717
rustc-hash = "1.1.0"
1818
arrayvec = "0.7"
1919
once_cell = "1.3.1"

0 commit comments

Comments
 (0)