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 <lukastw97@gmail.com>
Co-authored-by: Laurențiu Nicola <lnicola@dend.ro>
This commit is contained in:
bors[bot] 2021-07-10 15:59:26 +00:00 committed by GitHub
commit 4337f5e456
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 18 deletions

24
Cargo.lock generated
View file

@ -37,9 +37,9 @@ dependencies = [
[[package]]
name = "anyhow"
version = "1.0.41"
version = "1.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15af2628f6890fe2609a3b91bef4c83450512802e59489f9c1cb1fa5df064a61"
checksum = "595d3cfa7a60d4555cb5067b99f07142a08ea778de5cf993f7b75c7d8fabc486"
[[package]]
name = "anymap"
@ -162,9 +162,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chalk-derive"
version = "0.68.0"
version = "0.69.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea1552e7666a857f5417e6051ce705ea6856ab2cda39be7605e5b626fa47416b"
checksum = "ef492c0897139b578c495149fda9153ef074f91d07361603d70531b68e2a5c02"
dependencies = [
"proc-macro2",
"quote",
@ -174,9 +174,9 @@ dependencies = [
[[package]]
name = "chalk-ir"
version = "0.68.0"
version = "0.69.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19d7d5f1448dbac493541e97221f7f4c32326c4c76c6ecf543daf72a1dd93e66"
checksum = "95066fe8bd77ad2dd53981b87a0b72385695e5caab56d69151873cb5df415b42"
dependencies = [
"bitflags",
"chalk-derive",
@ -185,9 +185,9 @@ dependencies = [
[[package]]
name = "chalk-recursive"
version = "0.68.0"
version = "0.69.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0df406d2927321021b48acd193459dd33c913732155c93442d03f5ae8275385"
checksum = "ede3f73bba6510660b5c4fdab5a60a81396ecb0f38f9165fcdf80d62ba017800"
dependencies = [
"chalk-derive",
"chalk-ir",
@ -198,9 +198,9 @@ dependencies = [
[[package]]
name = "chalk-solve"
version = "0.68.0"
version = "0.69.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0cbfcd5daa5ab8b1c9e5e10e83b0ac26271480f6ae5b5f35e5b19e1f6a0e6e37"
checksum = "c9539e84bb8ac8960762794ae88e108d6c3e4b2f60d4241d461ddcf2ba8645e1"
dependencies = [
"chalk-derive",
"chalk-ir",
@ -1337,9 +1337,9 @@ dependencies = [
[[package]]
name = "rustc-ap-rustc_lexer"
version = "721.0.0"
version = "725.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ba1f60e2942dc7dc5ea64edeaae01cfba2303871b14936e1af0f54d5420b3d1"
checksum = "f950742ef8a203aa7661aad3ab880438ddeb7f95d4b837c30d65db1a2c5df68e"
dependencies = [
"unicode-xid",
]

View file

@ -17,9 +17,9 @@ ena = "0.14.0"
log = "0.4.8"
rustc-hash = "1.1.0"
scoped-tls = "1"
chalk-solve = { version = "0.68", default-features = false }
chalk-ir = "0.68"
chalk-recursive = { version = "0.68", default-features = false }
chalk-solve = { version = "0.69", default-features = false }
chalk-ir = "0.69"
chalk-recursive = { version = "0.69", default-features = false }
la-arena = { version = "0.2.0", path = "../../lib/arena" }
once_cell = { version = "1.5.0" }
@ -34,6 +34,9 @@ syntax = { path = "../syntax", version = "0.0.0" }
test_utils = { path = "../test_utils" }
expect-test = "1.1"
tracing = "0.1"
tracing-subscriber = { version = "0.2", default-features = false, features = ["env-filter", "registry"] }
tracing-subscriber = { version = "0.2", default-features = false, features = [
"env-filter",
"registry",
] }
tracing-tree = { version = "0.1.4" }
once_cell = { version = "1.5.0", features = ["unstable"] }

View file

@ -123,6 +123,11 @@ pub(super) fn find_importable_node(ctx: &AssistContext) -> Option<(ImportAssets,
{
ImportAssets::for_method_call(&method_under_caret, &ctx.sema)
.zip(Some(method_under_caret.syntax().clone()))
} else if let Some(pat) = ctx
.find_node_at_offset_with_descend::<ast::IdentPat>()
.filter(ast::IdentPat::is_simple_ident)
{
ImportAssets::for_ident_pat(&pat, &ctx.sema).zip(Some(pat.syntax().clone()))
} else {
None
}
@ -995,6 +1000,31 @@ mod foo {}
const _: () = {
Foo
};
"#,
);
}
#[test]
fn works_on_ident_patterns() {
check_assist(
auto_import,
r#"
mod foo {
pub struct Foo {}
}
fn foo() {
let Foo$0;
}
"#,
r#"
use foo::Foo;
mod foo {
pub struct Foo {}
}
fn foo() {
let Foo;
}
"#,
);
}

View file

@ -5,7 +5,11 @@ use hir::{
};
use itertools::Itertools;
use rustc_hash::FxHashSet;
use syntax::{ast, utils::path_to_string_stripping_turbo_fish, AstNode, SyntaxNode};
use syntax::{
ast::{self, NameOwner},
utils::path_to_string_stripping_turbo_fish,
AstNode, SyntaxNode,
};
use crate::{
items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT},
@ -115,6 +119,19 @@ impl ImportAssets {
})
}
pub fn for_ident_pat(pat: &ast::IdentPat, sema: &Semantics<RootDatabase>) -> Option<Self> {
let name = pat.name()?;
let candidate_node = pat.syntax().clone();
if !pat.is_simple_ident() {
return None;
}
Some(Self {
import_candidate: ImportCandidate::for_name(sema, &name)?,
module_with_candidate: sema.scope(&candidate_node).module()?,
candidate_node,
})
}
pub fn for_fuzzy_path(
module_with_candidate: Module,
qualifier: Option<ast::Path>,
@ -543,6 +560,20 @@ impl ImportCandidate {
)
}
fn for_name(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Option<Self> {
if sema
.scope(name.syntax())
.speculative_resolve(&ast::make::ext::ident_path(&name.text()))
.is_some()
{
return None;
}
Some(ImportCandidate::Path(PathImportCandidate {
qualifier: None,
name: NameToImport::Exact(name.to_string()),
}))
}
fn for_fuzzy_path(
qualifier: Option<ast::Path>,
fuzzy_name: String,

View file

@ -13,7 +13,7 @@ doctest = false
cov-mark = "2.0.0-pre.1"
itertools = "0.10.0"
rowan = "=0.13.0-pre.7"
rustc_lexer = { version = "721.0.0", package = "rustc-ap-rustc_lexer" }
rustc_lexer = { version = "725.0.0", package = "rustc-ap-rustc_lexer" }
rustc-hash = "1.1.0"
arrayvec = "0.7"
once_cell = "1.3.1"