Skip to content

Commit de24097

Browse files
bors[bot]TomasKralCZ
andauthored
Merge #2877
2877: "Insert explicit type " assist fix #2869, fix typo r=matklad a=TomasKralCZ So this was quite straightforward. I basically looked at how the other assists work and tried doing something simillar. I also fixed a typo in the other assist. Co-authored-by: TomasKralCZ <tomas@kral.hk>
2 parents 648241e + 72792f6 commit de24097

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

crates/ra_assists/src/assists/add_explicit_type.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use hir::{db::HirDatabase, HirDisplay};
22
use ra_syntax::{
33
ast::{self, AstNode, LetStmt, NameOwner},
4-
T,
4+
TextRange, T,
55
};
66

77
use crate::{Assist, AssistCtx, AssistId};
@@ -34,6 +34,14 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi
3434
// The binding must have a name
3535
let name = pat.name()?;
3636
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+
}
3745
// Assist not applicable if the type has already been specified
3846
if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) {
3947
return None;
@@ -109,4 +117,20 @@ mod tests {
109117
fn add_explicit_type_not_applicable_if_specified_ty_is_tuple() {
110118
check_assist_not_applicable(add_explicit_type, "fn f() { let a<|>: (i32, i32) = (3, 4); }");
111119
}
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+
}
112136
}

crates/ra_assists/src/assists/inline_local_variable.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{Assist, AssistCtx, AssistId};
2323
// (1 + 2) * 4;
2424
// }
2525
// ```
26-
pub(crate) fn inline_local_varialbe(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
26+
pub(crate) fn inline_local_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
2727
let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?;
2828
let bind_pat = match let_stmt.pat()? {
2929
ast::Pat::BindPat(pat) => pat,
@@ -117,7 +117,7 @@ mod tests {
117117
#[test]
118118
fn test_inline_let_bind_literal_expr() {
119119
check_assist(
120-
inline_local_varialbe,
120+
inline_local_variable,
121121
"
122122
fn bar(a: usize) {}
123123
fn foo() {
@@ -151,7 +151,7 @@ fn foo() {
151151
#[test]
152152
fn test_inline_let_bind_bin_expr() {
153153
check_assist(
154-
inline_local_varialbe,
154+
inline_local_variable,
155155
"
156156
fn bar(a: usize) {}
157157
fn foo() {
@@ -185,7 +185,7 @@ fn foo() {
185185
#[test]
186186
fn test_inline_let_bind_function_call_expr() {
187187
check_assist(
188-
inline_local_varialbe,
188+
inline_local_variable,
189189
"
190190
fn bar(a: usize) {}
191191
fn foo() {
@@ -219,7 +219,7 @@ fn foo() {
219219
#[test]
220220
fn test_inline_let_bind_cast_expr() {
221221
check_assist(
222-
inline_local_varialbe,
222+
inline_local_variable,
223223
"
224224
fn bar(a: usize): usize { a }
225225
fn foo() {
@@ -253,7 +253,7 @@ fn foo() {
253253
#[test]
254254
fn test_inline_let_bind_block_expr() {
255255
check_assist(
256-
inline_local_varialbe,
256+
inline_local_variable,
257257
"
258258
fn foo() {
259259
let a<|> = { 10 + 1 };
@@ -285,7 +285,7 @@ fn foo() {
285285
#[test]
286286
fn test_inline_let_bind_paren_expr() {
287287
check_assist(
288-
inline_local_varialbe,
288+
inline_local_variable,
289289
"
290290
fn foo() {
291291
let a<|> = ( 10 + 1 );
@@ -317,7 +317,7 @@ fn foo() {
317317
#[test]
318318
fn test_not_inline_mut_variable() {
319319
check_assist_not_applicable(
320-
inline_local_varialbe,
320+
inline_local_variable,
321321
"
322322
fn foo() {
323323
let mut a<|> = 1 + 1;
@@ -329,7 +329,7 @@ fn foo() {
329329
#[test]
330330
fn test_call_expr() {
331331
check_assist(
332-
inline_local_varialbe,
332+
inline_local_variable,
333333
"
334334
fn foo() {
335335
let a<|> = bar(10 + 1);
@@ -347,7 +347,7 @@ fn foo() {
347347
#[test]
348348
fn test_index_expr() {
349349
check_assist(
350-
inline_local_varialbe,
350+
inline_local_variable,
351351
"
352352
fn foo() {
353353
let x = vec![1, 2, 3];
@@ -367,7 +367,7 @@ fn foo() {
367367
#[test]
368368
fn test_method_call_expr() {
369369
check_assist(
370-
inline_local_varialbe,
370+
inline_local_variable,
371371
"
372372
fn foo() {
373373
let bar = vec![1];
@@ -387,7 +387,7 @@ fn foo() {
387387
#[test]
388388
fn test_field_expr() {
389389
check_assist(
390-
inline_local_varialbe,
390+
inline_local_variable,
391391
"
392392
struct Bar {
393393
foo: usize
@@ -415,7 +415,7 @@ fn foo() {
415415
#[test]
416416
fn test_try_expr() {
417417
check_assist(
418-
inline_local_varialbe,
418+
inline_local_variable,
419419
"
420420
fn foo() -> Option<usize> {
421421
let bar = Some(1);
@@ -437,7 +437,7 @@ fn foo() -> Option<usize> {
437437
#[test]
438438
fn test_ref_expr() {
439439
check_assist(
440-
inline_local_varialbe,
440+
inline_local_variable,
441441
"
442442
fn foo() {
443443
let bar = 10;
@@ -455,7 +455,7 @@ fn foo() {
455455
#[test]
456456
fn test_tuple_expr() {
457457
check_assist(
458-
inline_local_varialbe,
458+
inline_local_variable,
459459
"
460460
fn foo() {
461461
let a<|> = (10, 20);
@@ -471,7 +471,7 @@ fn foo() {
471471
#[test]
472472
fn test_array_expr() {
473473
check_assist(
474-
inline_local_varialbe,
474+
inline_local_variable,
475475
"
476476
fn foo() {
477477
let a<|> = [1, 2, 3];
@@ -487,7 +487,7 @@ fn foo() {
487487
#[test]
488488
fn test_paren() {
489489
check_assist(
490-
inline_local_varialbe,
490+
inline_local_variable,
491491
"
492492
fn foo() {
493493
let a<|> = (10 + 20);
@@ -505,7 +505,7 @@ fn foo() {
505505
#[test]
506506
fn test_path_expr() {
507507
check_assist(
508-
inline_local_varialbe,
508+
inline_local_variable,
509509
"
510510
fn foo() {
511511
let d = 10;
@@ -525,7 +525,7 @@ fn foo() {
525525
#[test]
526526
fn test_block_expr() {
527527
check_assist(
528-
inline_local_varialbe,
528+
inline_local_variable,
529529
"
530530
fn foo() {
531531
let a<|> = { 10 };
@@ -543,7 +543,7 @@ fn foo() {
543543
#[test]
544544
fn test_used_in_different_expr1() {
545545
check_assist(
546-
inline_local_varialbe,
546+
inline_local_variable,
547547
"
548548
fn foo() {
549549
let a<|> = 10 + 20;
@@ -565,7 +565,7 @@ fn foo() {
565565
#[test]
566566
fn test_used_in_for_expr() {
567567
check_assist(
568-
inline_local_varialbe,
568+
inline_local_variable,
569569
"
570570
fn foo() {
571571
let a<|> = vec![10, 20];
@@ -581,7 +581,7 @@ fn foo() {
581581
#[test]
582582
fn test_used_in_while_expr() {
583583
check_assist(
584-
inline_local_varialbe,
584+
inline_local_variable,
585585
"
586586
fn foo() {
587587
let a<|> = 1 > 0;
@@ -597,7 +597,7 @@ fn foo() {
597597
#[test]
598598
fn test_used_in_break_expr() {
599599
check_assist(
600-
inline_local_varialbe,
600+
inline_local_variable,
601601
"
602602
fn foo() {
603603
let a<|> = 1 + 1;
@@ -617,7 +617,7 @@ fn foo() {
617617
#[test]
618618
fn test_used_in_return_expr() {
619619
check_assist(
620-
inline_local_varialbe,
620+
inline_local_variable,
621621
"
622622
fn foo() {
623623
let a<|> = 1 > 0;
@@ -633,7 +633,7 @@ fn foo() {
633633
#[test]
634634
fn test_used_in_match_expr() {
635635
check_assist(
636-
inline_local_varialbe,
636+
inline_local_variable,
637637
"
638638
fn foo() {
639639
let a<|> = 1 > 0;

crates/ra_assists/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ mod assists {
157157
add_import::add_import,
158158
add_missing_impl_members::add_missing_impl_members,
159159
add_missing_impl_members::add_missing_default_members,
160-
inline_local_variable::inline_local_varialbe,
160+
inline_local_variable::inline_local_variable,
161161
move_guard::move_guard_to_arm_body,
162162
move_guard::move_arm_cond_to_match_guard,
163163
move_bounds::move_bounds_to_where_clause,

crates/ra_syntax/src/ast/extensions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ impl ast::LetStmt {
234234
Some(node) => node.kind() == T![;],
235235
}
236236
}
237+
238+
pub fn eq_token(&self) -> Option<SyntaxToken> {
239+
self.syntax().children_with_tokens().find(|t| t.kind() == EQ).and_then(|it| it.into_token())
240+
}
237241
}
238242

239243
impl ast::ExprStmt {

0 commit comments

Comments
 (0)