diff options
author | Dawer <[email protected]> | 2021-05-19 19:12:09 +0100 |
---|---|---|
committer | Dawer <[email protected]> | 2021-05-19 19:27:51 +0100 |
commit | 5c5fedb9454b4fcb237728da7c9e29e981fcdc3a (patch) | |
tree | 6f8d68f044ef88b5d90c90b43d96dd501e29f0c7 /crates/ide_assists | |
parent | 49a5d6a8d4a5da296ead4c44ac43090b9b0e8034 (diff) |
add_explicit_type respects `@` patterns
Diffstat (limited to 'crates/ide_assists')
-rw-r--r-- | crates/ide_assists/src/handlers/add_explicit_type.rs | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/crates/ide_assists/src/handlers/add_explicit_type.rs b/crates/ide_assists/src/handlers/add_explicit_type.rs index 36589203d..b7617ca3d 100644 --- a/crates/ide_assists/src/handlers/add_explicit_type.rs +++ b/crates/ide_assists/src/handlers/add_explicit_type.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use hir::HirDisplay; | 1 | use hir::HirDisplay; |
2 | use syntax::{ | 2 | use syntax::{ |
3 | ast::{self, AstNode, LetStmt, NameOwner}, | 3 | ast::{self, AstNode, LetStmt}, |
4 | TextRange, | 4 | TextRange, |
5 | }; | 5 | }; |
6 | 6 | ||
@@ -31,9 +31,6 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio | |||
31 | _ => return None, | 31 | _ => return None, |
32 | }; | 32 | }; |
33 | let pat_range = pat.syntax().text_range(); | 33 | let pat_range = pat.syntax().text_range(); |
34 | // The binding must have a name | ||
35 | let name = pat.name()?; | ||
36 | let name_range = name.syntax().text_range(); | ||
37 | 34 | ||
38 | // Assist should only be applicable if cursor is between 'let' and '=' | 35 | // Assist should only be applicable if cursor is between 'let' and '=' |
39 | let cursor_in_range = { | 36 | let cursor_in_range = { |
@@ -74,7 +71,7 @@ pub(crate) fn add_explicit_type(acc: &mut Assists, ctx: &AssistContext) -> Optio | |||
74 | builder.replace(ascribed_ty.syntax().text_range(), inferred_type); | 71 | builder.replace(ascribed_ty.syntax().text_range(), inferred_type); |
75 | } | 72 | } |
76 | None => { | 73 | None => { |
77 | builder.insert(name_range.end(), format!(": {}", inferred_type)); | 74 | builder.insert(pat_range.end(), format!(": {}", inferred_type)); |
78 | } | 75 | } |
79 | }, | 76 | }, |
80 | ) | 77 | ) |
@@ -246,4 +243,22 @@ fn main() { | |||
246 | "#, | 243 | "#, |
247 | ); | 244 | ); |
248 | } | 245 | } |
246 | |||
247 | #[test] | ||
248 | fn type_should_be_added_after_pattern() { | ||
249 | // LetStmt = Attr* 'let' Pat (':' Type)? '=' initializer:Expr ';' | ||
250 | check_assist( | ||
251 | add_explicit_type, | ||
252 | r#" | ||
253 | fn main() { | ||
254 | let $0test @ () = (); | ||
255 | } | ||
256 | "#, | ||
257 | r#" | ||
258 | fn main() { | ||
259 | let test @ (): () = (); | ||
260 | } | ||
261 | "#, | ||
262 | ); | ||
263 | } | ||
249 | } | 264 | } |