diff options
author | Aleksey Kladov <[email protected]> | 2019-02-24 11:18:10 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-02-24 11:18:10 +0000 |
commit | b3cc7c057d3e926c9a31467cb956a9c6f5320740 (patch) | |
tree | 26dd668348c30a4a7f7b973c00e7c0359cf417e4 /crates/ra_assists/src | |
parent | ef442b8682909f2ab758f55507d4c2e81673cfa1 (diff) |
dont show introduce variable everywhere
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/introduce_variable.rs | 68 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 20 |
2 files changed, 42 insertions, 46 deletions
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index 3d708ebb2..3fab29e29 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs | |||
@@ -9,6 +9,9 @@ use ra_syntax::{ | |||
9 | use crate::{AssistCtx, Assist, AssistId}; | 9 | use crate::{AssistCtx, Assist, AssistId}; |
10 | 10 | ||
11 | pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 11 | pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
12 | if ctx.frange.range.is_empty() { | ||
13 | return None; | ||
14 | } | ||
12 | let node = ctx.covering_node(); | 15 | let node = ctx.covering_node(); |
13 | if !valid_covering_node(node) { | 16 | if !valid_covering_node(node) { |
14 | return None; | 17 | return None; |
@@ -118,7 +121,7 @@ fn anchor_stmt(expr: &ast::Expr) -> Option<(&SyntaxNode, bool)> { | |||
118 | #[cfg(test)] | 121 | #[cfg(test)] |
119 | mod tests { | 122 | mod tests { |
120 | use super::*; | 123 | use super::*; |
121 | use crate::helpers::{check_assist, check_assist_not_applicable, check_assist_range, check_assist_target, check_assist_range_target}; | 124 | use crate::helpers::{check_assist_range_not_applicable, check_assist_range, check_assist_range_target}; |
122 | 125 | ||
123 | #[test] | 126 | #[test] |
124 | fn test_introduce_var_simple() { | 127 | fn test_introduce_var_simple() { |
@@ -309,11 +312,11 @@ fn main() { | |||
309 | 312 | ||
310 | #[test] | 313 | #[test] |
311 | fn test_introduce_var_path_simple() { | 314 | fn test_introduce_var_path_simple() { |
312 | check_assist( | 315 | check_assist_range( |
313 | introduce_variable, | 316 | introduce_variable, |
314 | " | 317 | " |
315 | fn main() { | 318 | fn main() { |
316 | let o = S<|>ome(true); | 319 | let o = <|>Some(true)<|>; |
317 | } | 320 | } |
318 | ", | 321 | ", |
319 | " | 322 | " |
@@ -327,11 +330,11 @@ fn main() { | |||
327 | 330 | ||
328 | #[test] | 331 | #[test] |
329 | fn test_introduce_var_path_method() { | 332 | fn test_introduce_var_path_method() { |
330 | check_assist( | 333 | check_assist_range( |
331 | introduce_variable, | 334 | introduce_variable, |
332 | " | 335 | " |
333 | fn main() { | 336 | fn main() { |
334 | let v = b<|>ar.foo(); | 337 | let v = <|>bar.foo()<|>; |
335 | } | 338 | } |
336 | ", | 339 | ", |
337 | " | 340 | " |
@@ -345,11 +348,11 @@ fn main() { | |||
345 | 348 | ||
346 | #[test] | 349 | #[test] |
347 | fn test_introduce_var_return() { | 350 | fn test_introduce_var_return() { |
348 | check_assist( | 351 | check_assist_range( |
349 | introduce_variable, | 352 | introduce_variable, |
350 | " | 353 | " |
351 | fn foo() -> u32 { | 354 | fn foo() -> u32 { |
352 | r<|>eturn 2 + 2; | 355 | <|>return 2 + 2<|>; |
353 | } | 356 | } |
354 | ", | 357 | ", |
355 | " | 358 | " |
@@ -363,13 +366,13 @@ fn foo() -> u32 { | |||
363 | 366 | ||
364 | #[test] | 367 | #[test] |
365 | fn test_introduce_var_does_not_add_extra_whitespace() { | 368 | fn test_introduce_var_does_not_add_extra_whitespace() { |
366 | check_assist( | 369 | check_assist_range( |
367 | introduce_variable, | 370 | introduce_variable, |
368 | " | 371 | " |
369 | fn foo() -> u32 { | 372 | fn foo() -> u32 { |
370 | 373 | ||
371 | 374 | ||
372 | r<|>eturn 2 + 2; | 375 | <|>return 2 + 2<|>; |
373 | } | 376 | } |
374 | ", | 377 | ", |
375 | " | 378 | " |
@@ -382,12 +385,12 @@ fn foo() -> u32 { | |||
382 | ", | 385 | ", |
383 | ); | 386 | ); |
384 | 387 | ||
385 | check_assist( | 388 | check_assist_range( |
386 | introduce_variable, | 389 | introduce_variable, |
387 | " | 390 | " |
388 | fn foo() -> u32 { | 391 | fn foo() -> u32 { |
389 | 392 | ||
390 | r<|>eturn 2 + 2; | 393 | <|>return 2 + 2<|>; |
391 | } | 394 | } |
392 | ", | 395 | ", |
393 | " | 396 | " |
@@ -399,7 +402,7 @@ fn foo() -> u32 { | |||
399 | ", | 402 | ", |
400 | ); | 403 | ); |
401 | 404 | ||
402 | check_assist( | 405 | check_assist_range( |
403 | introduce_variable, | 406 | introduce_variable, |
404 | " | 407 | " |
405 | fn foo() -> u32 { | 408 | fn foo() -> u32 { |
@@ -408,7 +411,7 @@ fn foo() -> u32 { | |||
408 | // bar | 411 | // bar |
409 | 412 | ||
410 | 413 | ||
411 | r<|>eturn 2 + 2; | 414 | <|>return 2 + 2<|>; |
412 | } | 415 | } |
413 | ", | 416 | ", |
414 | " | 417 | " |
@@ -427,12 +430,12 @@ fn foo() -> u32 { | |||
427 | 430 | ||
428 | #[test] | 431 | #[test] |
429 | fn test_introduce_var_break() { | 432 | fn test_introduce_var_break() { |
430 | check_assist( | 433 | check_assist_range( |
431 | introduce_variable, | 434 | introduce_variable, |
432 | " | 435 | " |
433 | fn main() { | 436 | fn main() { |
434 | let result = loop { | 437 | let result = loop { |
435 | b<|>reak 2 + 2; | 438 | <|>break 2 + 2<|>; |
436 | }; | 439 | }; |
437 | } | 440 | } |
438 | ", | 441 | ", |
@@ -449,11 +452,11 @@ fn main() { | |||
449 | 452 | ||
450 | #[test] | 453 | #[test] |
451 | fn test_introduce_var_for_cast() { | 454 | fn test_introduce_var_for_cast() { |
452 | check_assist( | 455 | check_assist_range( |
453 | introduce_variable, | 456 | introduce_variable, |
454 | " | 457 | " |
455 | fn main() { | 458 | fn main() { |
456 | let v = 0f32 a<|>s u32; | 459 | let v = <|>0f32 as u32<|>; |
457 | } | 460 | } |
458 | ", | 461 | ", |
459 | " | 462 | " |
@@ -467,39 +470,26 @@ fn main() { | |||
467 | 470 | ||
468 | #[test] | 471 | #[test] |
469 | fn test_introduce_var_for_return_not_applicable() { | 472 | fn test_introduce_var_for_return_not_applicable() { |
470 | check_assist_not_applicable( | 473 | check_assist_range_not_applicable(introduce_variable, "fn foo() { <|>return<|>; } "); |
471 | introduce_variable, | ||
472 | " | ||
473 | fn foo() { | ||
474 | r<|>eturn; | ||
475 | } | ||
476 | ", | ||
477 | ); | ||
478 | } | 474 | } |
479 | 475 | ||
480 | #[test] | 476 | #[test] |
481 | fn test_introduce_var_for_break_not_applicable() { | 477 | fn test_introduce_var_for_break_not_applicable() { |
482 | check_assist_not_applicable( | 478 | check_assist_range_not_applicable( |
483 | introduce_variable, | 479 | introduce_variable, |
484 | " | 480 | "fn main() { loop { <|>break<|>; }; }", |
485 | fn main() { | ||
486 | loop { | ||
487 | b<|>reak; | ||
488 | }; | ||
489 | } | ||
490 | ", | ||
491 | ); | 481 | ); |
492 | } | 482 | } |
493 | 483 | ||
494 | #[test] | 484 | #[test] |
495 | fn test_introduce_var_in_comment_not_applicable() { | 485 | fn test_introduce_var_in_comment_not_applicable() { |
496 | check_assist_not_applicable( | 486 | check_assist_range_not_applicable( |
497 | introduce_variable, | 487 | introduce_variable, |
498 | " | 488 | " |
499 | fn main() { | 489 | fn main() { |
500 | let x = true; | 490 | let x = true; |
501 | let tuple = match x { | 491 | let tuple = match x { |
502 | // c<|>omment | 492 | // <|>comment<|> |
503 | true => (2 + 2, true) | 493 | true => (2 + 2, true) |
504 | _ => (0, false) | 494 | _ => (0, false) |
505 | }; | 495 | }; |
@@ -511,13 +501,9 @@ fn main() { | |||
511 | // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic | 501 | // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic |
512 | #[test] | 502 | #[test] |
513 | fn introduce_var_target() { | 503 | fn introduce_var_target() { |
514 | check_assist_target( | 504 | check_assist_range_target( |
515 | introduce_variable, | 505 | introduce_variable, |
516 | " | 506 | "fn foo() -> u32 { <|>return 2 + 2<|>; }", |
517 | fn foo() -> u32 { | ||
518 | r<|>eturn 2 + 2; | ||
519 | } | ||
520 | ", | ||
521 | "2 + 2", | 507 | "2 + 2", |
522 | ); | 508 | ); |
523 | 509 | ||
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 56d276867..8e161dd37 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -259,6 +259,17 @@ mod helpers { | |||
259 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 259 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); |
260 | assert!(assist.is_none()); | 260 | assert!(assist.is_none()); |
261 | } | 261 | } |
262 | |||
263 | pub(crate) fn check_assist_range_not_applicable( | ||
264 | assist: fn(AssistCtx<MockDatabase>) -> Option<Assist>, | ||
265 | before: &str, | ||
266 | ) { | ||
267 | let (range, before) = extract_range(before); | ||
268 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | ||
269 | let frange = FileRange { file_id, range }; | ||
270 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | ||
271 | assert!(assist.is_none()); | ||
272 | } | ||
262 | } | 273 | } |
263 | 274 | ||
264 | #[cfg(test)] | 275 | #[cfg(test)] |
@@ -266,7 +277,7 @@ mod tests { | |||
266 | use hir::mock::MockDatabase; | 277 | use hir::mock::MockDatabase; |
267 | use ra_syntax::TextRange; | 278 | use ra_syntax::TextRange; |
268 | use ra_db::FileRange; | 279 | use ra_db::FileRange; |
269 | use test_utils::{extract_offset}; | 280 | use test_utils::{extract_offset, extract_range}; |
270 | 281 | ||
271 | #[test] | 282 | #[test] |
272 | fn assist_order_field_struct() { | 283 | fn assist_order_field_struct() { |
@@ -286,16 +297,15 @@ mod tests { | |||
286 | fn assist_order_if_expr() { | 297 | fn assist_order_if_expr() { |
287 | let before = " | 298 | let before = " |
288 | pub fn test_some_range(a: int) -> bool { | 299 | pub fn test_some_range(a: int) -> bool { |
289 | if let 2..6 = 5<|> { | 300 | if let 2..6 = <|>5<|> { |
290 | true | 301 | true |
291 | } else { | 302 | } else { |
292 | false | 303 | false |
293 | } | 304 | } |
294 | }"; | 305 | }"; |
295 | let (before_cursor_pos, before) = extract_offset(before); | 306 | let (range, before) = extract_range(before); |
296 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); | 307 | let (db, _source_root, file_id) = MockDatabase::with_single_file(&before); |
297 | let frange = | 308 | let frange = FileRange { file_id, range }; |
298 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | ||
299 | let assists = super::assists(&db, frange); | 309 | let assists = super::assists(&db, frange); |
300 | let mut assists = assists.iter(); | 310 | let mut assists = assists.iter(); |
301 | 311 | ||