From 932d4183a14a3598e054c9798347a25ab4ac1f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 10 Jul 2021 18:38:41 +0300 Subject: [PATCH 1/4] Bump deps --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c6c0810aa8d..ac425d32c7c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" From 79614c486b109484d7717c92cf68998f192016c2 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 10 Jul 2021 17:57:33 +0200 Subject: [PATCH 2/4] Enable `auto_import` on ident patterns --- .../ide_assists/src/handlers/auto_import.rs | 30 +++++++++++++++++ crates/ide_db/src/helpers/import_assets.rs | 33 ++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index b220a4ba418..3ecb3d38ea7 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs @@ -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::() + .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; +} "#, ); } diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index 9634d872e49..58fea3b1de5 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs @@ -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) -> Option { + 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, @@ -543,6 +560,20 @@ impl ImportCandidate { ) } + fn for_name(sema: &Semantics, name: &ast::Name) -> Option { + 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, fuzzy_name: String, From 861f1e2a86a1438cee9600938eea7070dbb74f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 10 Jul 2021 18:51:35 +0300 Subject: [PATCH 3/4] Bump rustc_lexer a little --- Cargo.lock | 4 ++-- crates/syntax/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ac425d32c7c..2b1c9771d57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/crates/syntax/Cargo.toml b/crates/syntax/Cargo.toml index e22fdb0312a..e0fb33933c6 100644 --- a/crates/syntax/Cargo.toml +++ b/crates/syntax/Cargo.toml @@ -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" From df729eda69e18dc54783d85188b59775ad90c10b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauren=C8=9Biu=20Nicola?= Date: Sat, 10 Jul 2021 18:56:56 +0300 Subject: [PATCH 4/4] Bump chalk --- Cargo.lock | 16 ++++++++-------- crates/hir_ty/Cargo.toml | 11 +++++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2b1c9771d57..a3fa70bdb3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/crates/hir_ty/Cargo.toml b/crates/hir_ty/Cargo.toml index 30f1f99b9c4..f8280e0c5c4 100644 --- a/crates/hir_ty/Cargo.toml +++ b/crates/hir_ty/Cargo.toml @@ -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"] }