Skip to content

Commit e1d6981

Browse files
committed
Don't unnecessarily unnest imports for import insertion
1 parent 9d3483a commit e1d6981

File tree

4 files changed

+203
-45
lines changed

4 files changed

+203
-45
lines changed

crates/assists/src/handlers/add_missing_impl_members.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,41 @@ impl foo::Foo for S {
414414
);
415415
}
416416

417+
#[test]
418+
fn test_qualify_path_2() {
419+
check_assist(
420+
add_missing_impl_members,
421+
r#"
422+
mod foo {
423+
pub mod bar {
424+
pub struct Bar;
425+
pub trait Foo { fn foo(&self, bar: Bar); }
426+
}
427+
}
428+
429+
use foo::bar;
430+
431+
struct S;
432+
impl bar::Foo for S { <|> }"#,
433+
r#"
434+
mod foo {
435+
pub mod bar {
436+
pub struct Bar;
437+
pub trait Foo { fn foo(&self, bar: Bar); }
438+
}
439+
}
440+
441+
use foo::bar;
442+
443+
struct S;
444+
impl bar::Foo for S {
445+
fn foo(&self, bar: bar::Bar) {
446+
${0:todo!()}
447+
}
448+
}"#,
449+
);
450+
}
451+
417452
#[test]
418453
fn test_qualify_path_generic() {
419454
check_assist(

crates/assists/src/handlers/auto_import.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ impl AutoImportAssets {
196196
})
197197
.filter_map(|candidate| match candidate {
198198
Either::Left(module_def) => {
199-
self.module_with_name_to_import.find_use_path(db, module_def)
199+
self.module_with_name_to_import.find_use_path_prefixed(db, module_def)
200200
}
201201
Either::Right(macro_def) => {
202-
self.module_with_name_to_import.find_use_path(db, macro_def)
202+
self.module_with_name_to_import.find_use_path_prefixed(db, macro_def)
203203
}
204204
})
205205
.filter(|use_path| !use_path.segments.is_empty())
@@ -290,6 +290,35 @@ mod tests {
290290
use super::*;
291291
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
292292

293+
#[test]
294+
fn applicable_when_found_an_import_partial() {
295+
check_assist(
296+
auto_import,
297+
r"
298+
mod std {
299+
pub mod fmt {
300+
pub struct Formatter;
301+
}
302+
}
303+
304+
use std::fmt;
305+
306+
<|>Formatter
307+
",
308+
r"
309+
mod std {
310+
pub mod fmt {
311+
pub struct Formatter;
312+
}
313+
}
314+
315+
use std::fmt::{self, Formatter};
316+
317+
Formatter
318+
",
319+
);
320+
}
321+
293322
#[test]
294323
fn applicable_when_found_an_import() {
295324
check_assist(

crates/hir/src/code_model.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ impl Module {
383383
pub fn find_use_path(self, db: &dyn DefDatabase, item: impl Into<ItemInNs>) -> Option<ModPath> {
384384
hir_def::find_path::find_path(db, item.into(), self.into())
385385
}
386+
387+
/// Finds a path that can be used to refer to the given item from within
388+
/// this module, if possible. This is used for returning import paths for use-statements.
389+
pub fn find_use_path_prefixed(
390+
self,
391+
db: &dyn DefDatabase,
392+
item: impl Into<ItemInNs>,
393+
) -> Option<ModPath> {
394+
hir_def::find_path::find_path_prefixed(db, item.into(), self.into())
395+
}
386396
}
387397

388398
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

0 commit comments

Comments
 (0)