diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-01-20 10:16:06 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-01-20 10:16:06 +0000 |
commit | de2409702ba639f06cf340f5e3f3b7e671021d1b (patch) | |
tree | 252a7b57f2a057540ce2ec554678bde15ab69ca0 /crates/ra_assists | |
parent | 648241ee930de08ba70b0b5c2172dfb3cc7a34c6 (diff) | |
parent | 72792f6bc5cc3c81b53fa8e71304b0f7321f8e22 (diff) |
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 <[email protected]>
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assists/add_explicit_type.rs | 26 | ||||
-rw-r--r-- | crates/ra_assists/src/assists/inline_local_variable.rs | 50 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 2 |
3 files changed, 51 insertions, 27 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 @@ | |||
1 | use hir::{db::HirDatabase, HirDisplay}; | 1 | use hir::{db::HirDatabase, HirDisplay}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, AstNode, LetStmt, NameOwner}, | 3 | ast::{self, AstNode, LetStmt, NameOwner}, |
4 | T, | 4 | TextRange, T, |
5 | }; | 5 | }; |
6 | 6 | ||
7 | use crate::{Assist, AssistCtx, AssistId}; | 7 | use 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 | } |
diff --git a/crates/ra_assists/src/assists/inline_local_variable.rs b/crates/ra_assists/src/assists/inline_local_variable.rs index 45e0f983f..d0c5c3b8c 100644 --- a/crates/ra_assists/src/assists/inline_local_variable.rs +++ b/crates/ra_assists/src/assists/inline_local_variable.rs | |||
@@ -23,7 +23,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
23 | // (1 + 2) * 4; | 23 | // (1 + 2) * 4; |
24 | // } | 24 | // } |
25 | // ``` | 25 | // ``` |
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> { |
27 | let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?; | 27 | let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?; |
28 | let bind_pat = match let_stmt.pat()? { | 28 | let bind_pat = match let_stmt.pat()? { |
29 | ast::Pat::BindPat(pat) => pat, | 29 | ast::Pat::BindPat(pat) => pat, |
@@ -117,7 +117,7 @@ mod tests { | |||
117 | #[test] | 117 | #[test] |
118 | fn test_inline_let_bind_literal_expr() { | 118 | fn test_inline_let_bind_literal_expr() { |
119 | check_assist( | 119 | check_assist( |
120 | inline_local_varialbe, | 120 | inline_local_variable, |
121 | " | 121 | " |
122 | fn bar(a: usize) {} | 122 | fn bar(a: usize) {} |
123 | fn foo() { | 123 | fn foo() { |
@@ -151,7 +151,7 @@ fn foo() { | |||
151 | #[test] | 151 | #[test] |
152 | fn test_inline_let_bind_bin_expr() { | 152 | fn test_inline_let_bind_bin_expr() { |
153 | check_assist( | 153 | check_assist( |
154 | inline_local_varialbe, | 154 | inline_local_variable, |
155 | " | 155 | " |
156 | fn bar(a: usize) {} | 156 | fn bar(a: usize) {} |
157 | fn foo() { | 157 | fn foo() { |
@@ -185,7 +185,7 @@ fn foo() { | |||
185 | #[test] | 185 | #[test] |
186 | fn test_inline_let_bind_function_call_expr() { | 186 | fn test_inline_let_bind_function_call_expr() { |
187 | check_assist( | 187 | check_assist( |
188 | inline_local_varialbe, | 188 | inline_local_variable, |
189 | " | 189 | " |
190 | fn bar(a: usize) {} | 190 | fn bar(a: usize) {} |
191 | fn foo() { | 191 | fn foo() { |
@@ -219,7 +219,7 @@ fn foo() { | |||
219 | #[test] | 219 | #[test] |
220 | fn test_inline_let_bind_cast_expr() { | 220 | fn test_inline_let_bind_cast_expr() { |
221 | check_assist( | 221 | check_assist( |
222 | inline_local_varialbe, | 222 | inline_local_variable, |
223 | " | 223 | " |
224 | fn bar(a: usize): usize { a } | 224 | fn bar(a: usize): usize { a } |
225 | fn foo() { | 225 | fn foo() { |
@@ -253,7 +253,7 @@ fn foo() { | |||
253 | #[test] | 253 | #[test] |
254 | fn test_inline_let_bind_block_expr() { | 254 | fn test_inline_let_bind_block_expr() { |
255 | check_assist( | 255 | check_assist( |
256 | inline_local_varialbe, | 256 | inline_local_variable, |
257 | " | 257 | " |
258 | fn foo() { | 258 | fn foo() { |
259 | let a<|> = { 10 + 1 }; | 259 | let a<|> = { 10 + 1 }; |
@@ -285,7 +285,7 @@ fn foo() { | |||
285 | #[test] | 285 | #[test] |
286 | fn test_inline_let_bind_paren_expr() { | 286 | fn test_inline_let_bind_paren_expr() { |
287 | check_assist( | 287 | check_assist( |
288 | inline_local_varialbe, | 288 | inline_local_variable, |
289 | " | 289 | " |
290 | fn foo() { | 290 | fn foo() { |
291 | let a<|> = ( 10 + 1 ); | 291 | let a<|> = ( 10 + 1 ); |
@@ -317,7 +317,7 @@ fn foo() { | |||
317 | #[test] | 317 | #[test] |
318 | fn test_not_inline_mut_variable() { | 318 | fn test_not_inline_mut_variable() { |
319 | check_assist_not_applicable( | 319 | check_assist_not_applicable( |
320 | inline_local_varialbe, | 320 | inline_local_variable, |
321 | " | 321 | " |
322 | fn foo() { | 322 | fn foo() { |
323 | let mut a<|> = 1 + 1; | 323 | let mut a<|> = 1 + 1; |
@@ -329,7 +329,7 @@ fn foo() { | |||
329 | #[test] | 329 | #[test] |
330 | fn test_call_expr() { | 330 | fn test_call_expr() { |
331 | check_assist( | 331 | check_assist( |
332 | inline_local_varialbe, | 332 | inline_local_variable, |
333 | " | 333 | " |
334 | fn foo() { | 334 | fn foo() { |
335 | let a<|> = bar(10 + 1); | 335 | let a<|> = bar(10 + 1); |
@@ -347,7 +347,7 @@ fn foo() { | |||
347 | #[test] | 347 | #[test] |
348 | fn test_index_expr() { | 348 | fn test_index_expr() { |
349 | check_assist( | 349 | check_assist( |
350 | inline_local_varialbe, | 350 | inline_local_variable, |
351 | " | 351 | " |
352 | fn foo() { | 352 | fn foo() { |
353 | let x = vec![1, 2, 3]; | 353 | let x = vec![1, 2, 3]; |
@@ -367,7 +367,7 @@ fn foo() { | |||
367 | #[test] | 367 | #[test] |
368 | fn test_method_call_expr() { | 368 | fn test_method_call_expr() { |
369 | check_assist( | 369 | check_assist( |
370 | inline_local_varialbe, | 370 | inline_local_variable, |
371 | " | 371 | " |
372 | fn foo() { | 372 | fn foo() { |
373 | let bar = vec![1]; | 373 | let bar = vec![1]; |
@@ -387,7 +387,7 @@ fn foo() { | |||
387 | #[test] | 387 | #[test] |
388 | fn test_field_expr() { | 388 | fn test_field_expr() { |
389 | check_assist( | 389 | check_assist( |
390 | inline_local_varialbe, | 390 | inline_local_variable, |
391 | " | 391 | " |
392 | struct Bar { | 392 | struct Bar { |
393 | foo: usize | 393 | foo: usize |
@@ -415,7 +415,7 @@ fn foo() { | |||
415 | #[test] | 415 | #[test] |
416 | fn test_try_expr() { | 416 | fn test_try_expr() { |
417 | check_assist( | 417 | check_assist( |
418 | inline_local_varialbe, | 418 | inline_local_variable, |
419 | " | 419 | " |
420 | fn foo() -> Option<usize> { | 420 | fn foo() -> Option<usize> { |
421 | let bar = Some(1); | 421 | let bar = Some(1); |
@@ -437,7 +437,7 @@ fn foo() -> Option<usize> { | |||
437 | #[test] | 437 | #[test] |
438 | fn test_ref_expr() { | 438 | fn test_ref_expr() { |
439 | check_assist( | 439 | check_assist( |
440 | inline_local_varialbe, | 440 | inline_local_variable, |
441 | " | 441 | " |
442 | fn foo() { | 442 | fn foo() { |
443 | let bar = 10; | 443 | let bar = 10; |
@@ -455,7 +455,7 @@ fn foo() { | |||
455 | #[test] | 455 | #[test] |
456 | fn test_tuple_expr() { | 456 | fn test_tuple_expr() { |
457 | check_assist( | 457 | check_assist( |
458 | inline_local_varialbe, | 458 | inline_local_variable, |
459 | " | 459 | " |
460 | fn foo() { | 460 | fn foo() { |
461 | let a<|> = (10, 20); | 461 | let a<|> = (10, 20); |
@@ -471,7 +471,7 @@ fn foo() { | |||
471 | #[test] | 471 | #[test] |
472 | fn test_array_expr() { | 472 | fn test_array_expr() { |
473 | check_assist( | 473 | check_assist( |
474 | inline_local_varialbe, | 474 | inline_local_variable, |
475 | " | 475 | " |
476 | fn foo() { | 476 | fn foo() { |
477 | let a<|> = [1, 2, 3]; | 477 | let a<|> = [1, 2, 3]; |
@@ -487,7 +487,7 @@ fn foo() { | |||
487 | #[test] | 487 | #[test] |
488 | fn test_paren() { | 488 | fn test_paren() { |
489 | check_assist( | 489 | check_assist( |
490 | inline_local_varialbe, | 490 | inline_local_variable, |
491 | " | 491 | " |
492 | fn foo() { | 492 | fn foo() { |
493 | let a<|> = (10 + 20); | 493 | let a<|> = (10 + 20); |
@@ -505,7 +505,7 @@ fn foo() { | |||
505 | #[test] | 505 | #[test] |
506 | fn test_path_expr() { | 506 | fn test_path_expr() { |
507 | check_assist( | 507 | check_assist( |
508 | inline_local_varialbe, | 508 | inline_local_variable, |
509 | " | 509 | " |
510 | fn foo() { | 510 | fn foo() { |
511 | let d = 10; | 511 | let d = 10; |
@@ -525,7 +525,7 @@ fn foo() { | |||
525 | #[test] | 525 | #[test] |
526 | fn test_block_expr() { | 526 | fn test_block_expr() { |
527 | check_assist( | 527 | check_assist( |
528 | inline_local_varialbe, | 528 | inline_local_variable, |
529 | " | 529 | " |
530 | fn foo() { | 530 | fn foo() { |
531 | let a<|> = { 10 }; | 531 | let a<|> = { 10 }; |
@@ -543,7 +543,7 @@ fn foo() { | |||
543 | #[test] | 543 | #[test] |
544 | fn test_used_in_different_expr1() { | 544 | fn test_used_in_different_expr1() { |
545 | check_assist( | 545 | check_assist( |
546 | inline_local_varialbe, | 546 | inline_local_variable, |
547 | " | 547 | " |
548 | fn foo() { | 548 | fn foo() { |
549 | let a<|> = 10 + 20; | 549 | let a<|> = 10 + 20; |
@@ -565,7 +565,7 @@ fn foo() { | |||
565 | #[test] | 565 | #[test] |
566 | fn test_used_in_for_expr() { | 566 | fn test_used_in_for_expr() { |
567 | check_assist( | 567 | check_assist( |
568 | inline_local_varialbe, | 568 | inline_local_variable, |
569 | " | 569 | " |
570 | fn foo() { | 570 | fn foo() { |
571 | let a<|> = vec![10, 20]; | 571 | let a<|> = vec![10, 20]; |
@@ -581,7 +581,7 @@ fn foo() { | |||
581 | #[test] | 581 | #[test] |
582 | fn test_used_in_while_expr() { | 582 | fn test_used_in_while_expr() { |
583 | check_assist( | 583 | check_assist( |
584 | inline_local_varialbe, | 584 | inline_local_variable, |
585 | " | 585 | " |
586 | fn foo() { | 586 | fn foo() { |
587 | let a<|> = 1 > 0; | 587 | let a<|> = 1 > 0; |
@@ -597,7 +597,7 @@ fn foo() { | |||
597 | #[test] | 597 | #[test] |
598 | fn test_used_in_break_expr() { | 598 | fn test_used_in_break_expr() { |
599 | check_assist( | 599 | check_assist( |
600 | inline_local_varialbe, | 600 | inline_local_variable, |
601 | " | 601 | " |
602 | fn foo() { | 602 | fn foo() { |
603 | let a<|> = 1 + 1; | 603 | let a<|> = 1 + 1; |
@@ -617,7 +617,7 @@ fn foo() { | |||
617 | #[test] | 617 | #[test] |
618 | fn test_used_in_return_expr() { | 618 | fn test_used_in_return_expr() { |
619 | check_assist( | 619 | check_assist( |
620 | inline_local_varialbe, | 620 | inline_local_variable, |
621 | " | 621 | " |
622 | fn foo() { | 622 | fn foo() { |
623 | let a<|> = 1 > 0; | 623 | let a<|> = 1 > 0; |
@@ -633,7 +633,7 @@ fn foo() { | |||
633 | #[test] | 633 | #[test] |
634 | fn test_used_in_match_expr() { | 634 | fn test_used_in_match_expr() { |
635 | check_assist( | 635 | check_assist( |
636 | inline_local_varialbe, | 636 | inline_local_variable, |
637 | " | 637 | " |
638 | fn foo() { | 638 | fn foo() { |
639 | let a<|> = 1 > 0; | 639 | let a<|> = 1 > 0; |
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index d45b58966..3337805a5 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -157,7 +157,7 @@ mod assists { | |||
157 | add_import::add_import, | 157 | add_import::add_import, |
158 | add_missing_impl_members::add_missing_impl_members, | 158 | add_missing_impl_members::add_missing_impl_members, |
159 | add_missing_impl_members::add_missing_default_members, | 159 | add_missing_impl_members::add_missing_default_members, |
160 | inline_local_variable::inline_local_varialbe, | 160 | inline_local_variable::inline_local_variable, |
161 | move_guard::move_guard_to_arm_body, | 161 | move_guard::move_guard_to_arm_body, |
162 | move_guard::move_arm_cond_to_match_guard, | 162 | move_guard::move_arm_cond_to_match_guard, |
163 | move_bounds::move_bounds_to_where_clause, | 163 | move_bounds::move_bounds_to_where_clause, |