From e1d6981f90ba7852e949141dad0add855b57184a Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 25 Sep 2020 15:19:22 +0200 Subject: Don't unnecessarily unnest imports for import insertion --- .../src/handlers/add_missing_impl_members.rs | 35 ++++++++++++++++++++++ crates/assists/src/handlers/auto_import.rs | 33 ++++++++++++++++++-- 2 files changed, 66 insertions(+), 2 deletions(-) (limited to 'crates/assists/src/handlers') diff --git a/crates/assists/src/handlers/add_missing_impl_members.rs b/crates/assists/src/handlers/add_missing_impl_members.rs index 8df1d786b..1ac5fefd6 100644 --- a/crates/assists/src/handlers/add_missing_impl_members.rs +++ b/crates/assists/src/handlers/add_missing_impl_members.rs @@ -414,6 +414,41 @@ impl foo::Foo for S { ); } + #[test] + fn test_qualify_path_2() { + check_assist( + add_missing_impl_members, + r#" +mod foo { + pub mod bar { + pub struct Bar; + pub trait Foo { fn foo(&self, bar: Bar); } + } +} + +use foo::bar; + +struct S; +impl bar::Foo for S { <|> }"#, + r#" +mod foo { + pub mod bar { + pub struct Bar; + pub trait Foo { fn foo(&self, bar: Bar); } + } +} + +use foo::bar; + +struct S; +impl bar::Foo for S { + fn foo(&self, bar: bar::Bar) { + ${0:todo!()} + } +}"#, + ); + } + #[test] fn test_qualify_path_generic() { check_assist( diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index b5eb2c722..ee7277c04 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs @@ -196,10 +196,10 @@ impl AutoImportAssets { }) .filter_map(|candidate| match candidate { Either::Left(module_def) => { - self.module_with_name_to_import.find_use_path(db, module_def) + self.module_with_name_to_import.find_use_path_prefixed(db, module_def) } Either::Right(macro_def) => { - self.module_with_name_to_import.find_use_path(db, macro_def) + self.module_with_name_to_import.find_use_path_prefixed(db, macro_def) } }) .filter(|use_path| !use_path.segments.is_empty()) @@ -290,6 +290,35 @@ mod tests { use super::*; use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; + #[test] + fn applicable_when_found_an_import_partial() { + check_assist( + auto_import, + r" + mod std { + pub mod fmt { + pub struct Formatter; + } + } + + use std::fmt; + + <|>Formatter + ", + r" + mod std { + pub mod fmt { + pub struct Formatter; + } + } + + use std::fmt::{self, Formatter}; + + Formatter + ", + ); + } + #[test] fn applicable_when_found_an_import() { check_assist( -- cgit v1.2.3