aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/assists/add_explicit_type.rs27
-rw-r--r--crates/ra_assists/src/assists/inline_local_variable.rs50
-rw-r--r--crates/ra_assists/src/lib.rs2
3 files changed, 52 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..713abf910 100644
--- a/crates/ra_assists/src/assists/add_explicit_type.rs
+++ b/crates/ra_assists/src/assists/add_explicit_type.rs
@@ -1,7 +1,8 @@
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 SyntaxKind::EQ,
5 TextRange, T,
5}; 6};
6 7
7use crate::{Assist, AssistCtx, AssistId}; 8use crate::{Assist, AssistCtx, AssistId};
@@ -34,6 +35,14 @@ pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi
34 // The binding must have a name 35 // The binding must have a name
35 let name = pat.name()?; 36 let name = pat.name()?;
36 let name_range = name.syntax().text_range(); 37 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 }
37 // Assist not applicable if the type has already been specified 46 // Assist not applicable if the type has already been specified
38 if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) { 47 if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) {
39 return None; 48 return None;
@@ -109,4 +118,20 @@ mod tests {
109 fn add_explicit_type_not_applicable_if_specified_ty_is_tuple() { 118 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); }"); 119 check_assist_not_applicable(add_explicit_type, "fn f() { let a<|>: (i32, i32) = (3, 4); }");
111 } 120 }
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 }
112} 137}
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// ```
26pub(crate) fn inline_local_varialbe(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 26pub(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 "
122fn bar(a: usize) {} 122fn bar(a: usize) {}
123fn foo() { 123fn 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 "
156fn bar(a: usize) {} 156fn bar(a: usize) {}
157fn foo() { 157fn 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 "
190fn bar(a: usize) {} 190fn bar(a: usize) {}
191fn foo() { 191fn 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 "
224fn bar(a: usize): usize { a } 224fn bar(a: usize): usize { a }
225fn foo() { 225fn 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 "
258fn foo() { 258fn 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 "
290fn foo() { 290fn 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 "
322fn foo() { 322fn 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 "
334fn foo() { 334fn 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 "
352fn foo() { 352fn 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 "
372fn foo() { 372fn 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 "
392struct Bar { 392struct 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 "
420fn foo() -> Option<usize> { 420fn 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 "
442fn foo() { 442fn 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 "
460fn foo() { 460fn 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 "
476fn foo() { 476fn 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 "
492fn foo() { 492fn 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 "
510fn foo() { 510fn 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 "
530fn foo() { 530fn 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 "
548fn foo() { 548fn 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 "
570fn foo() { 570fn 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 "
586fn foo() { 586fn 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 "
602fn foo() { 602fn 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 "
622fn foo() { 622fn 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 "
638fn foo() { 638fn 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,