Skip to content

Commit c3b9a19

Browse files
author
TomasKralCZ
committed
fix 'add_explicit_type' assist range
1 parent 8dc94a4 commit c3b9a19

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

crates/ra_assists/src/assists/add_explicit_type.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use hir::{db::HirDatabase, HirDisplay};
22
use ra_syntax::{
33
ast::{self, AstNode, LetStmt, NameOwner},
4-
T,
4+
SyntaxKind::EQ,
5+
TextRange, T,
56
};
67

78
use crate::{Assist, AssistCtx, AssistId};
@@ -34,6 +35,14 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi
3435
// The binding must have a name
3536
let name = pat.name()?;
3637
let name_range = name.syntax().text_range();
38+
// Assist should only be applicable if cursor is between 'let' and '='
39+
let stmt_range = stmt.syntax().text_range();
40+
let eq_range = stmt.syntax().descendants_with_tokens().find(|t| t.kind() == EQ)?.text_range();
41+
let let_range = TextRange::from_to(stmt_range.start(), eq_range.start());
42+
let cursor_in_range = ctx.frange.range.is_subrange(&let_range);
43+
if !cursor_in_range {
44+
return None;
45+
}
3746
// Assist not applicable if the type has already been specified
3847
if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) {
3948
return None;
@@ -109,4 +118,20 @@ mod tests {
109118
fn add_explicit_type_not_applicable_if_specified_ty_is_tuple() {
110119
check_assist_not_applicable(add_explicit_type, "fn f() { let a<|>: (i32, i32) = (3, 4); }");
111120
}
121+
122+
#[test]
123+
fn add_explicit_type_not_applicable_if_cursor_after_equals() {
124+
check_assist_not_applicable(
125+
add_explicit_type,
126+
"fn f() {let a =<|> match 1 {2 => 3, 3 => 5};}",
127+
)
128+
}
129+
130+
#[test]
131+
fn add_explicit_type_not_applicable_if_cursor_before_let() {
132+
check_assist_not_applicable(
133+
add_explicit_type,
134+
"fn f() <|>{let a = match 1 {2 => 3, 3 => 5};}",
135+
)
136+
}
112137
}

0 commit comments

Comments
 (0)