diff options
Diffstat (limited to 'crates/ra_assists/src')
5 files changed, 52 insertions, 9 deletions
diff --git a/crates/ra_assists/src/handlers/add_explicit_type.rs b/crates/ra_assists/src/handlers/add_explicit_type.rs index d86d804b2..6c56d93d8 100644 --- a/crates/ra_assists/src/handlers/add_explicit_type.rs +++ b/crates/ra_assists/src/handlers/add_explicit_type.rs | |||
@@ -52,21 +52,22 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option<Assist> { | |||
52 | } | 52 | } |
53 | // Infer type | 53 | // Infer type |
54 | let ty = ctx.sema.type_of_expr(&expr)?; | 54 | let ty = ctx.sema.type_of_expr(&expr)?; |
55 | // Assist not applicable if the type is unknown | 55 | |
56 | if ty.contains_unknown() { | 56 | if ty.contains_unknown() || ty.is_closure() { |
57 | return None; | 57 | return None; |
58 | } | 58 | } |
59 | 59 | ||
60 | let db = ctx.db; | 60 | let db = ctx.db; |
61 | let new_type_string = ty.display_truncated(db, None).to_string(); | ||
61 | ctx.add_assist( | 62 | ctx.add_assist( |
62 | AssistId("add_explicit_type"), | 63 | AssistId("add_explicit_type"), |
63 | format!("Insert explicit type '{}'", ty.display(db)), | 64 | format!("Insert explicit type '{}'", new_type_string), |
64 | |edit| { | 65 | |edit| { |
65 | edit.target(pat_range); | 66 | edit.target(pat_range); |
66 | if let Some(ascribed_ty) = ascribed_ty { | 67 | if let Some(ascribed_ty) = ascribed_ty { |
67 | edit.replace(ascribed_ty.syntax().text_range(), format!("{}", ty.display(db))); | 68 | edit.replace(ascribed_ty.syntax().text_range(), new_type_string); |
68 | } else { | 69 | } else { |
69 | edit.insert(name_range.end(), format!(": {}", ty.display(db))); | 70 | edit.insert(name_range.end(), format!(": {}", new_type_string)); |
70 | } | 71 | } |
71 | }, | 72 | }, |
72 | ) | 73 | ) |
@@ -174,4 +175,41 @@ mod tests { | |||
174 | "fn f() <|>{let a = match 1 {2 => 3, 3 => 5};}", | 175 | "fn f() <|>{let a = match 1 {2 => 3, 3 => 5};}", |
175 | ) | 176 | ) |
176 | } | 177 | } |
178 | |||
179 | #[test] | ||
180 | fn closure_parameters_are_not_added() { | ||
181 | check_assist_not_applicable( | ||
182 | add_explicit_type, | ||
183 | r#" | ||
184 | fn main() { | ||
185 | let multiply_by_two<|> = |i| i * 3; | ||
186 | let six = multiply_by_two(2); | ||
187 | }"#, | ||
188 | ) | ||
189 | } | ||
190 | |||
191 | #[test] | ||
192 | fn default_generics_should_not_be_added() { | ||
193 | check_assist( | ||
194 | add_explicit_type, | ||
195 | r#" | ||
196 | struct Test<K, T = u8> { | ||
197 | k: K, | ||
198 | t: T, | ||
199 | } | ||
200 | |||
201 | fn main() { | ||
202 | let test<|> = Test { t: 23, k: 33 }; | ||
203 | }"#, | ||
204 | r#" | ||
205 | struct Test<K, T = u8> { | ||
206 | k: K, | ||
207 | t: T, | ||
208 | } | ||
209 | |||
210 | fn main() { | ||
211 | let test<|>: Test<i32> = Test { t: 23, k: 33 }; | ||
212 | }"#, | ||
213 | ); | ||
214 | } | ||
177 | } | 215 | } |
diff --git a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs index 864373aa5..0621487e8 100644 --- a/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs +++ b/crates/ra_assists/src/handlers/add_from_impl_for_enum.rs | |||
@@ -98,7 +98,7 @@ fn already_has_from_impl( | |||
98 | }; | 98 | }; |
99 | let var_ty = hir_enum_var.fields(sema.db)[0].signature_ty(sema.db); | 99 | let var_ty = hir_enum_var.fields(sema.db)[0].signature_ty(sema.db); |
100 | 100 | ||
101 | e_ty.impls_trait(sema.db, from_trait, &[var_ty.clone()]) | 101 | e_ty.impls_trait(sema.db, from_trait, &[var_ty]) |
102 | } | 102 | } |
103 | 103 | ||
104 | #[cfg(test)] | 104 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/handlers/introduce_variable.rs b/crates/ra_assists/src/handlers/introduce_variable.rs index 8d0f7e922..8c09e6bcd 100644 --- a/crates/ra_assists/src/handlers/introduce_variable.rs +++ b/crates/ra_assists/src/handlers/introduce_variable.rs | |||
@@ -124,7 +124,7 @@ fn anchor_stmt(expr: ast::Expr) -> Option<(SyntaxNode, bool)> { | |||
124 | } | 124 | } |
125 | } | 125 | } |
126 | 126 | ||
127 | if ast::Stmt::cast(node.clone().into()).is_some() { | 127 | if ast::Stmt::cast(node.clone()).is_some() { |
128 | return Some((node, false)); | 128 | return Some((node, false)); |
129 | } | 129 | } |
130 | 130 | ||
diff --git a/crates/ra_assists/src/handlers/merge_imports.rs b/crates/ra_assists/src/handlers/merge_imports.rs index ef0ce0586..4be1238f1 100644 --- a/crates/ra_assists/src/handlers/merge_imports.rs +++ b/crates/ra_assists/src/handlers/merge_imports.rs | |||
@@ -30,7 +30,7 @@ pub(crate) fn merge_imports(ctx: AssistCtx) -> Option<Assist> { | |||
30 | .filter_map(|dir| neighbor(&use_item, dir)) | 30 | .filter_map(|dir| neighbor(&use_item, dir)) |
31 | .filter_map(|it| Some((it.clone(), it.use_tree()?))) | 31 | .filter_map(|it| Some((it.clone(), it.use_tree()?))) |
32 | .find_map(|(use_item, use_tree)| { | 32 | .find_map(|(use_item, use_tree)| { |
33 | Some((try_merge_trees(&tree, &use_tree)?, use_item.clone())) | 33 | Some((try_merge_trees(&tree, &use_tree)?, use_item)) |
34 | })?; | 34 | })?; |
35 | 35 | ||
36 | rewriter.replace_ast(&tree, &merged); | 36 | rewriter.replace_ast(&tree, &merged); |
diff --git a/crates/ra_assists/src/handlers/split_import.rs b/crates/ra_assists/src/handlers/split_import.rs index d9244f22d..f25826796 100644 --- a/crates/ra_assists/src/handlers/split_import.rs +++ b/crates/ra_assists/src/handlers/split_import.rs | |||
@@ -37,7 +37,7 @@ pub(crate) fn split_import(ctx: AssistCtx) -> Option<Assist> { | |||
37 | 37 | ||
38 | #[cfg(test)] | 38 | #[cfg(test)] |
39 | mod tests { | 39 | mod tests { |
40 | use crate::helpers::{check_assist, check_assist_target}; | 40 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_target}; |
41 | 41 | ||
42 | use super::*; | 42 | use super::*; |
43 | 43 | ||
@@ -63,4 +63,9 @@ mod tests { | |||
63 | fn split_import_target() { | 63 | fn split_import_target() { |
64 | check_assist_target(split_import, "use crate::<|>db::{RootDatabase, FileSymbol}", "::"); | 64 | check_assist_target(split_import, "use crate::<|>db::{RootDatabase, FileSymbol}", "::"); |
65 | } | 65 | } |
66 | |||
67 | #[test] | ||
68 | fn issue4044() { | ||
69 | check_assist_not_applicable(split_import, "use crate::<|>:::self;") | ||
70 | } | ||
66 | } | 71 | } |