diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-18 22:58:35 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-18 22:58:35 +0100 |
commit | 0d3bad85e60d011b35da0a8e5dd6611934f6816d (patch) | |
tree | 26403f2e9927f32698009cad0d8995ffa9cb058b /crates/ide_assists/src/handlers | |
parent | d9666ce5098442ab0cbac4d4363435074eb12923 (diff) | |
parent | 344cb5e76a31b8ef7ae80ce0ef39c54248ad8df7 (diff) |
Merge #9335
9335: feat: Don't insert imports outside of cfg attributed items r=Veykril a=Veykril
Closes #6939
Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/auto_import.rs | 59 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs | 13 |
2 files changed, 66 insertions, 6 deletions
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<()> | |||
103 | let scope = match scope.clone() { | 103 | let scope = match scope.clone() { |
104 | ImportScope::File(it) => ImportScope::File(builder.make_mut(it)), | 104 | ImportScope::File(it) => ImportScope::File(builder.make_mut(it)), |
105 | ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)), | 105 | ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)), |
106 | ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)), | ||
106 | }; | 107 | }; |
107 | insert_use(&scope, mod_path_to_ast(&import.import_path), &ctx.config.insert_use); | 108 | insert_use(&scope, mod_path_to_ast(&import.import_path), &ctx.config.insert_use); |
108 | }, | 109 | }, |
@@ -994,4 +995,62 @@ const _: () = { | |||
994 | "#, | 995 | "#, |
995 | ); | 996 | ); |
996 | } | 997 | } |
998 | |||
999 | #[test] | ||
1000 | fn respects_cfg_attr() { | ||
1001 | check_assist( | ||
1002 | auto_import, | ||
1003 | r#" | ||
1004 | mod bar { | ||
1005 | pub struct Bar; | ||
1006 | } | ||
1007 | |||
1008 | #[cfg(test)] | ||
1009 | fn foo() { | ||
1010 | Bar$0 | ||
1011 | } | ||
1012 | "#, | ||
1013 | r#" | ||
1014 | mod bar { | ||
1015 | pub struct Bar; | ||
1016 | } | ||
1017 | |||
1018 | #[cfg(test)] | ||
1019 | fn foo() { | ||
1020 | use bar::Bar; | ||
1021 | |||
1022 | Bar | ||
1023 | } | ||
1024 | "#, | ||
1025 | ); | ||
1026 | } | ||
1027 | |||
1028 | #[test] | ||
1029 | fn respects_cfg_attr2() { | ||
1030 | check_assist( | ||
1031 | auto_import, | ||
1032 | r#" | ||
1033 | mod bar { | ||
1034 | pub struct Bar; | ||
1035 | } | ||
1036 | |||
1037 | #[cfg(test)] | ||
1038 | const FOO: Bar = { | ||
1039 | Bar$0 | ||
1040 | } | ||
1041 | "#, | ||
1042 | r#" | ||
1043 | mod bar { | ||
1044 | pub struct Bar; | ||
1045 | } | ||
1046 | |||
1047 | #[cfg(test)] | ||
1048 | const FOO: Bar = { | ||
1049 | use bar::Bar; | ||
1050 | |||
1051 | Bar | ||
1052 | } | ||
1053 | "#, | ||
1054 | ); | ||
1055 | } | ||
997 | } | 1056 | } |
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( | |||
32 | 32 | ||
33 | let target = path.syntax().text_range(); | 33 | let target = path.syntax().text_range(); |
34 | let scope = ImportScope::find_insert_use_container_with_macros(path.syntax(), &ctx.sema)?; | 34 | let scope = ImportScope::find_insert_use_container_with_macros(path.syntax(), &ctx.sema)?; |
35 | let syntax = scope.as_syntax_node(); | ||
36 | acc.add( | 35 | acc.add( |
37 | AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite), | 36 | AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite), |
38 | "Replace qualified path with use", | 37 | "Replace qualified path with use", |
@@ -40,11 +39,13 @@ pub(crate) fn replace_qualified_name_with_use( | |||
40 | |builder| { | 39 | |builder| { |
41 | // Now that we've brought the name into scope, re-qualify all paths that could be | 40 | // Now that we've brought the name into scope, re-qualify all paths that could be |
42 | // affected (that is, all paths inside the node we added the `use` to). | 41 | // affected (that is, all paths inside the node we added the `use` to). |
43 | let syntax = builder.make_syntax_mut(syntax.clone()); | 42 | let scope = match scope { |
44 | if let Some(ref import_scope) = ImportScope::from(syntax.clone()) { | 43 | ImportScope::File(it) => ImportScope::File(builder.make_mut(it)), |
45 | shorten_paths(&syntax, &path.clone_for_update()); | 44 | ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)), |
46 | insert_use(import_scope, path, &ctx.config.insert_use); | 45 | ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)), |
47 | } | 46 | }; |
47 | shorten_paths(scope.as_syntax_node(), &path.clone_for_update()); | ||
48 | insert_use(&scope, path, &ctx.config.insert_use); | ||
48 | }, | 49 | }, |
49 | ) | 50 | ) |
50 | } | 51 | } |