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 ++++++++++++++++++++++ .../handlers/replace_qualified_name_with_use.rs | 13 ++--- 2 files changed, 66 insertions(+), 6 deletions(-) (limited to 'crates/ide_assists') 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 +} "#, ); } diff --git a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs index 26019c793..26778ee74 100644 --- a/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs +++ b/crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs @@ -32,7 +32,6 @@ pub(crate) fn replace_qualified_name_with_use( let target = path.syntax().text_range(); let scope = ImportScope::find_insert_use_container_with_macros(path.syntax(), &ctx.sema)?; - let syntax = scope.as_syntax_node(); acc.add( AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite), "Replace qualified path with use", @@ -40,11 +39,13 @@ pub(crate) fn replace_qualified_name_with_use( |builder| { // Now that we've brought the name into scope, re-qualify all paths that could be // affected (that is, all paths inside the node we added the `use` to). - let syntax = builder.make_syntax_mut(syntax.clone()); - if let Some(ref import_scope) = ImportScope::from(syntax.clone()) { - shorten_paths(&syntax, &path.clone_for_update()); - insert_use(import_scope, path, &ctx.config.insert_use); - } + let scope = match scope { + 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)), + }; + shorten_paths(scope.as_syntax_node(), &path.clone_for_update()); + insert_use(&scope, path, &ctx.config.insert_use); }, ) } -- cgit v1.2.3