From 344cb5e76a31b8ef7ae80ce0ef39c54248ad8df7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 18 Jun 2021 23:53:41 +0200 Subject: Don't insert imports outside of cfg attributed items --- crates/ide_assists/src/handlers/auto_import.rs | 59 ++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'crates/ide_assists/src/handlers/auto_import.rs') diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index 6c7348178..8df8b060d 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs @@ -103,6 +103,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> let scope = match scope.clone() { ImportScope::File(it) => ImportScope::File(builder.make_mut(it)), ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)), + ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)), }; insert_use(&scope, mod_path_to_ast(&import.import_path), &ctx.config.insert_use); }, @@ -991,6 +992,64 @@ mod foo {} const _: () = { Foo }; +"#, + ); + } + + #[test] + fn respects_cfg_attr() { + check_assist( + auto_import, + r#" +mod bar { + pub struct Bar; +} + +#[cfg(test)] +fn foo() { + Bar$0 +} +"#, + r#" +mod bar { + pub struct Bar; +} + +#[cfg(test)] +fn foo() { +use bar::Bar; + + Bar +} +"#, + ); + } + + #[test] + fn respects_cfg_attr2() { + check_assist( + auto_import, + r#" +mod bar { + pub struct Bar; +} + +#[cfg(test)] +const FOO: Bar = { + Bar$0 +} +"#, + r#" +mod bar { + pub struct Bar; +} + +#[cfg(test)] +const FOO: Bar = { +use bar::Bar; + + Bar +} "#, ); } -- cgit v1.2.3