Skip to content

Commit 662ed41

Browse files
bors[bot]Veykril
andauthored
Merge #6073
6073: Dont unnecessarily unnest imports r=matklad a=Veykril Fixes #6071 This has the side effect that paths that refer to items inside of the current module get prefixed with `self`. Changing this behavior is unfortunately not straightforward should it be unwanted, though I don't see a problem with this as prefixing imports like this with `self` is what I do personally anyways 😅. You can see what I mean with this in one of the tests which had to be changed in `crates/ssr/src/tests.rs`. There is one test that i still have to look at though, ~~which I by accident pushed with `#[ignore]` on it~~, which is `different_crate_renamed`, for some reason this now doesn't use the crate alias. This also makes me believe that aliases in general will break with this. So maybe this is not as straight forwards as I'd hoped for, but I don't really know how aliases work here. Edit: The failing test should work now Co-authored-by: Lukas Wirth <[email protected]>
2 parents dc09f15 + 747f6f6 commit 662ed41

File tree

5 files changed

+203
-55
lines changed

5 files changed

+203
-55
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/assists/src/utils/insert_use.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -809,16 +809,6 @@ use std::io;",
809809
// FIXME: have it emit `use {self, *}`?
810810
}
811811

812-
#[test]
813-
#[ignore] // FIXME: Support this
814-
fn merge_partial_path() {
815-
check_full(
816-
"ast::Foo",
817-
r"use syntax::{ast, algo};",
818-
r"use syntax::{ast::{self, Foo}, algo};",
819-
)
820-
}
821-
822812
#[test]
823813
fn merge_glob_nested() {
824814
check_full(

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)