diff options
Diffstat (limited to 'crates/ide_assists/src')
-rw-r--r-- | crates/ide_assists/src/handlers/auto_import.rs | 58 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/extract_function.rs | 26 |
2 files changed, 20 insertions, 64 deletions
diff --git a/crates/ide_assists/src/handlers/auto_import.rs b/crates/ide_assists/src/handlers/auto_import.rs index 8df8b060d..accc345fc 100644 --- a/crates/ide_assists/src/handlers/auto_import.rs +++ b/crates/ide_assists/src/handlers/auto_import.rs | |||
@@ -995,62 +995,4 @@ const _: () = { | |||
995 | "#, | 995 | "#, |
996 | ); | 996 | ); |
997 | } | 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 | } | ||
1056 | } | 998 | } |
diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index ac7f0959b..870d4f665 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs | |||
@@ -109,10 +109,15 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext) -> Option | |||
109 | 109 | ||
110 | let new_indent = IndentLevel::from_node(&insert_after); | 110 | let new_indent = IndentLevel::from_node(&insert_after); |
111 | let old_indent = fun.body.indent_level(); | 111 | let old_indent = fun.body.indent_level(); |
112 | let body_contains_await = body_contains_await(&fun.body); | ||
112 | 113 | ||
113 | builder.replace(target_range, format_replacement(ctx, &fun, old_indent)); | 114 | builder.replace( |
115 | target_range, | ||
116 | format_replacement(ctx, &fun, old_indent, body_contains_await), | ||
117 | ); | ||
114 | 118 | ||
115 | let fn_def = format_function(ctx, module, &fun, old_indent, new_indent); | 119 | let fn_def = |
120 | format_function(ctx, module, &fun, old_indent, new_indent, body_contains_await); | ||
116 | let insert_offset = insert_after.text_range().end(); | 121 | let insert_offset = insert_after.text_range().end(); |
117 | match ctx.config.snippet_cap { | 122 | match ctx.config.snippet_cap { |
118 | Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def), | 123 | Some(cap) => builder.insert_snippet(cap, insert_offset, fn_def), |
@@ -954,7 +959,12 @@ fn scope_for_fn_insertion_node(node: &SyntaxNode, anchor: Anchor) -> Option<Synt | |||
954 | last_ancestor | 959 | last_ancestor |
955 | } | 960 | } |
956 | 961 | ||
957 | fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel) -> String { | 962 | fn format_replacement( |
963 | ctx: &AssistContext, | ||
964 | fun: &Function, | ||
965 | indent: IndentLevel, | ||
966 | body_contains_await: bool, | ||
967 | ) -> String { | ||
958 | let ret_ty = fun.return_type(ctx); | 968 | let ret_ty = fun.return_type(ctx); |
959 | 969 | ||
960 | let args = fun.params.iter().map(|param| param.to_arg(ctx)); | 970 | let args = fun.params.iter().map(|param| param.to_arg(ctx)); |
@@ -994,6 +1004,9 @@ fn format_replacement(ctx: &AssistContext, fun: &Function, indent: IndentLevel) | |||
994 | } | 1004 | } |
995 | } | 1005 | } |
996 | format_to!(buf, "{}", expr); | 1006 | format_to!(buf, "{}", expr); |
1007 | if body_contains_await { | ||
1008 | buf.push_str(".await"); | ||
1009 | } | ||
997 | if fun.ret_ty.is_unit() | 1010 | if fun.ret_ty.is_unit() |
998 | && (!fun.vars_defined_in_body_and_outlive.is_empty() || !expr.is_block_like()) | 1011 | && (!fun.vars_defined_in_body_and_outlive.is_empty() || !expr.is_block_like()) |
999 | { | 1012 | { |
@@ -1122,12 +1135,13 @@ fn format_function( | |||
1122 | fun: &Function, | 1135 | fun: &Function, |
1123 | old_indent: IndentLevel, | 1136 | old_indent: IndentLevel, |
1124 | new_indent: IndentLevel, | 1137 | new_indent: IndentLevel, |
1138 | body_contains_await: bool, | ||
1125 | ) -> String { | 1139 | ) -> String { |
1126 | let mut fn_def = String::new(); | 1140 | let mut fn_def = String::new(); |
1127 | let params = make_param_list(ctx, module, fun); | 1141 | let params = make_param_list(ctx, module, fun); |
1128 | let ret_ty = make_ret_ty(ctx, module, fun); | 1142 | let ret_ty = make_ret_ty(ctx, module, fun); |
1129 | let body = make_body(ctx, old_indent, new_indent, fun); | 1143 | let body = make_body(ctx, old_indent, new_indent, fun); |
1130 | let async_kw = if body_contains_await(&fun.body) { "async " } else { "" }; | 1144 | let async_kw = if body_contains_await { "async " } else { "" }; |
1131 | match ctx.config.snippet_cap { | 1145 | match ctx.config.snippet_cap { |
1132 | Some(_) => format_to!(fn_def, "\n\n{}{}fn $0{}{}", new_indent, async_kw, fun.name, params), | 1146 | Some(_) => format_to!(fn_def, "\n\n{}{}fn $0{}{}", new_indent, async_kw, fun.name, params), |
1133 | None => format_to!(fn_def, "\n\n{}{}fn {}{}", new_indent, async_kw, fun.name, params), | 1147 | None => format_to!(fn_def, "\n\n{}{}fn {}{}", new_indent, async_kw, fun.name, params), |
@@ -3681,7 +3695,7 @@ async fn some_function() { | |||
3681 | "#, | 3695 | "#, |
3682 | r#" | 3696 | r#" |
3683 | fn main() { | 3697 | fn main() { |
3684 | fun_name(); | 3698 | fun_name().await; |
3685 | } | 3699 | } |
3686 | 3700 | ||
3687 | async fn $0fun_name() { | 3701 | async fn $0fun_name() { |
@@ -3710,7 +3724,7 @@ async fn some_function() { | |||
3710 | "#, | 3724 | "#, |
3711 | r#" | 3725 | r#" |
3712 | fn main() { | 3726 | fn main() { |
3713 | fun_name(); | 3727 | fun_name().await; |
3714 | } | 3728 | } |
3715 | 3729 | ||
3716 | async fn $0fun_name() { | 3730 | async fn $0fun_name() { |