aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists/add_explicit_type.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists/add_explicit_type.rs')
-rw-r--r--crates/ra_assists/src/assists/add_explicit_type.rs26
1 files changed, 25 insertions, 1 deletions
diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/assists/add_explicit_type.rs
index f9f826b88..38a351a54 100644
--- a/crates/ra_assists/src/assists/add_explicit_type.rs
+++ b/crates/ra_assists/src/assists/add_explicit_type.rs
@@ -1,7 +1,7 @@
1use hir::{db::HirDatabase, HirDisplay}; 1use hir::{db::HirDatabase, HirDisplay};
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{self, AstNode, LetStmt, NameOwner}, 3 ast::{self, AstNode, LetStmt, NameOwner},
4 T, 4 TextRange, T,
5}; 5};
6 6
7use crate::{Assist, AssistCtx, AssistId}; 7use crate::{Assist, AssistCtx, AssistId};
@@ -34,6 +34,14 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi
34 // The binding must have a name 34 // The binding must have a name
35 let name = pat.name()?; 35 let name = pat.name()?;
36 let name_range = name.syntax().text_range(); 36 let name_range = name.syntax().text_range();
37 // Assist should only be applicable if cursor is between 'let' and '='
38 let stmt_range = stmt.syntax().text_range();
39 let eq_range = stmt.eq_token()?.text_range();
40 let let_range = TextRange::from_to(stmt_range.start(), eq_range.start());
41 let cursor_in_range = ctx.frange.range.is_subrange(&let_range);
42 if !cursor_in_range {
43 return None;
44 }
37 // Assist not applicable if the type has already been specified 45 // Assist not applicable if the type has already been specified
38 if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) { 46 if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) {
39 return None; 47 return None;
@@ -109,4 +117,20 @@ mod tests {
109 fn add_explicit_type_not_applicable_if_specified_ty_is_tuple() { 117 fn add_explicit_type_not_applicable_if_specified_ty_is_tuple() {
110 check_assist_not_applicable(add_explicit_type, "fn f() { let a<|>: (i32, i32) = (3, 4); }"); 118 check_assist_not_applicable(add_explicit_type, "fn f() { let a<|>: (i32, i32) = (3, 4); }");
111 } 119 }
120
121 #[test]
122 fn add_explicit_type_not_applicable_if_cursor_after_equals() {
123 check_assist_not_applicable(
124 add_explicit_type,
125 "fn f() {let a =<|> match 1 {2 => 3, 3 => 5};}",
126 )
127 }
128
129 #[test]
130 fn add_explicit_type_not_applicable_if_cursor_before_let() {
131 check_assist_not_applicable(
132 add_explicit_type,
133 "fn f() <|>{let a = match 1 {2 => 3, 3 => 5};}",
134 )
135 }
112} 136}