aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/handlers
diff options
context:
space:
mode:
authorDawer <[email protected]>2021-05-19 19:12:09 +0100
committerDawer <[email protected]>2021-05-19 19:27:51 +0100
commit5c5fedb9454b4fcb237728da7c9e29e981fcdc3a (patch)
tree6f8d68f044ef88b5d90c90b43d96dd501e29f0c7 /crates/ide_assists/src/handlers
parent49a5d6a8d4a5da296ead4c44ac43090b9b0e8034 (diff)
add_explicit_type respects `@` patterns
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r--crates/ide_assists/src/handlers/add_explicit_type.rs25
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 @@
1use hir::HirDisplay; 1use hir::HirDisplay;
2use syntax::{ 2use 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#"
253fn main() {
254 let $0test @ () = ();
255}
256"#,
257 r#"
258fn main() {
259 let test @ (): () = ();
260}
261"#,
262 );
263 }
249} 264}